📌  相关文章
📜  检查一个字符串可以分为两个子字符串,以便一个子字符串是另一个子字符串

📅  最后修改于: 2021-04-22 00:09:00             🧑  作者: Mango

给定长度为N的字符串S ,任务是检查字符串可以分为两个子字符串,例如AB ,使得BA的子字符串。如果无法打印,请打印No。否则,打印

例子 :

天真的方法:解决该问题的最简单方法是在每个可能的索引处分割字符串S ,并检查右子字符串是否为左子字符串的子字符串。如果任何拆分均满足条件,则打印“是”。否则,打印“否”。

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

高效方法:为了优化上述方法,其思想是检查字符串S的最后一个字符是否存在于其余字符串。请按照以下步骤解决问题:

  • S的最后一个字符存储在c中
  • 检查c是否存在于子字符串S [0,N-2]中
  • 如果发现是真的,则打印“是” 。否则打印“否”

下面是上述方法的实现:

C++
// C++ program to implement
// the above approach
#include 
using namespace std;
 
// Function to check if a string can be
// divided into two substrings such that
// one substring is substring of the other
void splitString(string S, int N)
{
    // Store the last character of S
    char c = S[N - 1];
 
    int f = 0;
 
    // Traverse the characters at indices [0, N-2]
    for (int i = 0; i < N - 1; i++) {
 
        // Check if the current character is
        // equal to the last character
        if (S[i] == c) {
 
            // If true, set f = 1
            f = 1;
 
            // Break out of the loop
            break;
        }
    }
 
    if (f)
        cout << "Yes";
    else
        cout << "No";
}
 
// Driver Code
int main()
{
    // Given string, S
    string S = "abcdab";
 
    // Store the size of S
    int N = S.size();
 
    // Function Call
    splitString(S, N);
 
    return 0;
}


Java
// Java program to implement
// the above approach
import java.util.*;
class GFG{
 
// Function to check if a String can be
// divided into two subStrings such that
// one subString is subString of the other
static void splitString(String S, int N)
{
   
    // Store the last character of S
    char c = S.charAt(N - 1);
    int f = 0;
 
    // Traverse the characters at indices [0, N-2]
    for (int i = 0; i < N - 1; i++)
    {
 
        // Check if the current character is
        // equal to the last character
        if (S.charAt(i) == c)
        {
 
            // If true, set f = 1
            f = 1;
 
            // Break out of the loop
            break;
        }
    }
 
    if (f > 0)
        System.out.print("Yes");
    else
        System.out.print("No");
}
 
// Driver Code
public static void main(String[] args)
{
   
    // Given String, S
    String S = "abcdab";
 
    // Store the size of S
    int N = S.length();
 
    // Function Call
    splitString(S, N);
}
}
 
// This code is contributed by 29AjayKumar


Python3
# Python3 program to implement
# the above approach
 
# Function to check if a can be
# divided into two substrings such that
# one subis subof the other
def splitString(S, N):
     
    # Store the last character of S
    c = S[N - 1]
 
    f = 0
 
    # Traverse the characters at indices [0, N-2]
    for i in range(N - 1):
         
        # Check if the current character is
        # equal to the last character
        if (S[i] == c):
             
            # If true, set f = 1
            f = 1
             
            # Break out of the loop
            break
 
    if (f):
        print("Yes")
    else:
        print("No")
 
# Driver Code
if __name__ == '__main__':
     
    # Given string, S
    S = "abcdab"
 
    # Store the size of S
    N = len(S)
 
    # Function Call
    splitString(S, N)
     
# This code is contributed by mohit kumar 29


C#
// C# program to implement
// the above approach 
using System;
class GFG{
  
// Function to check if a string can be
// divided into two substrings such that
// one substring is substring of the other
static void splitString(string S, int N)
{
    // Store the last character of S
    char c = S[N - 1];
    int f = 0;
  
    // Traverse the characters at indices [0, N-2]
    for (int i = 0; i < N - 1; i++)
    {
  
        // Check if the current character is
        // equal to the last character
        if (S[i] == c)
        {
  
            // If true, set f = 1
            f = 1;
  
            // Break out of the loop
            break;
        }
    }
  
    if (f != 0)
        Console.Write("Yes");
    else
        Console.Write("No");
}
  
// Driver code
public static void Main()
{
    // Given string, S
    string S = "abcdab";
  
    // Store the size of S
    int N = S.Length;
  
    // Function Call
    splitString(S, N);
}
}
 
// This code is contributed by susmitakundugoaldanga


输出:
Yes

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