forked from HarshCasper/NeoAlgo
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMinimumWindowSubstring.java
113 lines (91 loc) · 3.51 KB
/
MinimumWindowSubstring.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/**
* Minimum Window Substring
* OR
* Smallest Window in a String Containing All Characters of Another String
* Given two strings string1 and string2, find the smallest substring in
* string1 containing all characters of string2.
*/
import java.io.*;
public class MinimumWindowSubstring {
static final int CHAR_COUNT = 26;
//Checking if all characters in pattern are contained in substring
//or not
public static boolean isValid(int[] str, int[] pattern) {
for(int i=0; i<CHAR_COUNT; i++) {
if(str[i] < pattern[i])
return false;
}
return true;
}
public static String findSmallestSubstring(String str, String pattern) {
//To store count of characters in str and pattern, respectively
int strCount[] = new int[CHAR_COUNT];
int ptrCount[] = new int[CHAR_COUNT];
//Counting number of occurrences of each character in pattern
for(int i=0; i<pattern.length(); i++)
ptrCount[pattern.charAt(i) - 'a']++;
int end, start;
end = start = 0;
String result = "-1"; //If no valid substring is found, "-1" is returned
//start and end are pointers for start and end of substring
while(end < str.length()) {
//When substring does not contain all characters of pattern, we
//expand substring by adding characters from the end
if(!isValid(strCount, ptrCount)) {
strCount[str.charAt(end) - 'a']++;
end++;
continue;
}
//Updating substring contained in result
if(result.equals("-1") || result.length() > (end - start))
result = str.substring(start, end);
//To get length of smallest valid substring, we shorten the substring
//from front (i.e., eliminate characters from start)
strCount[str.charAt(start) - 'a']--;
start++;
}
//For the substrings which end at (str.length() - 1)
while(isValid(strCount, ptrCount)) {
if(result.equals("-1") || result.length() > (end - start))
result = str.substring(start, end);
strCount[str.charAt(start) - 'a']--;
start++;
}
return result;
}
public static void main(String[] args) throws IOException {
InputStreamReader read = new InputStreamReader(System.in);
BufferedReader buf = new BufferedReader(read);
//Taking input from user
System.out.println("Enter a String text: ");
String str = buf.readLine().trim();
System.out.println("Enter a String pattern: ");
String pattern = buf.readLine().trim();
//Printing output
System.out.println("Smallest substring in text, containing all " +
"characters in pattern: ");
System.out.println(findSmallestSubstring(str, pattern));
}
}
/*
Time complexity: O(N) (considering that isValid() method works in constant
time as it makes a fixed number of comparisons)
Space complexity: O(CHAR_COUNT)
TEST CASES
INPUT
Enter a String text:
zaaskzaa
Enter a String pattern:
zsk
OUTPUT
Smallest substring in text, containing all characters in pattern:
skz
INPUT
Enter a String text:
pompandpageantrygalore
Enter a String pattern:
gtyn
OUTPUT
Smallest substring in text, containing all characters in pattern:
ntryg
*/