Longest Substring Without Repeating Characters

Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for “abcabcbb” is “abc”, which the length is 3. For “bbbbb” the longest substring is “b”, with the length of 1.

C++:
01 class Solution {
02 public:
03     int lengthOfLongestSubstring(string s) {
04         // Start typing your C/C++ solution below
05         // DO NOT write int main() function
06         set<char> A;
07         int n=s.size();
08         int i=0;
09         int j=0;
10         int result=0;
11         while(j<n)
12         {
13             if(A.find(s[j])==A.end())
14             {
15                 A.insert(s[j++]);
16                 if(ji>result)
17                     result=ji;
18                 continue;
19             }
20             while((i<n)&&(s[i]!=s[j]))
21             {
22                 A.erase(s[i++]);
23             }
24             A.erase(s[i++]);
25         }
26         return result;
27     }
28 };
Advertisements
This entry was posted in Array and linked list, String. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s