日期:2014-05-20 浏览次数:20714 次
public static void main(String[] args) { String str = "xabcdaef"; Set<String> list=new HashSet<String>(); for (int i = 0; i < str.length(); i++) { for (int j = str.length(); j > 0; j--) { if(i>=j){ break; } String temp = str.substring(i, j); Set<Character> set = new LinkedHashSet<Character>(); for (char c : temp.toCharArray()) set.add(c); if (temp.length() == set.size()) { list.add(temp); } } } System.out.println(list); }
------解决方案--------------------
public static void maxSubString(String str) { int begin = 0; int end = 0; int maxLength = 1; int tempLength = 0; int k = 0; for (int i = 0, j = 1; j < str.length(); j++) { String source = str.substring(i, j); System.out.println(source); k = source.indexOf(str.charAt(j)); if (k > -1) { i = k + 1; tempLength = j - i + 1; } else { tempLength += 1; if (tempLength > maxLength) { maxLength = tempLength; begin = i; end = j; } } } System.out.println("max substring: " + str.substring(begin, end + 1)); }