here is the code....
Problem - "ravgau" is a rotation of "gaurav".
public class Demo {
public static void main(String[] args) {
String s1 = "gaurav";
String s2 = "avgaur";
if (isSubString(s1 + s1, s2))
System.out.print("String :" + s2 + " is a rotation of string :" + s1);
else
System.out.print("String :" + s2 + " is not a rotation of string :" + s1);
}
public static boolean isSubString(String s1, String s2) {
int k = 0;
int i = 0;
int t;
if (s1.length() < s2.length()) {
return false;
} else {
while (i < (s1.length() - s2.length() + 1)) {
if (s1.charAt(i) == s2.charAt(k)) {
t = i;
t++;
k++;
// Always make sure that conditions in loop should appear in
// correct order otherwise it could give an exception
while ((k < s2.length()) && (s1.charAt(t) == s2.charAt(k))) {
t++;
k++;
}
if (k == s2.length())
return true;
else
k = 0;
}
i++;
}
}
return false;
}
}
No comments:
Post a Comment