How to check if second string a rotated version of first string in Java

1 Answer

0 votes
public class Program {
    static boolean isSecondStringRotatedOfFirstString(final String str1, final String str2) {
    	if (str1.length() != str2.length()) {
    		return false;
    	}
    
    	String concatenated = str1 + str1;
    
    	return concatenated.indexOf(str2) != -1;
    }

    
    public static void main(String[] args) {
        String first = "abcdefg";
	    String second = "cdefgab";

	    System.out.print((isSecondStringRotatedOfFirstString(first, second) ? "yes" : "no"));
        
    }
}




/*
run:

yes
 
*/

 



answered Mar 25, 2024 by avibootz

Related questions

...