Skip to content

Latest commit

 

History

History
39 lines (29 loc) · 1.09 KB

Ex_1_2_06.md

File metadata and controls

39 lines (29 loc) · 1.09 KB
title date draft tags categories
算法4 Java解答 1.2.06
2019-02-22 07:35:22 +0800
false
JAVA
技术
归档

1.2.06

问题:

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));
  }

参考:

YangXiaoHei