forked from oleg-cherednik/DailyCodingProblem
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.java
28 lines (22 loc) · 806 Bytes
/
Solution.java
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
/**
* @author Oleg Cherednik
* @since 19.01.2019
*/
public class Solution {
public static void main(String... args) {
System.out.println(getShortestSubstring("figehaeci", "aei"));
}
private static String getShortestSubstring(String str, String test) {
String res = null;
for (int i = 0, j = i; i < str.length() - test.length(); i++, j = i) {
if (test.indexOf(str.charAt(i)) == -1)
continue;
for (int k = 0, pos; k < test.length(); k++)
if (test.charAt(k) != str.charAt(i) && (pos = str.indexOf(test.charAt(k), i)) != -1)
j = Math.max(j, pos);
if (res == null || res.length() > j + 1 - i)
res = str.substring(i, j + 1);
}
return res;
}
}