Created
September 8, 2018 00:02
-
-
Save zhangxiaomu01/d4af031e8cb2b09bac740f0ccb18ad92 to your computer and use it in GitHub Desktop.
Longest_Sub_string_without_Repeat_Characters_BruteForce
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public: | |
bool isAllUnique(string &s, int start, int end) | |
{ | |
unordered_set<char> hash; | |
for(int i = start; i <= end; i++) | |
{ | |
if(hash.find(s[i]) != hash.end()) | |
return false; | |
hash.insert(s[i]); | |
} | |
return true; | |
} | |
int lengthOfLongestSubstring(string s) { | |
int len = s.size(); | |
int maxLen = s.empty()? 0 : 1; | |
for(int i = 0; i < len; i++) | |
{ | |
for(int j = i + 1; j< len; j++) | |
{ | |
if(isAllUnique(s, i, j)) | |
maxLen = max(j - i + 1, maxLen); | |
} | |
} | |
return maxLen; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment