Given a string S[], the task is to check whether the given string is a tautogramic sentence or not.
A sentence is a tautogram if all the letters in the sentence start with the same letter.
Examples:
Input: S=”Truly tautograms triumph, trumpeting trills to trounce terrible travesties”.
Output: YES
Explanation: Here, all the words in the sentences starts with same letter ‘t’ so it is a tautogram.Input: S = “The metal panels on top of the generator opened like a flower “
Output: NO
Explanation: Here, first word starts with “t” and second word starts with “m” so its not a tautogram.
Approach: The idea is to convert the string to lower case and then split the sentence into words wherever we find ” “ in the string and insert it into array words[]. Then, check whether each of the elements starts with same the same letter. Follow the steps below to solve the problem:
- Convert the string S[] into lower-case.
- Initialize the array words[] and store the words in the string separated by ” “ using the split function.
- Initialize the character first_word as words[0][0].
- Iterate over the range [0, size(words)) using the variable i and perform the following tasks:
- If words[i][0] is not equal to first_word then print NO and return.
- After performing the above steps, print the value “YES” as the answer.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h> using namespace std; vector<string> splitStringIntoString(string str) { // Used to split string around spaces. istringstream ss(str); //initialize vector to store words vector<string> v; string word; // for storing each word // Traverse through all words // while loop till we get // strings to store in string word while (ss >> word) { // print the read word v.push_back(word); } return v; } // Function to check whether the string // is tautogram or not string Is_tautogram(string S) { // Convert the string to lowercase transform(S.begin(), S.end(), S.begin(), :: tolower ); // Split the string into words vector<string> words = splitStringIntoString(S); // Initializing the first letter of // first word to a variable char first_word = words[0][0]; // Iterating the words string word = "" ; for ( int i = 0; i < words.size(); i++) word = words[i]; if (word[0] != first_word) // Returns False when first letter // not equal to first_word return "NO" ; // If all first letters are same returns true return "YES" ; } int main() { string S = "Truly tautograms triumph,trumpeting trills to trounce terrible travesties" ; // Calling function cout << (Is_tautogram(S)); return 0; } // This code is contributed by hrithikgarg03188 |
Java
// Java code for the above approach class GFG { // Function to check whether the string // is tautogram or not static String Is_tautogram(String S) { // Convert the string to lowercase S = S.toLowerCase(); // Split the string into words String[] words = S.split( " " ); // Initializing the first letter of // first word to a variable char first_word = words[ 0 ].charAt( 0 ); // Iterating the words String word = "" ; for ( int i = 0 ; i < words.length; i++) word = words[i]; if (word.charAt( 0 ) != first_word) // Returns False when first letter // not equal to first_word return "NO" ; // If all first letters are same returns true return "YES" ; } // Driver Code public static void main(String args[]) { String S = "Truly tautograms triumph,trumpeting trills to trounce terrible travesties" ; // Calling function System.out.println(Is_tautogram(S)); } } // This code is contributed by Saurabh Jaiswal |
Python3
# Python implementation of above approach # Function to check whether the string # is tautogram or not def Is_tautogram(S): # Convert the string to lowercase S = S.lower() # Split the string into words words = S.split( " " ) # Initializing the first letter of # first word to a variable first_word = words[ 0 ][ 0 ] # Iterating the words for i in range ( 1 , len (words)): word = words[i] if (word[ 0 ] ! = first_word): # Returns False when first letter # not equal to first_word return "NO" # If all first letters are same returns true return "YES" # Driver Code if __name__ = = "__main__" : S = "Truly tautograms triumph, " \ "trumpeting trills to trounce" \ " terrible travesties" # Calling function print (Is_tautogram(S)) |
Javascript
<script> // JavaScript code for the above approach // Function to check whether the string // is tautogram or not function Is_tautogram(S) { // Convert the string to lowercase S = S.toLowerCase(); // Split the string into words words = S.split( " " ) // Initializing the first letter of // first word to a variable first_word = words[0][0] // Iterating the words for (let i = 0; i < words.length; i++) word = words[i] if (word[0] != first_word) // Returns False when first letter // not equal to first_word return "NO" // If all first letters are same returns true return "YES" } // Driver Code let S = "Truly tautograms triumph,trumpeting trills to trounce terrible travesties" // Calling function document.write(Is_tautogram(S)) // This code is contributed by Potta Lokesh </script> |
C#
// C# code for the above approach using System; public class GFG { // Function to check whether the string // is tautogram or not static String Is_tautogram(String S) { // Convert the string to lowercase S = S.ToLower(); // Split the string into words String[] words = S.Split( ' ' ); // Initializing the first letter of // first word to a variable char first_word = words[0][0]; // Iterating the words String word = "" ; for ( int i = 0; i < words.Length; i++) word = words[i]; if (word[0] != first_word) // Returns False when first letter // not equal to first_word return "NO" ; // If all first letters are same returns true return "YES" ; } // Driver Code public static void Main(String []args) { String S = "Truly tautograms triumph,trumpeting trills to trounce terrible travesties" ; // Calling function Console.WriteLine(Is_tautogram(S)); } } // This code is contributed by shikhasingrajput |
YES
Time Complexity: O(N)
Auxiliary Space: O(N)