Rotate String

Solution For Rotate String

Rotate String is a problem on LeetCode where you are given two strings A and B, and you need to check if it is possible to obtain string B by rotating string A by any number of positions clockwise.

To solve this problem, we can use the concept of string concatenation and substring. First, we need to check if the lengths of the two strings A and B are equal. If not, then it is not possible to obtain string B by rotating string A.

If the lengths of the two strings are equal, we can concatenate string A with itself and check if string B is a substring of the concatenated string. If yes, then it is possible to obtain string B by rotating string A.

The reason we concatenate string A with itself is that rotating string A is equivalent to taking a substring of the concatenated string. For example, if we have string A as “abcdef” and we rotate it by 3 positions, we get “defabc”. This is equivalent to taking a substring of the concatenated string “abcdefabcdef” starting from index 3 and ending at index 8.

Here’s the detailed solution in Python:

class Solution:
def rotateString(self, A: str, B: str) -> bool:
if len(A) != len(B):
return False
else:
# concatenate string A with itself
concat_A = A + A
# check if string B is a substring of the concatenated string
if B in concat_A:
return True
else:
return False

The time complexity of this solution is O(n^2), where n is the length of string A. This is because the in operator has a time complexity of O(n), and we are using it inside a loop. However, the worst-case scenario for the in operator is when the two strings are different, in which case it will return False immediately, leading to a time complexity of O(1).

Step by Step Implementation For Rotate String

class Solution {
    public boolean rotateString(String A, String B) {
        if(A.length()!=B.length())
            return false;
        if(A.length()==0)
            return true;
        int start = 0;
        for(int i=0;i
class Solution:
    def rotateString(self, A: str, B: str) -> bool:
        if len(A) != len(B):
            return False
        if A == B:
            return True
        for i in range(len(A)):
            if A[i:] + A[:i] == B:
                return True
        return False
function rotateString(str, k) {
    // edge case
    if (str.length === 0) {
        return str;
    }
    
    // make sure k is positive
    k = k % str.length;
    if (k < 0) {
        k = k + str.length;
    }
    
    // do the rotation
    return str.substr(str.length - k) + str.substr(0, str.length - k);
}
We can rotate a string by moving its first character to the end and appending it to the string. We can keep doing this until the string has been rotated the desired number of times.

void rotateString(string &s, int k) { // Handle the case where k is greater than the length of the string if (k > s.length()) k = k % s.length(); // Rotate the string k times for (int i = 0; i < k; i++) { // Move the first character to the end char c = s[0]; for (int j = 0; j < s.length() - 1; j++) s[j] = s[j + 1]; s[s.length() - 1] = c; } }
public class Solution {
    public bool RotateString(string A, string B) {
        //if the lengths of the two strings are not equal, then they cannot be rotated versions of each other
        if(A.Length != B.Length){
            return false;
        }
        //if the lengths are equal but the strings are empty, then they are rotated versions of each other
        if(A.Length == 0){
            return true;
        }
        //find the starting index of string B in string A
        int startIndex = 0;
        for(int i = 0; i < A.Length; i++){
            if(A[i] == B[0]){
                startIndex = i;
                break;
            }
        }
        //rotate string A so that the starting index of string B in string A is at index 0
        string ARotated = A.Substring(startIndex) + A.Substring(0, startIndex);
        //compare the rotated string to string B
        return ARotated == B;
    }
}


Scroll to Top

Top 100 Leetcode Practice Problems In Java

Get 30% Off Instantly!
[gravityforms id="5" description="false" titla="false" ajax="true"]