📜  句子回文(去除空格、点等后的回文)

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

句子回文(去除空格、点等后的回文)

编写一个程序来检查一个句子是否是回文。您可以忽略空格和其他字符以将句子视为回文。
例子:

Input : str = "Too hot to hoot."
Output : Sentence is palindrome.

Input : str = "Abc def ghi jklm."
Output : Sentence is not palindrome.

请注意,两个单词之间可能有多个空格和/或点。

要确定一个句子是否是回文,请从左右比较每个字符。如果它们相等,则比较直到字符串的左右相等或右变得小于左。请记住忽略字符串中的空格和其他字符。

C++
// CPP program to find if a sentence is
// palindrome
#include 
using namespace std;
  
// To check sentence is palindrome or not
bool sentencePalindrome(string str)
{
    int l = 0, h = str.length() - 1;
  
    // Lowercase string
    for (int i = 0; i <= h; i++)
        str[i] = tolower(str[i]);
  
    // Compares character until they are equal
    while (l <= h) {
  
        // If there is another symbol in left
        // of sentence
        if (!(str[l] >= 'a' && str[l] <= 'z'))
            l++;
  
        // If there is another symbol in right
        // of sentence
        else if (!(str[h] >= 'a' && str[h] <= 'z'))
            h--;
  
        // If characters are equal
        else if (str[l] == str[h])
            l++, h--;
  
        // If characters are not equal then
        // sentence is not palindrome
        else
            return false;
    }
  
    // Returns true if sentence is palindrome
    return true;
}
  
// Driver program to test sentencePalindrome()
int main()
{
    string str = "Too hot to hoot.";
  
    if (sentencePalindrome(str))
        cout << "Sentence is palindrome.";
    else
        cout << "Sentence is not palindrome.";
  
    return 0;
}


Java
// Java program to find if a sentence is
// palindrome
public class GFG
{
    // To check sentence is palindrome or not
    static boolean sentencePalindrome(String str)
    {   
        int l = 0;
        int h = str.length()-1;
         
        // Lowercase string
        str = str.toLowerCase();
         
        // Compares character until they are equal
        while(l <= h)
        {
             
            char getAtl = str.charAt(l);
            char getAth = str.charAt(h);
             
            // If there is another symbol in left
            // of sentence
            if (!(getAtl >= 'a' && getAtl <= 'z'))
                l++;
             
            // If there is another symbol in right
            // of sentence
            else if(!(getAth >= 'a' && getAth <= 'z'))
                h--;
             
            // If characters are equal
            else if( getAtl == getAth)
            {
                l++;
                h--;
            }
             
            // If characters are not equal then
            // sentence is not palindrome
            else
                return false;
        }
         
        // Returns true if sentence is palindrome
        return true;   
    }
     
    // Driver program to test sentencePallindrome()
    public static void main(String[] args)
    {
        String str = "Too hot to hoot.";
        if( sentencePalindrome(str))
          System.out.println("Sentence is palindrome");
        else
          System.out.println("Sentence is not" + " " +
                                         "palindrome");
    }
}
 
//This code is contributed by Sumit Ghosh


Python3
# Python program to find if a sentence is
# palindrome
# To check sentence is palindrome or not
def sentencePalindrome(s):
    l, h = 0, len(s) - 1
   
    # Lowercase string
    s = s.lower()
   
    # Compares character until they are equal
    while (l <= h):
   
        # If there is another symbol in left
        # of sentence
        if (not(s[l] >= 'a' and s[l] <= 'z')):
            l += 1
   
        # If there is another symbol in right
        # of sentence
        else if (not(s[h] >= 'a' and s[h] <= 'z')):
            h -= 1
   
        # If characters are equal
        else if (s[l] == s[h]):
            l += 1
            h -= 1
         
        # If characters are not equal then
        # sentence is not palindrome
        else:
            return False
    # Returns true if sentence is palindrome
    return True
   
# Driver program to test sentencePalindrome()
s = "Too hot to hoot."
if (sentencePalindrome(s)):
    print ("Sentence is palindrome.")
else:
    print ("Sentence is not palindrome.")
 
# This code is contributed by Sachin Bisht


C#
// C# program to find if a
// sentence is palindrome
using System;
 
public class GFG
{
     
    // To check sentence is
    // palindrome or not
    static bool sentencePalindrome(String str)
    {
        int l = 0;
        int h = str.Length - 1;
         
        // Lowercase string
        str = str.ToLower();
         
        // Compares character until
        // they are equal
        while(l <= h)
        {
             
            char getAtl = str[l];
            char getAth = str[h];
             
            // If there is another symbol
            // in left of sentence
            if (!(getAtl >= 'a' &&
                  getAtl <= 'z'))
                l++;
             
            // If there is another symbol 
            // in right of sentence
            else if(! (getAth >= 'a' &&
                       getAth <= 'z'))
                h--;
             
            // If characters are equal
            else if( getAtl == getAth)
            {
                l++;
                h--;
            }
             
            // If characters are not equal then
            // sentence is not palindrome
            else
                return false;
        }
         
        // Returns true if sentence
        // is palindrome
        return true;
    }
     
    // Driver Code
    public static void Main()
    {
        String str = "Too hot to hoot.";
        if( sentencePalindrome(str))
        Console.Write("Sentence is palindrome");
        else
        Console.Write("Sentence is not" + " " +
                                     "palindrome");
    }
}
 
// This code is contributed by Nitin Mittal.


PHP
= 'a' && $str[$l] <= 'z'))
            $l++;
   
        // If there is another symbol in right
        // of sentence
        else if (!($str[$h] >= 'a' && $str[$h] <= 'z'))
            $h--;
   
        // If characters are equal
        else if ($str[$l] == $str[$h])
        {
             $l++;
            $h--;
        }
   
        // If characters are not equal then
        // sentence is not palindrome
        else
            return false;
    }
   
    // Returns true if sentence is palindrome
    return true;
}
   
// Driver program to test sentencePalindrome()
 
$str = "Too hot to hoot.";
 
if (sentencePalindrome($str))
    echo "Sentence is palindrome.";
else
    echo "Sentence is not palindrome.";
 
return 0;
?>


Javascript


输出:

Sentence is palindrome.

时间复杂度: O(N) 其中 N 是句子的长度
空间复杂度: O(1)