📌  相关文章
📜  检查一个字符串可以拆分为均匀长度的回文子字符串

📅  最后修改于: 2021-06-25 20:11:22             🧑  作者: Mango

给定一个字符串str ,任务是检查是否有可能将给定的字符串拆分为长度均匀的回文子字符串。
例子:

方法:想法是使用堆栈数据结构。步骤如下:

  1. 初始化一个空堆栈。
  2. 遍历给定的字符串str
  3. 对于给定字符串中的每个字符,请执行以下操作:
    • 如果字符等于堆栈顶部的字符,则从堆栈中弹出顶部元素。
    • 否则将当前字符推入堆栈。
  4. 如果在上述步骤之后堆栈为空,则给定的字符串可以分成长度均匀的回文子字符串。
  5. 否则,给定的字符串不能分解为长度均匀的回文子字符串。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
  
// Function to check string str can be
// split a string into even length
// palindromic substrings
bool check(string s, int n)
{
  
    // Initialize a stack
    stack st;
  
    // Iterate the string
    for (int i = 0; i < n; i++) {
  
        // If the i-th character is same
        // as that at the top of the stack
        // then pop the top element
        if (!st.empty() && st.top() == s[i])
            st.pop();
  
        // Else push the current charactor
        // into the stack
        else
            st.push(s[i]);
    }
  
    // If the stack is empty, then even
    // palindromic substrings are possible
    if (st.empty()) {
        return true;
    }
  
    // Else not-possible
    else {
        return false;
    }
}
  
// Driver Code
int main()
{
    // Given string
    string str = "aanncddc";
  
    int n = str.length();
  
    // Function Call
    if (check(str, n)) {
        cout << "Yes" << endl;
    }
    else {
        cout << "No" << endl;
    }
  
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
  
class GFG{
  
// Function to check String str can be
// split a String into even length
// palindromic subStrings
static boolean check(String s, int n)
{
      
    // Initialize a stack
    Stack st = new Stack();
  
    // Iterate the String
    for(int i = 0; i < n; i++)
    {
          
       // If the i-th character is same
       // as that at the top of the stack
       // then pop the top element
       if (!st.isEmpty() &&
               st.peek() == s.charAt(i))
           st.pop();
             
       // Else push the current charactor
       // into the stack
       else
           st.add(s.charAt(i));
    }
      
    // If the stack is empty, then even
    // palindromic subStrings are possible
    if (st.isEmpty())
    {
        return true;
    }
      
    // Else not-possible
    else
    {
        return false;
    }
}
  
// Driver Code
public static void main(String[] args)
{
      
    // Given String
    String str = "aanncddc";
  
    int n = str.length();
  
    // Function Call
    if (check(str, n))
    {
        System.out.print("Yes" + "\n");
    }
    else
    {
        System.out.print("No" + "\n");
    }
}
}
  
// This code is contributed by sapnasingh4991


Python3
# Python3 program for the above approach 
  
# Function to check string str can be 
# split a string into even length 
# palindromic substrings 
def check(s, n):
  
    st = []
  
    # Iterate the string 
    for i in range(n):
  
        # If the i-th character is same 
        # as that at the top of the stack 
        # then pop the top element 
        if (len(st) != 0 and 
         st[len(st) - 1] == s[i]):
            st.pop(); 
  
        # Else push the current charactor 
        # into the stack 
        else:
            st.append(s[i]); 
      
    # If the stack is empty, then even 
    # palindromic substrings are possible 
    if (len(st) == 0):
        return True; 
      
    # Else not-possible 
    else:
        return False; 
      
# Driver Code 
  
# Given string 
str = "aanncddc"; 
n = len(str)
  
# Function Call 
if (check(str, n)):
    print("Yes")
else: 
    print("No")
  
# This code is contributed by grand_master


C#
// C# program for the above approach
using System;
using System.Collections.Generic;
  
class GFG{
  
// Function to check String str can be
// split a String into even length
// palindromic subStrings
static bool check(String s, int n)
{
      
    // Initialize a stack
    Stack st = new Stack();
  
    // Iterate the String
    for(int i = 0; i < n; i++)
    {
          
        // If the i-th character is same
        // as that at the top of the stack
        // then pop the top element
        if (st.Count != 0 &&
            st.Peek() == s[i])
            st.Pop();
                  
        // Else push the current charactor
        // into the stack
        else
            st.Push(s[i]);
    }
      
    // If the stack is empty, then even
    // palindromic subStrings are possible
    if (st.Count == 0)
    {
        return true;
    }
      
    // Else not-possible
    else
    {
        return false;
    }
}
  
// Driver Code
public static void Main(String[] args)
{
      
    // Given String
    String str = "aanncddc";
  
    int n = str.Length;
  
    // Function call
    if (check(str, n))
    {
        Console.Write("Yes" + "\n");
    }
    else
    {
        Console.Write("No" + "\n");
    }
}
}
  
// This code is contributed by sapnasingh4991


输出:
Yes

时间复杂度: O(N) ,其中N是给定字符串的长度。

如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。