📌  相关文章
📜  检查任何字符串的左移和右移结果是否为给定的字符串

📅  最后修改于: 2021-09-05 12:00:12             🧑  作者: Mango

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

方法:

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

下面是上述方法的实现:

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


Javascript


输出:
Yes

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

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live