LeetCode-30. Substring with Concatenation of All Words [C++][Java]

LeetCode-30. Substring with Concatenation of All Wordshttps://leetcode.com/problems/substring-with-concatenation-of-all-words/

题目描述

You are given a string s and an array of strings words of the same length. Return all starting indices of substring(s) in s that is a concatenation of each word in words exactly oncein any order, and without any intervening characters.

You can return the answer in any order.

Example 1:

Input: s = "barfoothefoobarman", words = ["foo","bar"]
Output: [0,9]
Explanation: Substrings starting at index 0 and 9 are "barfoo" and "foobar" respectively.
The output order does not matter, returning [9,0] is fine too.

Example 2:

Input: s = "wordgoodgoodgoodbestword", words = ["word","good","best","word"]
Output: []

Example 3:

Input: s = "barfoofoobarthefoobarman", words = ["bar","foo","the"]
Output: [6,9,12]

解题思路

【C++】

给定一个字符串 s 和一些长度相同的单词 words。找出 s 中恰好可以由 words 中所有单词串联形成的子串的起始位置。注意子串要与 words 中的单词完全匹配,中间不能有其他字符,但不需要考虑 words 中单词串联的顺序。

class Solution {
public:
    vector<int> findSubstring(string s, vector<string>& words) {
        vector<int> res;
        int len = s.size(), m = words.size(), n = words[0].size(), wLen = m * n;
        if (len == 0 || m == 0 || len < wLen) {return res;}
        unordered_map<string, int> mp;
        for (int i=0; i<m; i++) {++mp[words[i]];}
        for (int i=0; i+wLen<=len; i++) {
            int j = i;
            unordered_map<string, int> tmp;
            for (; j < i + wLen; j += n) {
                string str = s.substr(j, n);
                if (mp.find(str) == mp.end()) {break;}
                ++tmp[str];
                if (mp[str] < tmp[str]) {break;}
            }
            if (j == i + wLen) {
                res.push_back(i);
            }
        }
        return res;
    }
};

【Java】

class Solution {
    public List<Integer> findSubstring(String s, String[] words) {
        List<Integer> res = new LinkedList<>();
        int len = s.length(), m = words.length, n = words[0].length();
        int wLen = m * n;
        if (len ==0 || m == 0 || len < wLen) {return res;}
        Map<String, Integer> dict = new HashMap<>();
        for (String word : words) {dict.put(word, dict.getOrDefault(word, 0) + 1);}
        for (int i=0; i<=len-wLen; i++) {
            int j = i;
            Map<String, Integer> tmp = new HashMap<>();
            for (; j < i + wLen; j += n) {
                String str = s.substring(j, j + n);
                if (!dict.containsKey(str)) {break;}
                tmp.put(str, tmp.getOrDefault(str, 0) + 1);
                if (dict.get(str) < tmp.get(str)) {break;}
            }
            if (j == i + wLen) {
                res.add(i);
            }
        }
        return res;
    }
}

参考文献

【1】c++中unordered_map的用法的详述(包含unordered_map和map的区别)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

贫道绝缘子

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

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

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

打赏作者

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

抵扣说明:

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

余额充值