forked from ashoklathwal/Code-for-Interview
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsmallestSubStringOfAllChar.java
75 lines (63 loc) · 2.36 KB
/
smallestSubStringOfAllChar.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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/*
/*
Given an array of unique characters arr and a string str, Implement a function getShortestUniqueSubstring
that finds the smallest substring of str containing all the characters in arr. Return "" (empty string)
if such a substring doesn’t exist.
Come up with an asymptotically optimal solution and analyze the time and space complexities.
Example:
input: arr = ['x','y','z'], str = "xyyzyzyx"
output: "zyx"
Time Complexity: we’re doing a linear iteration of both str and arr of lengths N and M respectively,
so the runtime complexity is linear in the size of the input, i.e. O(N+M).
Space Complexity: we’re using a Map/Hash Table countMap with M key/values pairs plus few
constant size counters, which together give us an O(M) space complexity (linear in the size of arr).
*/
*/
import java.util.HashMap;
class smallestSubStringOfAllChar
{
public static String smallestSubStringOfAllChar_util(char[] arr, String str)
{
HashMap<Character, Integer> hm = new HashMap<>();
int uniquecount = 0;
int headIndex = 0;
for(int i=0;i<arr.length;i++)
hm.put(arr[i], 0);
for(int tailIndex=0;tailIndex<str.length();tailIndex++)
{
if(!hm.containsKey(str.charAt(tailIndex)))
continue;
if(hm.get(str.charAt(tailIndex)) == 0)
{
hm.put(str.charAt(tailIndex), 1);
uniquecount++;
}
else
{
hm.put(str.charAt(tailIndex), hm.get(str.charAt(tailIndex))+1);
}
while(uniquecount == arr.length)
{
int templength = tailIndex - headIndex + 1;
if(templength == arr.length)
return str.substring(headIndex, tailIndex+1);
char headchar = str.charAt(headIndex);
if(hm.containsKey(headchar))
{
int headcount = hm.get(headchar) - 1;
if(headcount == 0)
uniquecount--;
hm.put(headchar, headcount);
}
headIndex++;
}
}
return "";
}
public static void main(String[] args)
{
char[] arr = {'x','y','z'};
String str = "xyzyzyxzy";
System.out.println(" smallest substring of str containing all the characters in arr : "+smallestSubStringOfAllChar_util(arr, str));
}
}