-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCountingSubstring.java
More file actions
27 lines (25 loc) · 1.06 KB
/
Copy pathCountingSubstring.java
File metadata and controls
27 lines (25 loc) · 1.06 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
import java.util.Scanner;
import java.util.TreeMap;
public class CountingSubstring {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String string = scanner.next();
String prefix = scanner.next();
String suffix = scanner.next();
TreeMap<String, Integer> treeMap = new TreeMap<String, Integer>();
int indexOfPrefix = string.indexOf(prefix),
indexOfSuffix = string.indexOf(suffix);
while (indexOfPrefix != -1) {
if (indexOfSuffix >= indexOfPrefix) {
String subString = string.substring(indexOfPrefix, indexOfSuffix + suffix.length());
if (subString.length() >= (Math.max(prefix.length(), suffix.length())))
treeMap.put(subString, 0);
}
else if (indexOfSuffix == -1) {
indexOfPrefix = string.indexOf(prefix, indexOfPrefix + 1);
}
indexOfSuffix = string.indexOf(suffix, indexOfSuffix + 1);
}
System.out.println(treeMap.size());
}
}