给定字符串s和t,要求在s中寻找包含t中所有字符的最小窗口

public class MinimumWindowSubstring
{
public MinimumWindowSubstring(){}

public String minWindow(String s, String t) 
{
	if(s.length() == 0 || t.length() == 0)
	{return "";}
	
	int start= 0;int end= 0;
	int minStart= 0;int minEnd= 0;
	int min= Integer.MAX_VALUE;
	int matched= 0;boolean found= false;
	Map<Character, Integer> s_map= new HashMap<>();
	Map<Character, Integer> t_map= getMap(t);
	Queue<Integer> queue= new LinkedList<>();
	
	while(end < s.length())
	{
		char c= s.charAt(end);
		if(t_map.containsKey(c))
		{
			queue.add(end);
			addtoMap(s_map, c);
			if(s_map.get(c) <= t_map.get(c))
			{matched++;}
		}
	
	    while(matched == t.length())
	    {
		    found= true;
		    start= queue.poll();
		    if((end-start) < min)
		    {
		    	minStart= start;
			    minEnd= end;
			    min= end-start;
		    }
		    removeformMap(s_map, s.charAt(start));
		    if(!s_map.containsKey(s.charAt(start)) ||
				s_map.get(s.charAt(start)) < 
				t_map.get(s.charAt(start)))
		    {matched--;}
	    }
	    end++;
	}
	return found ? s.substring(minStart, minEnd+1) : "";
}

private void addtoMap(Map<Character, Integer> map, char c)
{
	if(!map.containsKey(c))
	{map.put(c, 1);}
	else
	{
		int count= map.get(c);
		map.put(c, count+1);
	}
}

private void removeformMap(Map<Character, Integer> map, char c)
{
	int count= map.get(c);
	count--;
	if(count == 0)
	{map.remove(c);}
	else
	{map.put(c, count);}
}

private Map<Character, Integer> getMap(String t)
{
	Map<Character, Integer> map= new HashMap<>();
	for(int i=0;i<t.length();i++)
	{addtoMap(map, t.charAt(i));}
	return map;
}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用滑动窗口的思想来解决该问题。具体步骤如下: 1. 定义两个指针left和right,分别表示滑动窗口的左右端点,初始值为0。 2. 定义一个字典need,用于记录str2每个字符出现的次数。 3. 定义一个计数器count,用于记录滑动窗口已经包含了str2多少个不同的字符。 4. 定义一个变量min_len,用于记录最小包含str2所有字符的子串长度,初始值为正无穷大。 5. 开始循环,当右指针小于str1的长度时: a. 如果str1[right]在need出现过,将need[str1[right]]的值减一,并判断need[str1[right]]是否等于0,如果是,则将count加一。 b. 将右指针右移一位。 c. 当count等于need的长度时,说明滑动窗口已经包含了str2的所有字符,计算当前子串的长度,并将min_len更新为当前子串长度和min_len的较小值。 d. 如果str1[left]在need出现过,将need[str1[left]]的值加一,并判断need[str1[left]]是否大于0,如果是,则将count减一。 e. 将左指针右移一位。 6. 返回min_len即可。 代码实现如下: ``` def min_substring(str1, str2): need = {} for ch in str2: need[ch] = need.get(ch, 0) + 1 left = right = 0 count = 0 min_len = float('inf') while right < len(str1): if str1[right] in need: need[str1[right]] -= 1 if need[str1[right]] == 0: count += 1 right += 1 while count == len(need): if right - left < min_len: min_len = right - left if str1[left] in need: need[str1[left]] += 1 if need[str1[left]] > 0: count -= 1 left += 1 return min_len ``` 测试代码: ``` print(min_substring('abcd', 'bc')) # 2 print(min_substring('adbecf', 'abc')) # 3 print(min_substring('a', 'a')) # 1 print(min_substring('aaaaa', 'a')) # 1 print(min_substring('ab', 'a')) # 1 ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值