Longest Substring Without Repeating Characters (implemented in C), longestrepeating

Source: Internet
Author: User

Longest Substring Without Repeating Characters (implemented in C), longestrepeating

Given a string, find the length of the longest substring without repeating characters.

Examples:

Given"abcabcbb", The answer is"abc", Which the length is 3.

Given"bbbbb", The answer is"b", With the length of 1.

Given"pwwkew", The answer is"wke", With the length of 3. Note that the answer must be a substring,"pwke"IsSubsequenceAnd not a substring.

 

Address: https://leetcode.com/problems/longest-substring-without-repeating-characters/

Returns the length of the longest substring. The substring cannot contain repeated characters. (The substring is continuous .)

Solution: read each character of the string in sequence. If the first occurrence occurs, the length of the current substring is + 1. Otherwise, first determine whether the current length is greater than the maximum length, and then replace the maximum length. Then

Find duplicate characters in the original string.

The Code is as follows:

1 int lengthOfLongestSubstring (char * s) {2 int maxlen = 0, currlen = 0; 3 int table [128], I, j, start = 0; 4 memset (table, 0, sizeof (table); 5 for (I = 0; s [I]! = '\ 0'; ++ I) {6 if (++ table [s [I]) = 2) {7 if (currlen> maxlen) {8 maxlen = currlen; 9} 10 for (j = start; j <I; ++ j) {// The second position after the repeated characters in the start record is 11 if (s [j] = s [I]) {12 table [s [j] = 1; 13 start = j + 1; 14 break; 15} else {16 -- currlen; 17 table [s [j] = 0; 18} 19} 20} else {21 + + currlen; 22} 23} 24 if (currlen> maxlen) {25 maxlen = currlen; 26} 27 return maxlen; 28}

Related Article

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.