is one String the rotation of another string - Java Algorithm

Java examples for Algorithm:String

Description

is one String the rotation of another string

Demo Code


public class Main {

  public static void main(String[] args) {
    System.out.println(isSubString("abc", "abc"));
    System.out.println(isSubString("abc", "cab"));
  }//from www  .  ja v a2s .  com

  public static boolean isSubString(String s1, String s2) {
    

    char[] s1ChrArr = s1.toCharArray();
    char[] s2ChrArr = s2.toCharArray();

    char base = s1ChrArr[0];

    for (int i = 0; i < s2ChrArr.length; i++) {

      if (base == s2ChrArr[i]) {
        
        boolean isRight = true;
        
        for (int j = 0; j < s1ChrArr.length; j++) {
          
          if (s1ChrArr[j] != s2ChrArr[(i + j) % s2ChrArr.length]) {
            
            isRight = false;
            break;
          }
        }
        
        if(isRight == true){
          return isRight;
        }

      }

      

    }
    
    return false;
  }

}

Related Tutorials