📌  相关文章
📜  检查是否将任何字符串左右移动到给定的字符串

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

给定的字符串S仅包含小写英文字母。任务是寻找是否存在具有左移与右移都等于字符串S.如果存在任何字符串,然后打印是任意的字符串,否则打印号

例子:

方法:

  1. 主要目标是检查任何字符串等于给定字符串的左移和右移。
  2. 为此,我们只需要检查指定字符串的每个字符等于其旁边的下一个字符或不(即在字符(i)位置必须位于(i + 2)位等于字符)。
  3. 如果对于给定字符串上的每个位置都是正确的,那么我们可以说存在任何字符串,其左移和右移等于给定字符串,否则不相等。

下面是上述方法的实现:

C++
// C++ program to check if left
// and right shift of any string
// results into the given string
  
#include 
using namespace std;
  
// Function to check string exist
// or not as per above approach
void check_string_exist(string S)
{
    int size = S.length();
  
    bool check = true;
  
    for (int i = 0; i < size; i++) {
  
        // Check if any character
        // at position i and i+2
        // are not equal
        // then string doesnot exist
        if (S[i] != S[(i + 2) % size]) {
            check = false;
            break;
        }
    }
  
    if (check)
        cout << "Yes" << endl;
    else
        cout << "No" << endl;
}
  
// Driver code
int main()
{
    string S = "papa";
  
    check_string_exist(S);
  
    return 0;
}


Java
// Java program to check if left 
// and right shift of any string 
// results into the given string 
class GFG{
      
// Function to check string exist 
// or not as per above approach 
public static void check_string_exist(String S) 
{ 
    int size = S.length(); 
    boolean check = true; 
      
    for(int i = 0; i < size; i++)
    { 
         
       // Check if any character 
       // at position i and i+2 
       // are not equal 
       // then string doesnot exist 
       if (S.charAt(i) != S.charAt((i + 2) % size))
       { 
           check = false; 
           break; 
       } 
    } 
      
    if (check) 
        System.out.println("Yes");
    else
        System.out.println("No");
} 
  
// Driver Code
public static void main(String[] args)
{
    String S = "papa"; 
      
    check_string_exist(S); 
}
}
  
// This code is contributed by divyeshrabadiya07


Python3
# Python3 program to check if left 
# and right shift of any string 
# results into the given string 
  
# Function to check string exist 
# or not as per above approach 
def check_string_exist(S):
      
    size = len(S)
    check = True
      
    for i in range(size):
          
        # Check if any character 
        # at position i and i+2 
        # are not equal, then  
        # string doesnot exist 
        if S[i] != S[(i + 2) % size]:
            check = False
            break
          
    if check :
        print("Yes")
    else:
        print("No")
  
# Driver Code
S = "papa"
  
check_string_exist(S)
  
# This code is contributed by divyeshrabadiya07


C#
// C# program to check if left 
// and right shift of any string 
// results into the given string 
using System;
  
class GFG{
      
// Function to check string exist 
// or not as per above approach 
public static void check_string_exist(String S) 
{ 
    int size = S.Length; 
    bool check = true; 
      
    for(int i = 0; i < size; i++)
    { 
          
       // Check if any character 
       // at position i and i+2 
       // are not equal 
       // then string doesnot exist 
       if (S[i] != S[(i + 2) % size])
       { 
           check = false; 
           break; 
       } 
    } 
    if (check) 
        Console.WriteLine("Yes");
    else
        Console.WriteLine("No");
} 
  
// Driver Code
public static void Main(String[] args)
{
    String S = "papa"; 
      
    check_string_exist(S); 
}
}
  
// This code is contributed by sapnasingh4991


输出:
Yes

时间复杂度: O(N) ,其中N是字符串S的大小。

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