import java.util.*;
public class SmallSubString {
static final int MAX_CHARS_COUNT = 256;
static int maxDistChar(String str, int n)
{
int count[] = new int[MAX_CHARS_COUNT];
int max_distinct = 0;
for (int i = 0; i < n; i++) {
count[str.charAt(i)]++;
if (count[str.charAt(i)] == 1)
max_distinct++;
}
return max_distinct;
}
static int smallestSubString(String str)
{
int n = str.length();
int unique = maxDistChar(str, n);
int res = Integer.MAX_VALUE;
Map<Character, Integer> mp = new HashMap<Character, Integer>();
int j = 0;
for (int i = 0; i < str.length(); i++) {
char c = str.charAt(i);
if (mp.containsKey(c))
mp.put(c, mp.get(c) + 1);
else
mp.put(c, 1);
while (mp.size() == unique
&& mp.get(str.charAt(j)) > 1) {
mp.put(str.charAt(j),
(mp.get(str.charAt(j)) - 1));
j++;
}
if (mp.size() == unique)
res = Math.min(i - j + 1, res);
}
return res;
}
static public void main(String[] args)
{
String str = "AABBBCBB";
int len = smallestSubString(str);
System.out.println(
" The length of the smallest substring characters "+ len);
}
}
TechInsiderStory is place where you can find basic knowledge of various technology and framework. TechInsiderStory is provide free knowledge on various technology and framework.
Smallest Sub String
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment