LeetCode 30 串联所有单词的子串
生活随笔
收集整理的這篇文章主要介紹了
LeetCode 30 串联所有单词的子串
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
https://leetcode-cn.com/problems/substring-with-concatenation-of-all-words/
解決方案
class Solution {public List<Integer> findSubstring(String s, String[] words) {List<Integer> res = new ArrayList<>();if (s == null || s.length() == 0 || words == null || words.length == 0)return res;int one_word = words[0].length();int word_num = words.length;HashMap<String, Integer> map = new HashMap<>(word_num);for (String word : words) {map.put(word, map.getOrDefault(word, 0) + 1);}HashMap<String, Integer> tmp_map = new HashMap<>(word_num);for (int i = 0; i < one_word; i++,tmp_map.clear()) {for (int j = i + one_word ; j <= s.length() ; j+=one_word) {String word = s.substring(j -one_word, j);tmp_map.put(word, tmp_map.getOrDefault(word, 0) + 1);if (j/one_word > word_num - 1){int e0 = j - word_num * one_word;String s0= s.substring(e0 ,e0 + one_word);if (map.equals(tmp_map)) res.add(e0);tmp_map.put(s0,tmp_map.get(s0)-1);tmp_map.remove(s0,0);}}}return res;} }總結(jié)
以上是生活随笔為你收集整理的LeetCode 30 串联所有单词的子串的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: LeetCode 29 两数相除
- 下一篇: LeetCode 31 下一个排列