Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jianminchen/7a7004891c123b9c3befac761803de30 to your computer and use it in GitHub Desktop.
Save jianminchen/7a7004891c123b9c3befac761803de30 to your computer and use it in GitHub Desktop.
Find minimum substring - Being interviewer - May 4 2018
import java.io.*;
import java.util.*;
class Solution {
/*
Input: xyyzyzyx
All substring O(n*n) substrings * check each subtring O(m) = O(n*n*m):
x y y z y z y x
xy yy yz zy yz zy yx
xyy yyz yzy zyz yzz zyy
Max of O(n*n+n*m)
*/
static String getShortestUniqueSubstring(char[] arr, String str) {
if(arr.length == 0) {
return "";
}
// your code goes here
Set<Character> inp = new HashSet<Character>();
for(char ch : arr) {
inp.add(ch);
}
String minString = "";
for(int i = 0; i < str.length() - arr.length; i++) {
Set<Character> check = new HashSet<>();
check.addAll(inp);
for(int j = i; j < str.length(); ++j) {
check.remove(str.charAt(j));
if(check.isEmpty()) {
String cand = str.substring(i, j+1);
if(cand.length() < minString.length()) {
minString = cand;
}
break;
}
}
}
return minString;
}
public static void main(String[] args) {
}
}
/*
AAAAABC
| | -> "AAAAABC"
||||
ABC
n^n
using previous knowledge
ABABC
----->
---->
O(n)
ABC
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment