【算法与数据结构】基因组序列的研究,验证字符串s和t是否是回环变位
假设String A = ACTCACG; String B = TGACGAC;
判断A和B是否互为回环。
【思路】
只需满足两个条件即可
1. 两个字符串的长度必须相等
2. 把A自己接在自己后面(A.concat(A))在这个字符串中找B,找到了就输出yes,否则输出no (indexOf(B)负责找字符串B,找不到返回-1)
【代码】
1 public class Main { 2 public static void circularRotation(String str1, String str2) { 3 if (str1.length() == str2.length() && str1.concat(str1).indexOf(str2) != -1) { 4 System.out.println("Yes, it is circular rotation!"); 5 }else { 6 System.out.println("No, it is not circular rotation!") 7 } 8 } 9 }
【注】str.concat(str1) //是在字符串str末尾串联str1。