标签:map right string distance int ++ Substring 76 Window
算法描述:
Given a string S and a string T, find the minimum window in S which will contain all the characters in T in complexity O(n).
Example:
Input: S = "ADOBECODEBANC", T = "ABC" Output: "BANC"
Note:
- If there is no such window in S that covers all characters in T, return the empty string
""
. - If there is such window, you are guaranteed that there will always be only one unique minimum window in S.
解题思路:子字符串问题,可以采用强大的滑动窗口方法解决。首先用map对字符串进行映射,并用两个指针分别指向窗口左边和右边,通过不断的滑动两个指针实现对窗口内字符的操作。
string minWindow(string s, string t) { string res = ""; unordered_map<char,int> map; for(char c:t) map[c]++; int left=0; int right = 0; int distance = INT_MAX; int count = t.size(); int head = 0; while(right < s.size()){ if(map[s[right++]]-- >0) count--; while(count==0){ if(right-left< distance){ head = left; distance = right-head; } if(map[s[left++]]++ ==0) count++; } } return distance==INT_MAX?"":s.substr(head,distance); }
标签:map,right,string,distance,int,++,Substring,76,Window 来源: https://www.cnblogs.com/nobodywang/p/10372839.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。