Finds substrings of the longest and no repeating character in a string

Source: Internet
Author: User

Set a current substring: tempstring

Set up an array that keeps the longest and no repeating substring: list

Ideas:

Judging from the first character,

If the current substring does not include the current character, the current substring joins the current character as the new current substring,

If the current substring includes the current character, judging the position of the current character in the current string, divide the string into two strings according to this position, and if the trailing end adds the current character as the new current substring, the length of the substring in the current substring and the list array is determined, and if the current substring is long, the list is emptied. Joins the current substring, or, if it is equal, joins the current substring directly into the list.

The substring in the last list 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");
}

Finds substrings of the longest and no repeating character in a string

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.