|
| 1 | +class Solution { |
| 2 | +public: |
| 3 | + int lengthOfLongestSubstring(string s) { |
| 4 | + int maxLen = 0; |
| 5 | + int pos = 0; |
| 6 | + int lastpos = -1; |
| 7 | + unordered_map<char,int> m; |
| 8 | + m.clear(); |
| 9 | + for(int i=0;i<s.length();i++) { |
| 10 | + if(m.find(s[i]) != m.end()) { |
| 11 | + pos = m.find(s[i])->second; |
| 12 | + for(int j=pos;j>lastpos;j--) { |
| 13 | + if(m.find(s[j]) != m.end()) |
| 14 | + m.erase(m.find(s[j])); |
| 15 | + } |
| 16 | + lastpos = pos; |
| 17 | + m.insert({s[i],i}); |
| 18 | + } else { |
| 19 | + m.insert({s[i],i}); |
| 20 | + } |
| 21 | + if(maxLen < m.size()) { |
| 22 | + maxLen = m.size(); |
| 23 | + } |
| 24 | + } |
| 25 | + return maxLen; |
| 26 | + } |
| 27 | +}; |
| 28 | + |
| 29 | +string stringToString(string input) { |
| 30 | + assert(input.length() >= 2); |
| 31 | + string result; |
| 32 | + for (int i = 1; i < input.length() -1; i++) { |
| 33 | + char currentChar = input[i]; |
| 34 | + if (input[i] == '\\') { |
| 35 | + char nextChar = input[i+1]; |
| 36 | + switch (nextChar) { |
| 37 | + case '\"': result.push_back('\"'); break; |
| 38 | + case '/' : result.push_back('/'); break; |
| 39 | + case '\\': result.push_back('\\'); break; |
| 40 | + case 'b' : result.push_back('\b'); break; |
| 41 | + case 'f' : result.push_back('\f'); break; |
| 42 | + case 'r' : result.push_back('\r'); break; |
| 43 | + case 'n' : result.push_back('\n'); break; |
| 44 | + case 't' : result.push_back('\t'); break; |
| 45 | + default: break; |
| 46 | + } |
| 47 | + i++; |
| 48 | + } else { |
| 49 | + result.push_back(currentChar); |
| 50 | + } |
| 51 | + } |
| 52 | + return result; |
| 53 | +} |
| 54 | + |
| 55 | +int main() { |
| 56 | + string line; |
| 57 | + while (getline(cin, line)) { |
| 58 | + string s = stringToString(line); |
| 59 | + |
| 60 | + int ret = Solution().lengthOfLongestSubstring(s); |
| 61 | + |
| 62 | + string out = to_string(ret); |
| 63 | + cout << out << endl; |
| 64 | + } |
| 65 | + return 0; |
| 66 | +} |
0 commit comments