檢查句子是否為首字母相同詞句
在這個問題中,我們需要檢查給定的句子是否為首字母相同詞句。如果所有單詞的起始字元都相同,我們可以說任何句子都是首字母相同詞句。
我們將學習兩種解決問題的方法。解決問題的邏輯是檢查所有單詞的第一個字元。如果任何單詞的第一個字元不匹配,我們可以說該句子不是首字母相同詞句。
問題陳述 – 我們有一個包含 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) 用於將所有單詞儲存在列表中。
在這兩種方法中,我們都獲取句子的每個單詞,並將其第一個字元與字串的第一個字元進行匹配。第一種方法比第二種方法更快,因為我們只遍歷字串一次。
資料結構
網路
關係資料庫管理系統 (RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP