1 /** 2 * 获取无重复字符的最长子串 3 * @param srcStr 4 * @return 5 */ 6 public static String getMaxLenNoRepeatChar(String srcStr){ 7 if (srcStr==null){ 8 return null; 9 } 10 if (srcStr.isEmpty()){ 11 return null; 12 } 13 14 Random random = new Random(); 15 int count=0; 16 HashSet<String> stringHashSet = new HashSet<>(); 17 while (true){ 18 19 int subStrBegIndex = random.nextInt(srcStr.length()); 20 int subStrEndIndex = random.nextInt(srcStr.length()); 21 int subStrEndIndex1 = subStrEndIndex + 1; 22 if (subStrBegIndex<subStrEndIndex1){ 23 String substring = srcStr.substring(subStrBegIndex, subStrEndIndex1); 24 stringHashSet.add(substring); 25 } 26 if (count>1000000){ 27 break; 28 } 29 count++; 30 } 31 32 ArrayList<String> stringArrayList = new ArrayList<>(); 33 for (String s : stringHashSet) { 34 boolean b = checkStrHasRepeatChar(s); 35 if (!b){ 36 stringArrayList.add(s); 37 } 38 } 39 40 Collections.sort(stringArrayList, new Comparator<String>() { 41 @Override 42 public int compare(String o1, String o2) { 43 return o1.length()-o2.length(); 44 } 45 }); 46 47 System.out.println(stringArrayList); 48 49 HashMap<String, Integer> stringIntegerHashMap = new HashMap<>(); 50 for (String s : stringArrayList) { 51 if (stringIntegerHashMap.containsKey(s.length()+"")){ 52 stringIntegerHashMap.put(s.length()+"",stringIntegerHashMap.get(s.length()+"")+1); 53 }else { 54 stringIntegerHashMap.put(s.length()+"",1); 55 } 56 } 57 58 System.out.println(stringIntegerHashMap); 59 60 ArrayList<CustmerStrCompute> custmerStrComputes = new ArrayList<>(); 61 for (Map.Entry<String,Integer> entry : stringIntegerHashMap.entrySet()){ 62 String key = entry.getKey(); 63 Integer value = entry.getValue(); 64 65 CustmerStrCompute custmerStrCompute = new CustmerStrCompute(); 66 custmerStrCompute.setId(UUID.randomUUID().toString()); 67 custmerStrCompute.setStrLen(Integer.valueOf(key)); 68 custmerStrCompute.setStrRepeatFrequence(value); 69 70 custmerStrComputes.add(custmerStrCompute); 71 } 72 73 Collections.sort(custmerStrComputes, new Comparator<CustmerStrCompute>() { 74 @Override 75 public int compare(CustmerStrCompute o1, CustmerStrCompute o2) { 76 return o1.getStrLen()-o2.getStrLen(); 77 } 78 }); 79 80 System.out.println(custmerStrComputes); 81 82 CustmerStrCompute custmerStrCompute = custmerStrComputes.get(custmerStrComputes.size() - 1); 83 Integer strLen = custmerStrCompute.getStrLen(); 84 // Integer strRepeatFrequence = custmerStrCompute.getStrRepeatFrequence(); 85 ArrayList<String> stringArrayList1 = new ArrayList<>(); 86 for (String s : stringArrayList) { 87 if (s.length()==strLen){ 88 stringArrayList1.add(s); 89 } 90 } 91 return stringArrayList1.toString(); 92 } 93 94 /** 95 * 判断字符串是否有重复字符 96 * @param srcStr 97 * @return 98 */ 99 public static boolean checkStrHasRepeatChar(String srcStr){ 100 if (srcStr==null){ 101 return false; 102 } 103 if (srcStr.isEmpty()){ 104 return false; 105 } 106 107 HashMap<Character, Integer> characterIntegerHashMap = new HashMap<>(); 108 for (int i = 0; i < srcStr.length(); i++) { 109 char c = srcStr.charAt(i); 110 if (characterIntegerHashMap.containsKey(c)){ 111 characterIntegerHashMap.put(c,characterIntegerHashMap.get(c)+1); 112 }else { 113 characterIntegerHashMap.put(c,1); 114 } 115 } 116 117 for (Map.Entry<Character,Integer> entry : characterIntegerHashMap.entrySet()){ 118 // Character key = entry.getKey(); 119 Integer value = entry.getValue(); 120 121 if (value>=2){ 122 return true; 123 } 124 } 125 return false; 126 } 127 128 class CustmerStrCompute{ 129 private String id; 130 private Integer strLen; 131 private Integer strRepeatFrequence; 132 133 public String getId() { 134 return id; 135 } 136 137 public void setId(String id) { 138 this.id = id; 139 } 140 141 public Integer getStrLen() { 142 return strLen; 143 } 144 145 public void setStrLen(Integer strLen) { 146 this.strLen = strLen; 147 } 148 149 public Integer getStrRepeatFrequence() { 150 return strRepeatFrequence; 151 } 152 153 public void setStrRepeatFrequence(Integer strRepeatFrequence) { 154 this.strRepeatFrequence = strRepeatFrequence; 155 } 156 157 @Override 158 public String toString() { 159 return "CustmerStrCompute{" + 160 "id='" + id + '\'' + 161 ", strLen=" + strLen + 162 ", strRepeatFrequence=" + strRepeatFrequence + 163 '}'; 164 } 165 }
标签:子串,字符,return,String,public,srcStr,new,Integer,最长 From: https://www.cnblogs.com/liaowanzhong/p/18394516