Check whether a sentence is tautogram or not
Last Updated :
25 Jan, 2022
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)
{
istringstream ss(str);
vector<string> v;
string word;
while (ss >> word)
{
v.push_back(word);
}
return v;
}
string Is_tautogram(string S) {
transform(S.begin(), S.end(), S.begin(), :: tolower );
vector<string> words = splitStringIntoString(S);
char first_word = words[0][0];
string word = "" ;
for ( int i = 0; i < words.size(); i++)
word = words[i];
if (word[0] != first_word)
return "NO" ;
return "YES" ;
}
int main() {
string S = "Truly tautograms triumph,trumpeting trills to trounce terrible travesties" ;
cout << (Is_tautogram(S));
return 0;
}
|
Java
class GFG
{
static String Is_tautogram(String S) {
S = S.toLowerCase();
String[] words = S.split( " " );
char first_word = words[ 0 ].charAt( 0 );
String word = "" ;
for ( int i = 0 ; i < words.length; i++)
word = words[i];
if (word.charAt( 0 ) != first_word)
return "NO" ;
return "YES" ;
}
public static void main(String args[])
{
String S = "Truly tautograms triumph,trumpeting trills to trounce terrible travesties" ;
System.out.println(Is_tautogram(S));
}
}
|
Python3
def Is_tautogram(S):
S = S.lower()
words = S.split( " " )
first_word = words[ 0 ][ 0 ]
for i in range ( 1 , len (words)):
word = words[i]
if (word[ 0 ] ! = first_word):
return "NO"
return "YES"
if __name__ = = "__main__" :
S = "Truly tautograms triumph, " \
"trumpeting trills to trounce" \
" terrible travesties"
print (Is_tautogram(S))
|
Javascript
<script>
function Is_tautogram(S) {
S = S.toLowerCase();
words = S.split( " " )
first_word = words[0][0]
for (let i = 0; i < words.length; i++)
word = words[i]
if (word[0] != first_word)
return "NO"
return "YES"
}
let S = "Truly tautograms triumph,trumpeting trills to trounce terrible travesties"
document.write(Is_tautogram(S))
</script>
|
C#
using System;
public class GFG
{
static String Is_tautogram(String S) {
S = S.ToLower();
String[] words = S.Split( ' ' );
char first_word = words[0][0];
String word = "" ;
for ( int i = 0; i < words.Length; i++)
word = words[i];
if (word[0] != first_word)
return "NO" ;
return "YES" ;
}
public static void Main(String []args)
{
String S = "Truly tautograms triumph,trumpeting trills to trounce terrible travesties" ;
Console.WriteLine(Is_tautogram(S));
}
}
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...