class Solution {//easy public: int lengthOfLongestSubstring(string s) { // Start typing your C/C++ solution below // DO NOT write int main() function vector<int> charPos(300,-1);//latest pos of a letter int start = 0, max = 0; for(int end = 0; end < s.size(); ++end){ char const letter = s[end]; if (charPos[letter] >= start){//letter exists start = charPos[letter] + 1; } charPos[letter] = end; int const length = end-start+1; if (max < length) max=length; } return max; } };
Solving problems is fun. But solving the same problem over and over is pain. Once solved, forever solved.
sourcecode
Tuesday, December 4, 2012
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.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment