Sunday, March 6, 2022

A Program to check if strings are rotations of each other or not

 

Program - 

public class CheckRotation{

     public static void main(String []args){
        
        String str1 = "ABCD";
        String str2 = "CDAB";
        
        if(str1.length() != str2.length())
        {
            System.out.println("Not a Rotation");
            System.exit(0);
        }
        
        String join = str1 + str1;
        
        if(join.indexOf(str2) != -1)
            System.out.println("Rotation");
        else
            System.out.println("Not a Rotation");
     }
 
 

Video explanation -

 


 

 

No comments:

Post a Comment