检查句子是否为首字母相同词句


在这个问题中,我们需要检查给定的句子是否为首字母相同词句。如果所有单词的起始字符都相同,我们可以说任何句子都是首字母相同词句。

我们将学习两种解决问题的方法。解决问题的逻辑是检查所有单词的第一个字符。如果任何单词的第一个字符不匹配,我们可以说该句子不是首字母相同词句。

问题陈述 – 我们有一个包含 N 个字符的字符串。我们需要检查给定的字符串是否为首字母相同词句。

注意 – 首字母相同词句包含所有以相同字符开头的单词。

示例

输入

str = "Tutorialspoint teaches tough things!"

输出

'Yes'

解释- 给定的句子是首字母相同词句,因为所有单词的第一个字符相同。

输入

str = "Hi! How are you?";

输出

‘No’

解释 – 给定的句子不是首字母相同词句,因为所有单词的第一个字符不相同。

输入

str = ‘mno’

输出

‘Yes’

解释 – 字符串只包含一个单词,所以它始终是首字母相同词句。

方法一

在这种方法中,我们将取第一个单词的第一个字符。之后,我们将通过遍历字符串来获取字符串的每个单词。当我们在字符串中得到空格时,我们可以初始化一个新单词并继续追加字符串字符,直到我们得到下一个空格,并检查第一个字符是否相同。

算法

步骤 1 – 将字符串转换为小写。

步骤 2 – 将字符串的第一个字符存储在“firstChar”变量中。

步骤 3 – 使用空字符串初始化“currentWord”变量来存储单词。

步骤 4 – 开始遍历字符串。

步骤 5 – 如果当前字符是空格,并且“currentWord”的第一个字符与“firstChar”变量不匹配,则返回“否”。

步骤 6 – 同样,使用空字符串初始化“currentWord”。

步骤 7 – 如果当前字符不是空格,则将字符附加到currentWord。

步骤 8 – 最后,返回“是”。

示例

#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;
}

输出

The string is tautogram - NO

时间复杂度 – O(N) 用于检查每个单词的第一个字符。

空间复杂度 – O(1),因为我们没有使用额外的空间。

方法二

在这种方法中,我们将使用空格作为分隔符来分割字符串。之后,我们将遍历单词数组以检查每个单词的第一个字符。

算法

步骤 1 – 在第一步中,我们需要将字符串转换为小写。

步骤 2 – 执行 getWords() 函数以获取字符串的所有单词并将它们存储在 allWords 向量中。

步骤 2.1 – 在 getWords() 函数中,使用“istringstream”将字符串转换为流。

步骤 2.2 – 定义“allWords”和“currentWord”变量。

步骤 2.3 – 使用 while 循环遍历字符串流,逐个获取所有单词并将它们存储到“allWords”向量中。

步骤 2.4 – 返回“allWords”向量。

步骤 3 – 从向量中获取第一个单词及其第一个字符。

步骤 4 – 遍历单词列表,并检查每个单词的第一个字符。

步骤 5 - 如果我们在任何单词中发现第一个字符不匹配,则返回“否”。

步骤 6 – 最后返回“是”。

示例

#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;
}

输出

The string is tautogram - YES

时间复杂度 – O(N) 用于分割字符串。

空间复杂度 – O(N) 用于将所有单词存储在列表中。

在这两种方法中,我们都获取句子的每个单词,并将其第一个字符与字符串的第一个字符进行匹配。第一种方法比第二种方法更快,因为我们只遍历字符串一次。

更新于:2023年8月24日

62 次浏览

开始您的职业生涯

完成课程获得认证

开始
广告
© . All rights reserved.