📌  相关文章
📜  检查字符串的两半是否都回文

📅  最后修改于: 2021-04-26 08:25:38             🧑  作者: Mango

给定一个字符串str ,任务是检查给定的字符串是否可以分为两半,每个都是回文的。如果可能,请打印“是” 。否则,打印No。
例子:

方法:
请按照以下步骤解决问题:

  • 遍历字符串的前((N / 2)/ 2-1 )个索引。
  • 同时通过以下两个条件检查两个半部是否均为回文式:
    • 如果S [i]不等于S [N / 2 – 1 – i] ,那么前一半不是回文的。
    • 如果S [N / 2 + i]不等于S [N – 1 – i] ,则后一半不是回文的。
  • 如果在迭代过程中一次都不能满足上述条件,则这两个部分都是回文的。打印
  • 否则,打印No。

下面是上述方法的实现:

C++
// C++ Program to check whether
// both halves of a string is
// Palindrome or not
#include 
using namespace std;
 
// Function to check if both halves
// of a string are palindrome or not
void checkPalindrome(string S)
{
    // Length of string
    int N = S.size();
 
    // Initialize both part as true
    bool first_half = true;
    bool second_half = true;
 
    int cnt = (N / 2) - 1;
 
    for (int i = 0; i < ((N / 2) / 2); i++) {
 
        // If first half is not palindrome
        if (S[i] != S[cnt]) {
            first_half = false;
            break;
        }
 
        // If second half is not palindrome
        if (S[N / 2 + i] != S[N / 2 + cnt]) {
            second_half - false;
            break;
        }
 
        cnt--;
    }
 
    // If both halves are Palindrome
    if (first_half && second_half) {
        cout << "Yes" << endl;
    }
    else {
        cout << "No" << endl;
    }
}
 
int main()
{
    string S = "momdad";
 
    checkPalindrome(S);
 
    return 0;
}


Java
// Java Program to check whether
// both halves of a string is
// Palindrome or not
public class Main {
 
    // Function to check and return if
    // both halves of a string are
    // palindromic or not
    public static void checkPalindrome(
        String S)
    {
        // Length of string
        int N = S.length();
 
        // Initialize both part as true
        boolean first_half = true;
        boolean second_half = true;
 
        int cnt = (N / 2) - 1;
 
        for (int i = 0; i < (N / 2); i++) {
 
            // If first half is not palindrome
            if (S.charAt(i) != S.charAt(cnt)) {
                first_half = false;
                break;
            }
 
            // If second half is not palindrome
            if (S.charAt(N / 2 + i)
                != S.charAt(N / 2 + cnt)) {
                second_half = false;
                break;
            }
 
            cnt--;
        }
 
        // If both halves are Palindrome
        if (first_half && second_half) {
            System.out.println("Yes");
        }
        else {
            System.out.println("No");
        }
    }
 
    // Driver Code
    public static void main(String[] args)
    {
 
        String S = "momdad";
 
        checkPalindrome(S);
    }
}


Python3
# Python3 program to check whether
# both halves of a string is
# palindrome or not
 
# Function to check if both halves
# of a string are palindrome or not
def checkPalindrome(S):
     
    # Length of string
    n = len(S)
     
    # Initialize both part as true
    first_half = True
    second_half = True
     
    cnt = (n // 2) - 1
     
    for i in range(0, int((n / 2) / 2)):
         
        # If first half is not palindrome
        if (S[i] != S[cnt]):
            first_half = False
            break
             
        # If second half is not palindrome
        if (S[n // 2 + i] != S[n // 2 + cnt]):
            second_half = False
            break
             
        cnt -= 1
         
    # If both halves are palindrome
    if (first_half and second_half):
        print('Yes', end = '')
    else:
        print('No', end = '')
 
# Driver code
if __name__=='__main__':
     
    S = 'momdad'
     
    checkPalindrome(S)
                 
# This code is contributed by virusbuddah_


C#
// C# program to check whether
// both halves of a string is
// Palindrome or not
using System;
 
class GFG{
 
// Function to check and return if
// both halves of a string are
// palindromic or not
public static void checkPalindrome(String S)
{
     
    // Length of string
    int N = S.Length;
 
    // Initialize both part as true
    bool first_half = true;
    bool second_half = true;
 
    int cnt = (N / 2) - 1;
 
    for(int i = 0; i < (N / 2); i++)
    {
         
        // If first half is not palindrome
        if (S[i] != S[cnt])
        {
            first_half = false;
            break;
        }
 
        // If second half is not palindrome
        if (S[N / 2 + i] != S[N / 2 + cnt])
        {
            second_half = false;
            break;
        }
        cnt--;
    }
 
    // If both halves are Palindrome
    if (first_half && second_half)
    {
        Console.Write("Yes");
    }
    else
    {
        Console.Write("No");
    }
}
 
// Driver code
public static void Main()
{
    String S = "momdad";
 
    checkPalindrome(S);
}
}
 
// This code is contributed by grand_master


Javascript


输出:
Yes

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