public class AE { public static void main(String[] args) { //String s = "abacdecfgab"; String s = "abacdbe"; System.out.println(noDuplicate(s)); } public static String noDuplicate(String str) { String sub = ""; String result = ""; for (int i = 0; i < str.length(); i++) { String c = "" + str.charAt(i); if (sub.contains(c)) { if (sub.length() > result.length()) { result = sub; } sub = sub.substring(sub.indexOf(c)+1); } sub += c; } if (sub.length() > result.length()) { result = sub; } return result; } }
时间: 2024-10-07 18:14:33