Coding-Interview-101

Solutions to LeetCode problems filtered with companies, topics and difficulty.

View project on GitHub

Rotate String


Solution


    class Solution {
    public:
        bool rotateString(string A, string B) {
            if(A.length() != B.length())
                return false;
            if(A.length() == 0)
                return true;
            for(int i = 0; i < A.length(); i++) {
                if(A[i] == B[0]) {
                    int j = (i + 1) % A.length(), k = 1;
                    while(j != i) {
                        if(A[j] != B[k])
                            break;
                        j = (j + 1) % A.length();
                        k++;
                    }
                    if(j == i)
                        return true;
                }
            }
            return false;
        }
    };