Python Program to Determine Whether one String is a Rotation of Another

In the previous article, we have discussed Python Program to Shuffle a List
Given a string, and the task is to determine whether one string is a rotation of another.

Examples:

Example1:

Input:

Given first string = "btechgeeks"
Given second string = "geeksbtech"

Output:

The given second string is the rotation of the given second string

Example2:

Input:

Given first string = "pqrst"
Given second string = "sggsf"

Output:

The given second string is not the rotation of the given second string

Program to Determine Whether one String is a Rotation of Another

Below are the ways to determine whether one string is the rotation of another.

Method #1: Using String Concatenation (Static Input)

Approach:

  • Give the first string as static input and store it in a variable.
  • Give the second string as static input and store it in another variable.
  • Check if the length of the first string is not equal to the length of the second string using the if conditional statement and using the len() function.
  • If the statement is true, print “The given second string is not the rotation of the given first string”.
  • Else concatenate the first string with the first string itself using the ‘+ ‘ operator and store it in a variable say “conat_str”.
  • Check if the second string is present in the “conca_str” using the if conditional statement.
  • If the statement is true, print  “The given second string is the rotation of the given first string”.
  • Else print  “The given second string is not the rotation of the given first string”.
  • The Exit of the Program.

Below is the implementation:

# Give the first string as static input and store it in a variable.
fst_str = "pqrst"
# Give the second string as static input and store it in another variable.
secnd_str = "stpqr"
# Check if the length of the first string is not equal to the length of the second
# string using the if conditional statement and using the len() function.
if(len(fst_str) != len(secnd_str)):
    # If the statement is true, print "The given second string is not the rotation of the given first string".
    print("The given second string is not the rotation of the given first string")
else:
    # Else concatenate the first string with the first string itself using the '+ ' operator
    # and store it in a variable say "conat_str".
    conct_str = fst_str + fst_str
# Check if the second string is present in the "conca_str" using the if
# conditional statement.
    if(secnd_str in conct_str):
        # If the statement is true, print  "The given second string is the rotation of the given first string".
        print("The given second string is the rotation of the given  first string")
    else:
        # Else print  "The given second string is not the rotation of the given first string".
        print("The given second string is not the rotation of the first given string")

Output:

The given second string is the rotation of the second string

Method #2: Using String Concatenation (User Input)

Approach:

  • Give the first string as user input using the input() function and store it in a variable.
  • Give the second string as user input using the input() function and store it in another variable.
  • Check if the length of the first string is not equal to the length of the second string using the if conditional statement and using the len() function.
  • If the statement is true, print “The given second string is not the rotation of the given first string”.
  • Else concatenate the first string with the first string itself using the ‘+ ‘ operator and store it in a variable say “conat_str”.
  • Check if the second string is present in the “conca_str” using the if conditional statement.
  • If the statement is true, print  “The given second string is the rotation of the given first string”.
  • Else print  “The given second string is not the rotation of the given first string”.
  • The Exit of the Program.

Below is the implementation:

# Give the first string as user input using input() function and store it in a variable.
fst_str = input("Enter some random string = ")
# Give the second string as user input using input() function and store it in another variable.
secnd_str = input("Enter some random string = ")
# Check if the length of the first string is not equal to the length of the second
# string using the if conditional statement and using the len() function.
if(len(fst_str) != len(secnd_str)):
    # If the statement is true, print "The given second string is not the rotation of the given first string".
    print("The given second string is not the rotation of the given first string")
else:
    # Else concat the first string with the first string itself using the '+ ' operator
    # and store it in a variable say "conat_str".
    conct_str = fst_str + fst_str
# Check if the second string is present in the "conca_str" using the if
# conditional statement.
    if(secnd_str in conct_str):
        # If the statement is true, print  "The given second string is the rotation of the given first string".
        print("The given second string is the rotation of the given first string")
    else:
        # Else print  "The given second string is not the rotation of the given first string".
        print("The given second string is not the rotation of the given first string")

Output:

Enter some random string = btechgeeks
Enter some random string = geeksbtech
The given second string is the rotation of the second string

Explore more instances related to python concepts from Python Programming Examples Guide and get promoted from beginner to professional programmer level in Python Programming Language.