本系列博客習(xí)題來自《算法(第四版)》,算是本人的讀書筆記,如果有人在讀這本書的,歡迎大家多多交流。為了方便討論,本人新建了一個(gè)微信群(算法交流),想要加入的,請(qǐng)?zhí)砑游业奈⑿盘?hào):zhujinhui207407 謝謝。另外,本人的個(gè)人博客 http://www.kyson.cn 也在不停的更新中,歡迎一起討論

知識(shí)點(diǎn)
- Java中的字符串
- 回環(huán)變位
題目
1.2.6 如果字符串 s 中的字符循環(huán)移動(dòng)任意位置之后能夠得到另一個(gè)字符串 t,那么 s 就被稱為 t 的回環(huán)變位(circular rotation)。例如,ACTGACG 就是 TGACGAC 的一個(gè)回環(huán)變位,反之亦然。判定這個(gè)條件在基因組序列的研究中是很重要的。編寫一個(gè)程序檢查兩個(gè)給定的字符串 s 和 t 是否互為回環(huán)變位。提示:答案只需要一行用到 indexOf()、length() 和字符串連接的代碼。
1.2.6 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
答案
public static boolean isCircularRotation(String s, String t) {
return s.length() == t.length() && (t + t).contains(s);
}
代碼索引
視頻講解
廣告
我的首款個(gè)人開發(fā)的APP壁紙寶貝上線了,歡迎大家下載。