Check whether a Sentence is Tautogram or Not


In this problem, we need to check whether the given sentence is tautogramic. We can say any sentence is tautogramic if all words have the same starting character.

We will learn two approaches to solving the problem. The logic to solve the problem is to check all words' first characters. If any word has a mismatched first character, we can say the sentence is not tautogramic.

Problem statement – We have a string containing the N characters. We need to check whether the given string is tautogramic or not.

Note – The tautogramic contains all words starting with the same character.

Sample examples

Input

str = "Tutorialspoint teaches tough things!"

Output

'Yes'

Explanation- The given sentence is tautogramic, as the first character of all words is same.

Input

str = "Hi! How are you?";

Output

‘No’

Explanation – The given sentence is not tautogramic, as the first character of all words is not same.

Input

str = ‘mno’

Output

‘Yes’

Explanation – The string contains only one word, so it is always tautogramic.

Approach 1

In this approach, we will take the first character of the first word. After that, we will get each word of the string by traversing the string. When we get space in the string, we can initialize a new word and keep appending the string character until we get next space, and check whether the first character is the same.

Algorithm

Step 1 – Convert the string to lowercase.

Step 2 – Store the first character of the string in the ‘firstChar’ variable.

Step 3 – Initialize the ‘currentWord’ variable with an empty string to store words.

Step 4 – Start traversing the string.

Step 5 – If the current character is space, and if the first character of the ‘currentWord’ does not match the ‘firstChar’ variable, return ‘No’.

Step 6 – Also, initialize the ‘currentWord’ with the empty string.

Step 7 – If the current character is not space, append the character to the currentWord.

Step 8 – At last, return ‘Yes’.

Example

#include <bits/stdc++.h>
using namespace std;

string checkTautogram(string str) {
    // Converting to lowercase
    transform(str.begin(), str.end(), str.begin(), ::tolower);
    // Get the first char of the first word
    char firstChar = str[0];
    // to store single word
    string currentWord = "";
    // traverse the string
    for (int p = 0; p < str.length(); p++) {
        // If we find space, check the first character of the word
        if (str[p] == ' ') {
            if (currentWord[0] != firstChar) {
                return "NO";
            }
            currentWord = "";
        } else {
            // append character to word
            currentWord += str[p];
        }
    }
    // return YES, If the sentence is tautogram
    return "YES";
}
int main() {
    string str = "Hi! How are you?";
    cout << "The string is tautogram - " << checkTautogram(str);
    return 0;
}

Output

The string is tautogram - NO

Time complexity – O(N) to check the first character of each word.

Space complexity – O(1) as we don’t use extra space.

Approach 2

We will split the string using space as a delimiter in this approach. After that, we will traverse the array of words to check the first character of each word.

Algorithm

Step 1 – In the first step, we are required to convert the string into lowercase.

Step 2 – Execute the getWords() function to get all words of the string and store them in the allWords vector.

Step 2.1 – In the getWords() function, use the ‘istringstream’ to convert the string to stream.

Step 2.2 – Define the ‘allWords’ and ‘currentWord’ variables.

Step 2.3 – Use the while loop to traverse the string stream, get all words one by one and store them into the ‘allWords’ vector.

Step 2.4 – Return the ‘allWords’ vector.

Step 3 – From the vector, get the first word and its first character.

Step 4 – Traverse the list of words, and check the first character of each word.

Step 5 - Return' No' if we find first character mismatch in any word.

Step 6 – Return ‘Yes’ at last.

Example

#include <bits/stdc++.h>
using namespace std;

vector<string> getWords(string str) {
    // Split the string
    istringstream ss(str);
    vector<string> allWords;
    string currentWord;
    while (ss >> currentWord) {
        // insert the word in vector
        allWords.push_back(currentWord);
    }
    // return vector of words
    return allWords;
}
string checkTautogram(string str) {
    // Converting to lowercase
    transform(str.begin(), str.end(), str.begin(), ::tolower);
    // Get words of a sentence
    vector<string> allWords = getWords(str);
    // Get the first char of the first word
    char firstChar = allWords[0][0];
    // Traverse the word list
    for (int p = 0; p < allWords.size(); p++) {
        string currentWord = allWords[p];
        if (currentWord[0] != firstChar) {
            return "NO";
        }
    }
    // return YES, If the sentence is tautogram
    return "YES";
}
int main() {
    string str = "Tutorialspoint teaches tough things!";
    cout << "The string is tautogram - " << checkTautogram(str);
    return 0;
}

Output

The string is tautogram - YES

Time complexity – O(N) to split the string.

Space complexity – O(N) to store all words in the list.

In both approaches, we get each word of the sentence and match its first character with the string’s first character. The first approach is faster than the second approach as we traverse the string only once.

Updated on: 24-Aug-2023

35 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements