给出一个非空的字符串,判断这个字符串是否是由它的一个子串进行多次首尾拼接构成的。
例如,"abcabcabc"满足条件,因为它是由"abc"首尾拼接而成的,而"abcab"则不满足条件。
import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.IOException; public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String str = br.readLine().trim(); int n = str.length(); String res = "false"; for(int end = 1; end <= n / 2 + 1; end++){ int times = n / end; // 重复次数 if(times * end == n && str.equals(repeat(str.substring(0, end), times))){ res = str.substring(0, end); } } System.out.println(res); } private static String repeat(String s, int times) { StringBuilder sb = new StringBuilder(); for(int i = 0; i < times; i++){ sb.append(s); } return sb.toString(); } }