- Notifications
You must be signed in to change notification settings - Fork 88
Expand file tree
/
Copy pathRepeatingStringOfSpecificLength.java
More file actions
Latest commit
46 lines (42 loc) · 1.62 KB
/
Copy pathRepeatingStringOfSpecificLength.java
File metadata and controls
46 lines (42 loc) · 1.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
packagestrings.repeatingstringsofspecifiedlength;
importjava.util.HashSet;
importjava.util.Set;
importjava.util.TreeSet;
/**
* Identify all the 'n' (n will be input) letter-long sequences that occur more than once in
* any given input string. Write a program that prints out all such sequences to the standard output stream,
* sorted in alphabetical order.
*
* This question can be twisted and asked as: "Find repeating sequences of specified length in given dna
* chromosome sequence."
*
* Created by techpanja
* Created on 1/21/14 3:37 PM.
*/
publicclassRepeatingStringOfSpecificLength {
publicstaticvoidprintRepeatingStrings(StringinputString, intsequenceLength) {
if (inputString.isEmpty() || sequenceLength <= 0 || sequenceLength >= inputString.length()) {
System.out.println("Invalid input");
} else {
inti = 0;
intj = i + sequenceLength;
Set<String> tempSet = newHashSet<>();
Set<String> repeatingSequences = newTreeSet<>();
while (j <= inputString.length()) {
if (!tempSet.add(inputString.substring(i, j))) {
repeatingSequences.add(inputString.substring(i, j));
}
i++;
j = i + sequenceLength;
}
for (Stringstr : repeatingSequences) {
System.out.println(str);
}
}
}
publicstaticvoidmain(String[] args) {
printRepeatingStrings("ABABC", 2);
printRepeatingStrings("ABABBABBZEDZEDZE", 3);
printRepeatingStrings("AAGATCCGTCCCCCCAAGATCCGTC", 10);
}
}