题目描述:请从字符串中找出一个最长的不包含重复字符串的的子字符串,计算该最长子字符串的长度。
#include<iostream> #include<string> #include<vector> #include<map> #include<algorithm> #include<climits> #include<unordered_map> using namespace std; int lengthOfLongestSubString(string& s){ unordered_map<char, int> window; int ans = 0, left = 0, right = 0; while(right < s.size()){ window[s[right]]++; right++; while(window[s[right]] > 1){ window[left]--; left++; } ans = max(ans, right - left); } return ans; } void utils(){ cout << "enter string: "; string s; cin >> s; cout << "longestSize: " << lengthOfLongestSubString(s) << endl; } int main(){ utils(); return 0; }