Given two strings s and t of lengths m and n respectively, return the minimum window substring ofssuch that every character int(including duplicates) is included in the window. If there is no such substring**, return the empty string"".
The testcases will be generated such that the answer is unique.
A substring is a contiguous sequence of characters within the string.
Example 1:
1 2 3
Input: s = "ADOBECODEBANC", t = "ABC" Output: "BANC" Explanation: The minimum window substring "BANC" includes 'A', 'B', and 'C' from string t.
Example 2:
1 2 3
Input: s = "a", t = "a" Output: "a" Explanation: The entire string s is the minimum window.
Example 3:
1 2 3 4
Input: s = "a", t = "aa" Output: "" Explanation: Both 'a's from t must be included in the window. Since the largest window of s only has one 'a', return empty string.
int left = 0, right = 0, valid = 0; while (right < s2.length()) { char c = charS2[right]; right++; if (need.containsKey(c)) { window.put(c, window.getOrDefault(c, 0) + 1); if (window.get(c).equals(need.get(c))) { valid++; } }
System.out.println("window:"+left+"->"+right); System.out.println("window is: "+window.toString()); System.out.println("need is: "+need.toString()); System.out.println("====================================\n");
while (right - left >= s1.length()) { if (valid == need.size()) { returntrue; } char d = charS2[left]; left++; if (need.containsKey(d)) { if (window.get(d).intValue() == need.get(d).intValue()) { valid--; } window.put(d, window.getOrDefault(d, 0) - 1); }
} } returnfalse; } }
对于这道题的解法代码,基本上和最小覆盖子串一模一样,只需要改变两个地方:
1、本题移动 left 缩小窗口的时机是窗口大小大于 t.size() 时,因为排列嘛,显然长度应该是一样的。
Given two strings s and p, return an array of all the start indices ofp‘s anagrams ins. You may return the answer in any order.
An Anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once.
Example 1:
1 2 3 4 5
Input: s = "cbaebabacd", p = "abc" Output: [0,6] Explanation: The substring with start index = 0 is "cba", which is an anagram of "abc". The substring with start index = 6 is "bac", which is an anagram of "abc".
Example 2:
1 2 3 4 5 6
Input: s = "abab", p = "ab" Output: [0,1,2] Explanation: The substring with start index = 0 is "ab", which is an anagram of "ab". The substring with start index = 1 is "ba", which is an anagram of "ab". The substring with start index = 2 is "ab", which is an anagram of "ab".
publicclassFindAnagrams{ public List<Integer> findAnagrams(String s, String p){ HashMap<Character, Integer> need = new HashMap<>(); HashMap<Character, Integer> window = new HashMap<>();
int left = 0, right = 0, valid = 0; List<Integer> res = new ArrayList<>(); while (right < s.length()) { char c = charTargetString[right]; right++; if (need.containsKey(c)) { window.put(c, window.getOrDefault(c, 0) + 1); if (need.get(c).equals(window.get(c))) { valid++; } }
Given a string s, find the length of the longest substring without repeating characters.
Example 1:
1 2 3
Input: s = "abcabcbb" Output: 3 Explanation: The answer is "abc", with the length of 3.
Example 2:
1 2 3
Input: s = "bbbbb" Output: 1 Explanation: The answer is "b", with the length of 1.
Example 3:
1 2 3 4
Input: s = "pwwkew" Output: 3 Explanation: The answer is "wke", with the length of 3. Notice that the answer must be a substring, "pwke" is a subsequence and not a substring.
Example 4:
1 2
Input: s = "" Output: 0
这就是变简单了,连 need 和 valid 都不需要,而且更新窗口内数据也只需要简单的更新计数器 window 即可。
当 window[c] 值大于 1 时,说明窗口中存在重复字符,不符合条件,就该移动 left 缩小窗口了嘛。
唯一需要注意的是,在哪里更新结果 res 呢?我们要的是最长无重复子串,哪一个阶段可以保证窗口中的字符串是没有重复的呢?
这里和之前不一样,要在收缩窗口完成后更新 res,因为窗口收缩的 while 条件是存在重复元素,换句话说收缩完成后一定保证窗口中没有重复嘛。