Created
May 5, 2018 03:38
-
-
Save jianminchen/7a7004891c123b9c3befac761803de30 to your computer and use it in GitHub Desktop.
Find minimum substring - Being interviewer - May 4 2018
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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