Rotate String

Question

We are given two strings, A and B.

A shift on A consists of taking string A and moving the leftmost character to the rightmost position. For example, if A = 'abcde', then it will be 'bcdea' after one shift on A. Return True if and only if A can become B after some number of shifts on A.

Example 1:

Input: A = 'abcde', B = 'cdeab'
Output: true

Example 2:

Input: A = 'abcde', B = 'abced'
Output: false

Note:
A and B will have length at most 100.

Solution

Approach 1:

class Solution {
    public boolean rotateString(String A, String B) {

// if the input string is already same as output string return true.
        if(A.equals(B)){
            return true;
        }
// if the length of input string is not same as the length of output string
// the output string can never be the rotation of input string, return false.

        if(A.length() != B.length()){
            return false;
        }
// concatenate the input string A with itself using special '+' character.The
// concatenated string will contain all possible left rotation of the string A.
        String C = A + A;

// If the concatenated string C contains string B as a substring then string B
// is the rotation of string A, return true, otherwise return false.
        if(C.contains(B)){
            return true;
        }
        return false;
    }
}

Approach 2:

class Solution {
 public boolean rotateString(String A, String B) {
  char[] charArrayA = A.toCharArray();
  if (A.equals(B)) {
   return true;
  }

  if(A.length() != B.length()){
      return false;
  }

  for (int r = 0; r < (charArrayA.length); r++) {

   A = new String(charArrayA);
   char firstChar = charArrayA[0];
   int i = 0;

   if (A.equals(B)) {
    return true;
   }

   while (i < (charArrayA.length - 1)) {

    charArrayA[i] = charArrayA[i + 1];
    i++;

   }
   charArrayA[i] = firstChar;
  }
  return false;
 }
}

Test Cases

  1. String B is rotation of String A of odd length, with center index. for eg. A=‘aloha’ and B= ‘ohaal’.
  2. String B is rotation of String A of even length, with begin index(String B is same as String A). for eg. A=‘abba’ and B= ‘abba’.
  3. String B is rotation of String A, with end index. for eg. A=‘aloha’ and B= ‘aaloh’.
  4. String A and B are of single character and are rotation of each other. for eg. A= ‘a’ and B= ‘a’.
  5. String B is rotation of String A. Strings contains both uppercase and lowercase of same letter. for eg. A= ‘alohA’ and B= ‘Aaloh’.
  6. String B is not a rotation of String A. Strings contains both uppercase and lowercase of same letter. for eg. A= ‘alohA’ and B= ‘aaloh’.
  7. String B is not a rotation of String A. for eg. A=‘hello’ and B= ‘llheo’ .
  8. String B is rotation of String A and is of maximum length =100.
  9. String B is not a rotation of String A and is of maximum length =100.
  10. String B is rotation of String A and contains special characters. for eg. A= ‘你好%&&’ and B= ‘%&&你好’.
  11. String B length is more than String A length. for eg. A=‘hell’ and B= ‘lleho’.
  12. String B length is less than String A length. for eg. A=‘hello’ and B= ‘lleh’.
  13. String A and B are empty.for eg. A=” and B= “.
  14. String A is empty and String B is null.This will throw an exception which is not handled in this code(negative testing).
  15. String A and B are null.This will throw an exception which is not handled in this code(negative testing).
  16. String A id null and B is empty.This will throw an exception which is not handled in this code(negative testing).

See also