C++ 检查句子是否为同构句
在这个问题中,我们需要检查给定的句子是否为同构句。如果所有单词的首字母都相同,我们可以说该句子是同构句。
我们将学习两种解决该问题的方法。解决该问题的逻辑是检查所有单词的首字母。如果任何一个单词的首字母不匹配,我们可以说该句子不是同构句。
问题陈述 –我们有一个包含N个字符的字符串。我们需要检查给定的字符串是否为同构句。
注意 –同构句包含所有以相同字符开头的单词。
示例示例
输入
str = "Tutorialspoint teaches tough things!"
输出
'Yes'
解释 - 给定的句子是同音的,因为所有单词的第一个字符相同。
输入
str = "Hi! How are you?";
输出结果
‘No’
解释 - 给定的句子不是同字母开头句,因为所有单词的第一个字母不一样。
输入
str = ‘mno’
输出
‘Yes’
解释 - 该字符串只包含一个单词,因此它总是谐音的。
方法1
在这个方法中,我们将取第一个单词的第一个字符。然后,通过遍历字符串来获取字符串的每个单词。当在字符串中找到空格时,我们可以初始化一个新的单词,并将字符追加到字符串直到下一个空格,并检查第一个字符是否相同。
算法
步骤1 - 将字符串转换为小写。
步骤2 - 将字符串的第一个字符存储在变量‘firstChar’中。
步骤3 - 用一个空字符串初始化变量‘currentWord’以存储单词。
步骤4 - 开始遍历字符串。
步骤5 - 如果当前字符是空格,并且‘currentWord’的第一个字符与变量‘firstChar’不匹配,则返回‘No’。
步骤6 - 同样,将‘currentWord’初始化为空字符串。
步骤7 - 如果当前字符不是空格,则将字符追加到currentWord中。
步骤8 - 最后,返回‘Yes’。
例子
#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) 因为我们不使用额外的空间。
方法2
在这个方法中,我们将使用空格作为分隔符来拆分字符串。然后,我们将遍历单词数组以检查每个单词的第一个字符。
算法
步骤1 - 在第一步中,我们需要将字符串转换为小写。
步骤2 - 执行getWords()函数来获取字符串的所有单词,并将它们存储在allWords向量中。
步骤2.1 - 在getWords()函数中,使用istringstream将字符串转换为流。
步骤2.2 - 定义allWords和currentWord变量。
步骤2.3 - 使用while循环遍历字符串流,逐个获取所有单词并将它们存储在allWords向量中。
步骤2.4 - 返回allWords向量。
步骤3 - 从向量中获取第一个单词及其第一个字符。
步骤4 - 遍历单词列表,检查每个单词的第一个字符。
步骤5 - 如果在任何单词中找到第一个字符不匹配,则返回“不”。
步骤6 - 最后返回“Yes”。
示例
#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)来将所有单词存储在列表中。
在这两种方法中,我们获取句子的每个单词,并将其第一个字符与字符串的第一个字符匹配。第一种方法比第二种方法更快,因为我们只遍历字符串一次。