Tuesday, September 24, 2024
Google search engine
HomeLanguagesPython3 Program for Check if a string can be obtained by rotating...

Python3 Program for Check if a string can be obtained by rotating another string d places

Given two strings str1 and str2 and an integer d, the task is to check whether str2 can be obtained by rotating str1 by d places (either to the left or to the right).

Examples: 

Input: str1 = “abcdefg”, str2 = “cdefgab”, d = 2 
Output: Yes 
Rotate str1 2 places to the left.

Input: str1 = “abcdefg”, str2 = “cdfdawb”, d = 6 
Output: No 
 

Approach: An approach to solve the same problem has been discussed here. In this article, reversal algorithm is used to rotate the string to the left and to the right in O(n). If any one of the rotations of str1 is equal to str2 then print Yes else print No.

Below is the implementation of the above approach:  

Python3




# Python3 implementation of the approach
 
# Function to reverse an array from left
# index to right index (both inclusive)
def ReverseArray(arr, left, right) :
     
    while (left < right) :
        temp = arr[left];
        arr[left] = arr[right];
        arr[right] = temp;
        left += 1;
        right -= 1;
 
# Function that returns true if str1 can be
# made equal to str2 by rotating either
# d places to the left or to the right
def RotateAndCheck(str1, str2, d) :
 
    if (len(str1) != len(str2)) :
        return False;
 
    # Left Rotation string will contain
    # the string rotated Anti-Clockwise
    # Right Rotation string will contain
    # the string rotated Clockwise
    left_rot_str1 = []; right_rot_str1 = [];
    left_flag = True; right_flag = True;
    str1_size = len(str1);
 
    # Copying the str1 string to left rotation string
    # and right rotation string
    for i in range(str1_size) :
        left_rot_str1.append(str1[i]);
        right_rot_str1.append(str1[i]);
 
    # Rotating the string d positions to the left
    ReverseArray(left_rot_str1, 0, d - 1);
    ReverseArray(left_rot_str1, d, str1_size - 1);
    ReverseArray(left_rot_str1, 0, str1_size - 1);
 
    # Rotating the string d positions to the right
    ReverseArray(right_rot_str1, 0, str1_size - d - 1);
    ReverseArray(right_rot_str1,
                 str1_size - d, str1_size - 1);
    ReverseArray(right_rot_str1, 0, str1_size - 1);
 
    # Comparing the rotated strings
    for i in range(str1_size) :
 
        # If cannot be made equal with left rotation
        if (left_rot_str1[i] != str2[i]) :
            left_flag = False;
 
        # If cannot be made equal with right rotation
        if (right_rot_str1[i] != str2[i]) :
            right_flag = False;
 
    # If both or any one of the rotations
    # of str1 were equal to str2
    if (left_flag or right_flag) :
        return True;
    return False;
 
# Driver code
if __name__ == "__main__" :
 
    str1 = list("abcdefg");
    str2 = list("cdefgab");
 
    # d is the rotating factor
    d = 2;
 
    # In case length of str1 < d
    d = d % len(str1);
 
    if (RotateAndCheck(str1, str2, d)) :
        print("Yes");
    else :
        print("No");
 
# This code is contributed by AnkitRai01


Output: 

Yes

 

Time Complexity: O(n), where n represents the size of the given string.
Auxiliary Space: O(n), where n represents the size of the given string.

Please refer complete article on Check if a string can be obtained by rotating another string d places for more details!
 

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments