public class LongDiffString {
public int find(char[] s) {
HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
int begin = 0;
int end = 0;
int lmax = 0;
int maxBegin = 0;
int maxEnd = 0;
for(int i=0; i<s.length; i++){
boolean initBegin = false;
if(map.get((int)s[i]) != null){
int p = map.get((int)s[i]);
if(p >= begin){
end = i;
initBegin = true;
}else{
end++;
}
}else{
end++;
}
if(end-begin > lmax){
lmax = end-begin;
maxBegin = begin;
maxEnd = end;
}
if(initBegin){
begin = map.get((int)s[i])+1;
end++;
}
map.put((int)s[i], i);
}
for(int i=maxBegin; i<maxEnd; i++){
System.out.print(s[i] + " ");
}
System.out.println();
return lmax;
}
/**
* @param args
*/
public static void main(String[] args) {
char[] s = "magoodboye".toCharArray();
// char[] s = "iam".toCharArray();
LongDiffString lds = new LongDiffString();
int x = lds.find(s);
System.out.println();
System.out.println(x);
}
}