Leetcode-Minimum Window Substrings(unordered_map)

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).

For example,
S = "ADOBECODEBANC"
T = "ABC"

Minimum window is "BANC".

Note:
If there is no such window in S that covers all characters in T, return the empty string "".

If there are multiple such windows, you are guaranteed that there will always be only one unique minimum window in S.

算法思想:通过建立hash表unordered_map来记录扫描过的元素,并且记录其重复个数。因为S只能遍历一遍,所以还需要两个指针去限制最终包含区域的范围,即first与behind。且还需要一个count去记录每次循环过程中是否达到了包含字符串T的条件。具体代码如下:

string minWindow(string S, string T) {
	string result;
	if (S.empty() || T.empty())
		return result;
	unordered_map<char, int> m1; //建立<char,int>类型的hash表,方便记录重复元素
	unordered_map<char, int> m2;
	for (int i = 0; i < T.length(); i++)
		m1[T[i]]++;  //记录target中每个元素的个数
	int first = 0, behind = 0;//用两个指针去维护字符串S
	int count=0;//监控器
	int minlength = INT_MAX;
	for (; first < S.length(); first++) {
		if (m1.find(S[first]) != m1.end()) {
			m2[S[first]]++;
			if (m2[S[first]] <= m1[S[first]]) // 元素重复不能比待匹配串多
				count++;
		}
		if (count >= T.length()) {
			while (m1.find(S[behind]) == m1.end() || m2[S[behind]]>m1[S[behind]]) { //当出现查找不到或者m2的某元素已经多余m1的这个元素时
				m2[behind]--;
				behind++; //向后移一位
			}
			if (first - behind + 1 < minlength) { //遇到更短的区间时更新
				minlength = first - behind + 1;
				result = S.substr(behind, minlength);
			}
		}
	}
	return result;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值