title | date | draft | tags | categories | |||
---|---|---|---|---|---|---|---|
算法4 Java解答 1.2.06 |
2019-02-22 07:35:22 +0800 |
false |
|
|
A string s is a circular rotation of a string t if it matches when the characters are circularly shifted by any number of positions; e.g., ACTGACG is a circular shift of TGACGAC, and vice versa. Detecting this condition is important in the study of genomic sequences. Write a program that checks whether two given strings s and t are circular shifts of one another. Hint : The solution is a one-liner with indexOf(), length(), and string concatenation.
public static boolean isCircular(String s, String t) {
return s.length() == t.length() && (s + s).indexOf(t) != -1;
}
public static void main(String[] args) {
String s = "ACTGACG";
String t = "TGACGAC";
StdOut.println(isCircular(s, t));
s = "123456789";
t = "912345678";
StdOut.println(isCircular(s, t));
}