📜  检查一个句子是否是重言式

📅  最后修改于: 2022-05-13 01:57:08.523000             🧑  作者: Mango

检查一个句子是否是重言式

给定一个字符串S[],任务是检查给定的字符串是否是一个重言式句子。

例子:

方法:想法是将字符串转换为小写,然后在字符串中找到“”的任何地方将句子拆分为单词并将其插入数组words[]中。然后,检查每个元素是否以相同的相同字母开头。请按照以下步骤解决问题:

  • 将字符串S[]转换为小写。
  • 初始化数组words[]并使用 split函数将单词存储在以“”分隔的字符串中。
  • 将字符first_word初始化为words[0][0]。
  • 使用变量i遍历范围[0, size(words))并执行以下任务:
    • 如果words[i][0]不等于first_word则打印NO并返回。
  • 执行上述步骤后,打印值“YES”作为答案。

下面是上述方法的实现:

C++
#include 
using namespace std;
 
vector splitStringIntoString(string str)
{
    // Used to split string around spaces.
    istringstream ss(str);
   
      //initialize vector to store words
      vector v;
    string word; // for storing each word
   
    // Traverse through all words
    // while loop till we get
    // strings to store in string word
    while (ss >> word)
    {
        // print the read word
        v.push_back(word);
    }
  return v;
}
  // Function to check whether the string
  // is tautogram or not
  string Is_tautogram(string S) {
 
    // Convert the string to lowercase
    transform(S.begin(), S.end(), S.begin(), ::tolower);
 
    // Split the string into words
   vector words = splitStringIntoString(S);
 
    // Initializing the first letter of
    // first word to a variable
    char first_word = words[0][0];
 
    // Iterating the words
    string word = "";
    for (int i = 0; i < words.size(); i++)
      word = words[i];
    if (word[0] != first_word)
 
      // Returns False when first letter
      // not equal to first_word
      return "NO";
 
    // If all first letters are same returns true
    return "YES";
 
  }
 
int main() {
 
    string S = "Truly tautograms triumph,trumpeting trills to trounce terrible travesties";
 
    // Calling function
   cout << (Is_tautogram(S));
    return 0;
}
 
// This code is contributed by hrithikgarg03188


Java
// Java code for the above approach
class GFG
{
 
  // Function to check whether the string
  // is tautogram or not
  static String Is_tautogram(String S) {
 
    // Convert the string to lowercase
    S = S.toLowerCase();
 
    // Split the string into words
    String[] words = S.split(" ");
 
    // Initializing the first letter of
    // first word to a variable
    char first_word = words[0].charAt(0);
 
    // Iterating the words
    String word = "";
    for (int i = 0; i < words.length; i++)
      word = words[i];
    if (word.charAt(0) != first_word)
 
      // Returns False when first letter
      // not equal to first_word
      return "NO";
 
    // If all first letters are same returns true
    return "YES";
 
  }
 
  // Driver Code
  public static void main(String args[])
  {
    String S = "Truly tautograms triumph,trumpeting trills to trounce terrible travesties";
 
    // Calling function
    System.out.println(Is_tautogram(S));
  }
}
 
// This code is contributed by Saurabh Jaiswal


Python3
# Python implementation of above approach
 
# Function to check whether the string
# is tautogram or not
 
 
def Is_tautogram(S):
 
    # Convert the string to lowercase
    S = S.lower()
 
    # Split the string into words
    words = S.split(" ")
 
    # Initializing the first letter of
    # first word to a variable
    first_word = words[0][0]
 
    # Iterating the words
    for i in range(1, len(words)):
        word = words[i]
        if(word[0] != first_word):
 
            # Returns False when first letter
            # not equal to first_word
            return "NO"
 
    # If all first letters are same returns true
    return "YES"
 
 
# Driver Code
if __name__ == "__main__":
    S = "Truly tautograms triumph, "\
         "trumpeting trills to trounce"\
         " terrible travesties"
     
    # Calling function
    print(Is_tautogram(S))


Javascript


C#
// C# code for the above approach
using System;
 
public class GFG
{
 
  // Function to check whether the string
  // is tautogram or not
  static String Is_tautogram(String S) {
 
    // Convert the string to lowercase
    S = S.ToLower();
 
    // Split the string into words
    String[] words = S.Split(' ');
 
    // Initializing the first letter of
    // first word to a variable
    char first_word = words[0][0];
 
    // Iterating the words
    String word = "";
    for (int i = 0; i < words.Length; i++)
      word = words[i];
    if (word[0] != first_word)
 
      // Returns False when first letter
      // not equal to first_word
      return "NO";
 
    // If all first letters are same returns true
    return "YES";
 
  }
 
  // Driver Code
  public static void Main(String []args)
  {
    String S = "Truly tautograms triumph,trumpeting trills to trounce terrible travesties";
 
    // Calling function
    Console.WriteLine(Is_tautogram(S));
  }
}
 
// This code is contributed by shikhasingrajput


输出
YES

时间复杂度: O(N)
辅助空间: O(N)