Find the longest string substring of characters without repeating

Setting a current sub-string: tempString

Setting up a repeat-free holding substring array: list

Ideas:

Judging from the beginning of the first character,

If the current sub-string does not include the current character, the current substring join the current character becomes the new current substring,

If the current substring including the current character in the current character position is determined in the current string, according to the position of the string into two strings, a rear end if the current character is added to the new current substring, determined current sub substring-length string array with the list, if the current sub-string length, the empty list, the current substring added; if equal, added directly to the current substring list.

The final list of the substring is the longest substring

public static void longestNodupSubstring(String string) {
  if (null == string || string.length() == 0) {
   return;
  }
  List<String> list = new ArrayList<String>();
  String tempString = "";
  for (int i = 0; i < string.length(); i++) {
   if (tempString.indexOf(string.charAt(i)) == -1) {
    tempString += string.charAt(i);
   } else {
    int j = tempString.indexOf(string.charAt(i));
    tempString = tempString.substring(j + 1) + string.charAt(i);
   }
   if (list.size() == 0) {
    list.add(tempString);
   } else {
    if (tempString.length() > list.get(0).length()) {
     list.clear();
     list.add(tempString);
    } else {
     if (tempString.length() == list.get(0).length()) {
      list.add(tempString);
     }
    }
   }
  }
  for (int i = 0; i < list.size(); i++) {
   System.out.println(list.get(i));
  }
 }

 public static void main(String[] args) {
  longestNodupSubstring1("abcabcabc");
 }

Published 34 original articles · won praise 2 · views 40000 +

Guess you like

Origin blog.csdn.net/zjj2006/article/details/42744781
Recommended