Find the longest non-repeating substring of a string, output the largest substring

Source: Internet
Author: User

For example: Ababcdebc, so his biggest substring is: ABCDE

Using system;using system.collections.generic;using system.linq;using system.text;using  system.threading.tasks;namespace consoleapplication4{    class program{         static void main (String[] args) {             string s =  "ABCABABCDEFGFG";             char[] list = s.tochararray () ;             findmaxsubdifferencestring ss  = new findmaxsubdifferencestring (); SS. Findmaxsubdifferencestrings (list, s.length);             string s1 =  "AAAAAA";             char[] list1 = s1. ToCharArray (); SS. FindmaxsubdifferenceStringS (list1, s1. Length);            string s2 =  " Ababab ";            char[] list2 =  S2. ToCharArray (); SS. Findmaxsubdifferencestrings (LIST2,&NBSP;S2. Length);            string s3 =  " ABCABABCDEFGFGABCABABCDEFGFG ";             char[]  list3 = s3. ToCharArray (); SS. Findmaxsubdifferencestrings (LIST3,&NBSP;S3. Length);}}     public class FindMaxSubDifferenceString{         public bool isdup (char[] list, int len, char targetchar,  Int begin, int end, out int targetindex) {targetindex = -1;             // note: first, we need to check all parameters are  Valid, such as list is null, len >0, or begin <end,  begin>0, end<len, etc             for (int i=begin;i<=end; i++) {                 if (List[i]==targetchar) {targetindex = i;                     return  true;}}             return false;}         public void findmaxsubdifferencestrings (char[]  List, int len) {            if  (list  == null)  return;            if  (len <= 0)  return;            int maxsubstringlen  = 0;            int  maxsubstringbeginindex = 0;             int currentmaxsubstringbeginindex = 0;             int currentMaxSubLen = 0;             for  (int i = 1; i < len; i++) {                 int dupindex =  -1;                if   (Isdup (LIST,&NBSP;LEN,&NBSP;LIST[I],&NBsp;currentmaxsubstringbeginindex, i - 1, out dupindex)) {currentMaxSubLen =  i - currentMaxSubStringBeginIndex;                     if  (currentmaxsublen >  Maxsubstringlen) {maxsubstringlen = currentmaxsublen;maxsubstringbeginindex =  Currentmaxsubstringbeginindex;}                      else{}currentmaxsubstringbeginindex = dupindex + 1;}                 else // not dup{}}             if  ( maxsubstringlen == 0) {Maxsubstringlen = len;}             console.write ("INPUT&Nbsp;string:  ");            for  (int  i = 0; i < len; i++) {                 console.write (List[i]);}             console.writeline ("");             console.write ("after input: ");             for  (int i =  maxsubstringbeginindex; i < maxsubstringbeginindex + maxsubstringlen; i++) {                 console.write ( List[i]);}              console.writeline ("");}}


Find the longest non-repeating substring of a string, output the largest substring

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.