32. 最小子串覆盖

32. 最小子串覆盖

 

给定两个字符串 source 和 target. 求 source 中最短的包含 target中每一个字符的子串.

样例

样例 1:

输入: source = "abc", target = "ac"

输出: "abc"

样例 2:

输入: source = "adobecodebanc", target = "abc"

输出: "banc"

解释: "banc" 是 source 的包含 target 的每一个字符的最短的子串.

样例 3:

输入: source = "abc", target = "aa"

输出: ""

解释: 没有子串包含两个 'a'.

挑战

O(n) 时间复杂度

注意事项

  1. 如果没有答案, 返回 "".

  2. 保证答案是唯一的.

  3. target 可能包含重复的字符, 而你的答案需要包含至少相同数量的该字符.


public class Solution {

    /**

     * @param source : A string

     * @param target: A string

     * @return: A string denote the minimum window, return "" if there is no such a string

     */

   public String minWindow(String source, String target) {

            // write your code here

           if (source.contains(target)) return target;

            String result = "";

            int[] sour = new int[256];

            int[] tar = new int[256];

            for (int i = 0; i < target.length(); i++) {

                char c = target.charAt(i);

                tar[c]++;

            }

            int start = 0, end = 0, len = Integer.MAX_VALUE;

            for (int i = 0; i < source.length(); i++) {

                char c = source.charAt(i);

                sour[c]++;

                if (equalTwo(sour, tar)) {

                    end = i;

                    while (true) {

                        char ch = source.charAt(start);

                        if (sour[ch]-1 <tar[ch] ) {

                            break;

                        }

                        sour[ch]--;

                        start++;

                    }

                    if (end - start < len) {

                        len = end - start;

                        result = source.substring(start, end + 1);

                        // System.out.println(start+","+ end);

                    }

                }

            }

            return result;

        }





        boolean equalTwo(int[] x, int[] y) {

            for (int i = 0; i < x.length; i++) {

                if (x[i] < y[i]) return false;

            }

            return true;

        }

}







public class Solution {

    /**

     * @param source : A string

     * @param target: A string

     * @return: A string denote the minimum window, return "" if there is no such a string

     */

   public String minWindow(String source, String target) {

            // write your code here

            if (source.contains(target)) return target;

            String result = "";

            HashMap<Character, Integer> hashMap = new HashMap<>();

            for (int i = 0; i < target.length(); i++) {

                char c = target.charAt(i);

                if (hashMap.containsKey(c)) {

                    int num = hashMap.get(c);

                    num++;

                    hashMap.put(c, num);

                } else {

                    hashMap.put(c, 1);

                }

            }

           HashMap<Character, Integer> add = new HashMap<>();

            int start = 0, end = 0 ,len=Integer.MAX_VALUE;

            for (int i = 0; i < source.length(); i++) {

                char c = source.charAt(i);

                if (hashMap.containsKey(c)) {

                    if (add.containsKey(c)) {

                        int num = add.get(c);

                        num++;

                        add.put(c, num);

                    } else {

                        add.put(c, 1);

                    }

                    if (equalTwo(hashMap, add)) {

                        end = i;

                        while (true){

                            char ch = source.charAt(start);

                            if (hashMap.containsKey(ch)){

                                int num = add.get(ch);

                                num--;

                                int num2 = hashMap.get(ch);

                                if (num2>num){

                                    break;

                                }

                                add.put(ch, num);

                            }

                            start++;

                        }

                        if (end-start<len){

                            len=end-start;

                            result = source.substring(start, end + 1);

                            // System.out.println(start+","+ end);

                        }

                    }

                }

            }

            return result;

        }





         boolean equalTwo(HashMap<Character, Integer> hashMap1, HashMap<Character, Integer> hashMap2) {

            // if (hashMap1.size() != hashMap2.size()) return false;

            for (Map.Entry<Character, Integer> entry : hashMap1.entrySet()) {

                // System.out.println(entry.getValue()+","+hashMap2.get(entry.getKey()));

                if (hashMap2.get(entry.getKey())==null){

                    return false;

                }else {

                    if (entry.getValue() > hashMap2.get(entry.getKey())) {

                        return false;

                    }

                }

            }

            return true;

        }

}

 

1.import axios from 'axios'   2.const service = axios.create({   3.    baseURL: '', // url = base url + request url   4.})   5.   6.function request(url,method, params, data, success, failure){   7.   8.    return new Promise((resolve,reject)=>{   9.        service.request({   10.            url: store.getters.serverUrl+url,   11.            method: method,   12.            data: data,   13.            withCredentials: params.withCredentials === false ? false : true,   14.        }).then(res=>{   15.            if (res.statusCode == 200) {   16.                //成功   17.                if(res.data.error){   18.                    reject(res.data.error);   19.                }   20.                resolve(res.data.result);   21.            }else{   22.                reject(res);   23.            }   24.        }).catch(err=>{   25.            uni.showModal({   26.                title: '提示',   27.                showCancel:false,   28.                content: '服务请求失败:' + err.errMsg,   29.                success(res) {}   30.            });   31.            reject(err);   32.        })   33.    })   34.}   export default request 
最新发布
05-27
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

时代我西

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值