Advertisement
Guest User

Untitled

a guest
Feb 6th, 2015
243
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.58 KB | None | 0 0
  1.     public boolean checkStringRotation(String string1, String string2) {
  2.  
  3.         /* Length of string1 */
  4.         int n = string1.length();
  5.         /* Length of the strings are not same - Not a rotation */
  6.         if (n != string2.length())
  7.             return false;
  8.  
  9.         int i = 0;
  10.         int j = 0;
  11.         while (i < (n - 1) && j < (n - 1)) {
  12.             int k = 1;
  13.             while (k <= n && string1.charAt((i + k) % n) == string2.charAt((j + k) % n))
  14.                 k++;
  15.            
  16.             if (k > n)             
  17.                 return true;
  18.            
  19.             if (string1.charAt((i + k) % n) > string2.charAt((j + k) % n))
  20.                 i += k;
  21.            
  22.             else
  23.                 j += k;
  24.         }
  25.         return false;
  26.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement