2021-12-26 array linked list day3

Sliding window idea

Topic 1:

76. Minimum coverage substringlabuladong problem solutionthinking

Give you a string {s and a string} t. Returns the minimum substring of all characters of # t # in # s #. If there is no substring covering all characters of {t} in {s}, the empty string "" is returned.

 

be careful:

  • For repeated characters in # t #, the number of characters in the substring we are looking for must not be less than the number of characters in # t #.
  • If there is such a substring in , s , we guarantee that it is the only answer.

 

Example 1:

Input: S = "Adobe codebanc", t = "ABC"
Output: "BANC"

Example 2:

Input: s = "a", t = "a"
Output: "a"

Example 3:

Input: s = "a", t = "aa"
Output: ''
Explanation: both characters' a 'in t should be included in the substring of s,
Therefore, there is no qualified substring and an empty string is returned.

 

Tips:

  • 1 <= s.length, t.length <= 105
  • s , and t , are composed of English letters

 

Advanced: can you design an algorithm to solve this problem in {o(n) time?
 
class Solution {
    public String minWindow(String s, String t) {
        int[] tmap=new int[52];
        int[] smap=new int[52];
        int l=0,r=0;
        for (int i=0;i<t.length();i++){
            //26 lowercase in the front and capital in the back
            if ('a'<=t.charAt(i)) tmap[t.charAt(i)-'a'+26]++;
            else tmap[t.charAt(i)-'A']++;
        }
        int max=10000,index=-1;
        while (r<s.length()){
            if ('a'<=s.charAt(r)) smap[s.charAt(r)-'a'+26]++;
            else smap[s.charAt(r)-'A']++;
            r++;
        //When the current conditions are met, try to reduce the left boundary as much as possible
while (check(tmap,smap)){
          //Judge whether the answer has been updated. If not, update the solution first
if (index==-1) { index=l; max=r-l; } if ('a'<=s.charAt(l)) smap[s.charAt(l)-'a'+26]--; else smap[s.charAt(l)-'A']--; if (check(tmap,smap)) { l++;
            //The left boundary can be reduced to determine whether it is smaller than the current solution and update the solution
if (r-l<max){ index=l; max=r-l; } }else{ if ('a'<=s.charAt(l)) smap[s.charAt(l)-'a'+26]++; else smap[s.charAt(l)-'A']++; break; } } } //If there is no updated solution, an empty string is returned return index!=-1?s.substring(index,index+max):""; } public boolean check(int[] a,int[] b){ for (int i=0;i<a.length;i++){ if (a[i]>b[i]) return false; } return true; } }

Topic 2:

Give you two strings s1 and s2, and write a function to judge whether s2 contains the arrangement of s1. If yes, return {true; Otherwise, false is returned.

In other words, one of the permutations of s1 {is the} substring of s2}.

 

Example 1:

Input: s1 = "ab" s2 = "eidbaooo"
Output: true
 Explanation: s2 contains one of the permutations of s1 ("ba")

Example 2:

Input: s1= "ab" s2 = "eidboaoo"
Output: false

 

Tips:

  • 1 <= s1.length, s2.length <= 104
  • s1 and s2 contain only lowercase letters
 1 class Solution {
 2     public boolean checkInclusion(String s1, String s2) {
 3         int[] count=new int[26];
 4         int[] ans=new int[26];
 5         for (int i=0;i<s1.length();i++){
 6             count[s1.charAt(i)-'a']++;
 7         }
 8         int l=0,r=0,len=s2.length();
 9         while (r<len){
10             ans[s2.charAt(r)-'a']++;
11             r++;
12             if (r>=s1.length()){
13                 if (r>s1.length()){
14                     ans[s2.charAt(l)-'a']--;
15                     l++;
16                 }
17                 if (check(count,ans)){
18                     return true;
19                 }
20             }
21             //System.out.println(Arrays.toString(ans));
22         }
23         return false;
24     }
25 
26     public boolean check(int[] a,int[] b){
27         for (int i=0;i<a.length;i++){
28             if (a[i]>b[i]) return false;
29         }
30         return true;
31     }
32 }

Fixed length sliding window

Question 3:

Given two strings ^ s ^ and ^ p, find the substrings of ^ p ^ of all ^ p ^ in ^ s ^ and return the starting indexes of these substrings. The order in which answers are output is not considered.

Ectopic words {refer to strings formed by rearrangement of the same letters (including the same string).

 

Example 1:

Input: S = "cbaebacd", P = "ABC"
Output: [0,6]
Explanation:
The substring with the starting index equal to 0 is "cba", which is an ectopic word of "abc".
The substring with the starting index equal to 6 is "bac", which is an ectopic word of "abc".

Example 2:

Input: s = "abab", p = "ab"
Output: [0,1,2]
Explanation:
The substring with the starting index equal to 0 is "ab", which is an ectopic word of "ab".
The substring with the starting index equal to 1 is "ba", which is an ectopic word of "ab".
The substring with the starting index equal to 2 is "ab", which is an ectopic word of "ab".

 

Tips:

  • 1 <= s.length, p.length <= 3 * 104
  • s , and p , contain lowercase letters only
 1 class Solution {
 2     public List<Integer> findAnagrams(String s, String p) {
 3         if (s.length()<p.length()){
 4             return new ArrayList<>();
 5         }else{
 6             int[] a=new int[26];
 7             for (int i = 0; i < p.length(); i++) {
 8                 a[p.charAt(i)-'a']+=1;
 9             }
10             int[] b=new int[26];
11             ArrayList<Integer> arrayList = new ArrayList<>();
12             String temp=s.substring(0,p.length());
13             for (int i = 0; i <= s.length()-p.length(); i++) {
14                 if (i==0){
15                     for (int j = 0; j < temp.length(); j++) {
16                         b[temp.charAt(j)-'a']+=1;
17                     }
18                 }else{
19                     b[s.charAt(i-1)-'a']-=1;
20                     b[s.charAt(i+p.length()-1)-'a']+=1;
21                 }
22                 if (Arrays.equals(a,b)){
23                     arrayList.add(i);
24                 }
25             }
26             return arrayList;
27         }
28     }
29 }

Question 4:

3. Longest substring without repeated characterslabuladong problem solutionthinking

Medium difficulty 6627 collection sharing switch to English to receive dynamic feedback

Given a string , s, please find the length of the longest substring , which does not contain duplicate characters.

 

Example 1:

input: s = "abcabcbb"
output: 3 
explain: Because the longest substring without duplicate characters is "abc",So its length is 3.

Example 2:

input: s = "bbbbb"
output: 1
 explain: Because the longest substring without duplicate characters is "b",So its length is 1.

Example 3:

input: s = "pwwkew"
output: 3
 explain: Because the longest substring without duplicate characters is "wke",So its length is 3.
     Please note that your answer must be the length of the substring,"pwke" Is a subsequence, not a substring.

Example 4:

Input: s = ""
Output: 0

 

Tips:

  • 0 <= s.length <= 5 * 104
  • s , consists of English letters, numbers, symbols and spaces
 1 class Solution {
 2     public int lengthOfLongestSubstring(String s) {
 3         Map<Character,Integer> map=new HashMap<>();
 4         int l=0,r=0,len=s.length(),max=0;
 5         while (r<len) {
 6             if (map.containsKey(s.charAt(r))&&map.get(s.charAt(r))!=0){
 7                 while (l<r&&s.charAt(l)!=s.charAt(r)){
 8                     map.put(s.charAt(l),map.get(s.charAt(l))-1);
 9                     l++;
10                 }
11                 //System.out.println(map+" "+r);
12                 if (l!=r) {
13                     map.put(s.charAt(l),map.get(s.charAt(l))-1);
14                     l++;
15                 }
16                 //System.out.println(map+" "+r);
17             }else {
18                 map.put(s.charAt(r),1);
19                 r++;
20                 max=Math.max(max,r-l);
21                 //System.out.println(map+" "+r);
22             }
23         }
24         return max;
25     }
26 }

 

Added by 7724 on Fri, 31 Dec 2021 02:57:12 +0200