pugal
BAN USERimport java.util.LinkedList;
import java.util.List;
import java.util.Map.Entry;
import java.util.TreeMap;
public class RepeatSubString {
public static List<String> repeatSubString(String s, int n) {
TreeMap<String,Integer> repSubMap = new TreeMap<>();
for (int i=0; i+n-1<s.length(); ++i) {
String sub = s.substring(i,i+n);
if (repSubMap.containsKey(sub)) {
int count = repSubMap.get(sub);
count++;
repSubMap.put(sub, count);
} else {
repSubMap.put(sub,1);
}
}
List<String> list = new LinkedList<>();
for (Entry<String,Integer> kv : repSubMap.entrySet()) {
if (kv.getValue() > 1) {
list.add(kv.getKey());
}
}
return list;
}
public static void main(String[] args) {
System.out.println(repeatSubString("ABCACBABC", 3));
System.out.println(repeatSubString("ABCABCA", 2));
}
}
- pugal January 14, 2018