Write a program to compute if one string is a rotation of another. - Techno junkie

Breaking

Be the Techno giant.

Post Top Ad

Responsive Ads Here

Post Top Ad

Responsive Ads Here

Thursday, 27 September 2018

Write a program to compute if one string is a rotation of another.

public class StringRotation {
    public static boolean checkRotation(String st1, String st2) {
        if (st1.length() != st2.length()) {
            return false;
        }
        String st3 = st1 + st1;
        if (st3.contains(st2))
            return true;
        else
            return false;
    }
 
    public static void main(String[] args) {
        String str1 = "DAGGAD";
        String str2 = "GADDAG";
        System.out.println("Checking if a string is rotation of another");
        if (checkRotation(str1, str2)) {
            System.out.println("Yes " + str2 + " is rotation of " + str1);
        } else {
            System.out.println("No " + str2 + " is not rotation of " + str1);
        }
    }
}

Output:-



No comments:

Post a Comment

Post Top Ad

Responsive Ads Here