Python3 Program for Minimum Rotations Required to get the Same String


In this problem, we need to find the total number of rotations to get the same string. The naïve approach to solving the problem is that we can keep rotating the string. We can print the total number of required rotations if we find the same string.

Also, other approaches take the substring from the string and make it equal to the original string. After that, we can get rotations using the substring length.

Problem statement − We have given string str. We need to find the total number of rotations required to get the same string again.

Sample examples

Input

str = "abab"

Output

2

Explanation − When we make 2 left rotations of the string, we can get the same string.

  • In the first rotation, the string becomes ‘baba’.

  • In the second rotation, the string becomes ‘abab’ equal to the original string.

Input

 str = ‘aaaa’

Output

4

Explanation − As all rotations of string are the same, so we need total 4 rotations to get the original string.

Input

 str = ‘abcd’

Output

4

Explanation − As all rotations of the string are different, we need rotations equal to the length of the string.

Approach 1

In this approach, we will take a substring from the pth index to the end and start from the 0th index to the pth index. After that, we will merge both strings. If the resultant string is equal to the original string, we can say that the total rotations required are p to get the same string.

Algorithm

Step 1 − Initialize the ‘rotations’ variable with zero to store the total number of rotations.

Step 2 − Start traversing the string using the loop.

Step 3 − Take a substring starting from pth index. Also, take the substring from the 0th index to the pth index.

Step 4 − Merge both substrings.

Step 5 − If the merged string is equal to alpha, update the value of the rotations variable with p and break the loop.

Step 6 − If the value of ‘rotations’ is zero, return the length of the string. Otherwise, return the ‘rotations’.

Example

def totalRotations(alpha):
    rotations = 0  # to store total rotations
    length = len(alpha)  # string length
    # traverse the string
    for p in range(1, len(alpha) - 1):
        # if [p: length] + [0: p] is the same as [0: length], rotation found
        if (alpha[p: length] + alpha[0: p] == alpha):
            rotations = p
            break
#  If the string is not rotated at all, then return the length of the string
    if (rotations == 0):
        return length
    return rotations

if __name__ == '__main__':
    str = "abab"
    result = totalRotations(str)
    print("The number of rotations to get the same string are:", result)

Output

The number of rotations to get the same string are: 2

Time complexity− O(N^2) as we use loop to traverse the string and get the substring inside it.

Space complexity − O(1) as we don’t use dynamic space.

Approach 2

In this approach, we will take the rotational substring of length equal to N starting from the pth index. If it matches the original string, we can say that the minimum rotations required to achieve the original string are equal to the current index.

Algorithm

Step 1 − Merge the string with itself and store it in the ‘tmp’ variable.

Step 2 − Start traversing the ‘tmp’ string.

Step 3 − Get the rotational substring starting from the pth index, and the length equals the original string’s length.

Step 4 − If ‘str’ equals the ‘substr’, return p.

Step 5 − At last, return ‘len’.

Example

def totalRotations(str):
    # Merge string with itself
    tmp = str + str
    length = len(str)

    for p in range(1, length + 1):
        # get a substring of length equal to len and starting from the pth index of the tmp string
        substring = tmp[p: p+length]

        # If str and substring are equal, return p
        if str == substring:
            return p
    return len

if __name__ == '__main__':
    str = "efg"
    result = totalRotations(str)
    print("The number of rotations to get the same string are:", result)

Output

The number of rotations to get the same string are: 3

Time complexity− O(N^2) as we get rotational substring in the loop.

Space complexity − O(N) as we store merged string to ‘tmp’ variable.

We learned two approaches to get the total number of rotations for the original string. Both approaches get the substring of a specific length, merge them, and find the total number of required rotations.

Updated on: 14-Aug-2023

42 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements