Below is the example for finding the minimum window subString from Java String........
public class MinimumWindow {
public static void main(String args[]) {
Vector<Character> set = new Vector<Character>();
set.add('t');
set.add('i');
set.add('s');
set.add('t');
String x = minWindow("Hi, this is a test string ", set);
System.out.println(x);
}
public static String minWindow(String s, Vector<Character> charset) {
int n = charset.size();
int startindex = 0;
int endindex = n - 1;
Map<Character, Integer> CharSet = new HashMap<Character, Integer>();
for (Character c : charset) {
if (CharSet.containsKey(c)) {
int val = CharSet.get(c);
CharSet.put(c, val + 1);
} else
CharSet.put(c, 1);
}
int end = 0, start = 0;
for (; end < s.length() && n > 0; ++end) {
if (CharSet.containsKey(s.charAt(end))) {
int val = CharSet.get(s.charAt(end));
CharSet.put(s.charAt(end), val - 1);
if (val - 1 >= 0)
--n;
}
}
while (!CharSet.containsKey(s.charAt(start)))
++start;
int min_length = end - start + 1;
startindex = start;
endindex = end - 1;
System.out.println(startindex + " " + endindex);
for (; end < s.length(); ++end) {
if (CharSet.containsKey(s.charAt(end))) {
int val = CharSet.get(s.charAt(end));
CharSet.put(s.charAt(end), val - 1);
// System.out.println("E"+start+" "+end);
while (!CharSet.containsKey(s.charAt(start))
|| CharSet.get(s.charAt(start)) < 0) {
if (CharSet.containsKey(s.charAt(start))
&& CharSet.get(s.charAt(start)) < 0) {
val = CharSet.get(s.charAt(start));
CharSet.put(s.charAt(start), val + 1);
}
++start;
}
int currentLength = end - start + 1;
if (currentLength < min_length) {
min_length = currentLength;
startindex = start;
endindex = end;
}
System.out.println("S----"+start+" "+end);
}
}
System.out.println(startindex + " " + endindex);
return s.substring(startindex, endindex + 1);
}
}
Below is the output.....
1 14
S----4 16
S----9 17
S----9 19
S----9 20
S----17 22
17 22
t stri
public class MinimumWindow {
public static void main(String args[]) {
Vector<Character> set = new Vector<Character>();
set.add('t');
set.add('i');
set.add('s');
set.add('t');
String x = minWindow("Hi, this is a test string ", set);
System.out.println(x);
}
public static String minWindow(String s, Vector<Character> charset) {
int n = charset.size();
int startindex = 0;
int endindex = n - 1;
Map<Character, Integer> CharSet = new HashMap<Character, Integer>();
for (Character c : charset) {
if (CharSet.containsKey(c)) {
int val = CharSet.get(c);
CharSet.put(c, val + 1);
} else
CharSet.put(c, 1);
}
int end = 0, start = 0;
for (; end < s.length() && n > 0; ++end) {
if (CharSet.containsKey(s.charAt(end))) {
int val = CharSet.get(s.charAt(end));
CharSet.put(s.charAt(end), val - 1);
if (val - 1 >= 0)
--n;
}
}
while (!CharSet.containsKey(s.charAt(start)))
++start;
int min_length = end - start + 1;
startindex = start;
endindex = end - 1;
System.out.println(startindex + " " + endindex);
for (; end < s.length(); ++end) {
if (CharSet.containsKey(s.charAt(end))) {
int val = CharSet.get(s.charAt(end));
CharSet.put(s.charAt(end), val - 1);
// System.out.println("E"+start+" "+end);
while (!CharSet.containsKey(s.charAt(start))
|| CharSet.get(s.charAt(start)) < 0) {
if (CharSet.containsKey(s.charAt(start))
&& CharSet.get(s.charAt(start)) < 0) {
val = CharSet.get(s.charAt(start));
CharSet.put(s.charAt(start), val + 1);
}
++start;
}
int currentLength = end - start + 1;
if (currentLength < min_length) {
min_length = currentLength;
startindex = start;
endindex = end;
}
System.out.println("S----"+start+" "+end);
}
}
System.out.println(startindex + " " + endindex);
return s.substring(startindex, endindex + 1);
}
}
Below is the output.....
1 14
S----4 16
S----9 17
S----9 19
S----9 20
S----17 22
17 22
t stri
No comments:
Post a Comment