📌  相关文章
📜  检查一个字符串可以分成两个子字符串,使得一个子字符串是另一个的子字符串

📅  最后修改于: 2021-10-26 02:34:45             🧑  作者: Mango

给定一个长度为N的字符串S ,任务是检查一个字符串可以分成两个子字符串,比如说AB ,这样BA的子字符串。如果不可能,请打印No 。否则,打印Yes

例子 :

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

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

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

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

下面是上述方法的实现:

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


Javascript


输出:
Yes

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

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程