nvat
BAN USERimport java.util.ArrayList;
import java.util.HashSet;
public class UniqueSubString {
public static void main(String[] args){
System.out.println(findLongestSubString("aabbcde"));
System.out.println(findLongestSubString("abccc"));
System.out.println(findLongestSubString("abcccdefg"));
}
public static String findLongestSubString(String str){
HashSet<Character> set = new HashSet<>();
ArrayList<String> list = new ArrayList<>();
int maxSizeIndex =0;
int maxSize = 0;
for(int i=0;i<str.length();i++){
if(set.contains(str.charAt(i)) || i==str.length()-1) {
String substr = "";
for(Character char2:set){
substr+=char2;
}
if(i==str.length()-1) substr+=str.charAt(i);
list.add(substr);
if(maxSize < substr.length()) {
maxSize = set.size();
maxSizeIndex = list.size()-1;
}
set.clear();
}
set.add(str.charAt(i));
}
return list.isEmpty()?null:list.get(maxSizeIndex);
}
}
public class PasswordPossibilities {
public static void main(String[] args){
Set<String> possiblities = new HashSet<>();
String str ="Password";
possiblities(str,"",possiblities);
for(int i=0;i<str.length();i++){
if(str.charAt(i) == 's'){
possiblities(str.substring(0,i)+"$"+str.substring(i+1),"",possiblities);
} else if(str.charAt(0) == 'a') {
possiblities(str.substring(0,i)+"@"+str.substring(i+1),"",possiblities);
}
}
System.out.println(possiblities);
}
private static void possiblities(String rest, String prefix, Set<String> possiblities) {
if(rest.length()==0){
return;
}
possiblities.add(prefix+rest);
if(rest.charAt(0) == 's'){
possiblities(rest.substring(1),prefix+"$",possiblities);
} else if(rest.charAt(0) == 'a') {
possiblities(rest.substring(1),prefix+"@",possiblities);
} else{
possiblities(rest.substring(1),prefix+rest.charAt(0),possiblities);
}
}
}
- nvat July 04, 2017