LeetCode【30. 串联所有单词的子串】

编程入门 行业动态 更新时间:2024-10-23 09:37:14

LeetCode【30. 串联所有<a href=https://www.elefans.com/category/jswz/34/1769906.html style=单词的子串】"/>

LeetCode【30. 串联所有单词的子串】

给定一个字符串 s 和一个字符串数组 words words 中所有字符串 长度相同

 s 中的 串联子串 是指一个包含  words 中所有字符串以任意顺序排列连接起来的子串。

  • 例如,如果 words = ["ab","cd","ef"], 那么 "abcdef", "abefcd""cdabef", "cdefab""efabcd", 和 "efcdab" 都是串联子串。 "acdbef" 不是串联子串,因为他不是任何 words 排列的连接。

返回所有串联子串在 s 中的开始索引。你可以以 任意顺序 返回答案。

示例 1:

输入:s = "barfoothefoobarman", words = ["foo","bar"]
输出:[0,9]
解释:因为 words.length == 2 同时 words[i].length == 3,连接的子字符串的长度必须为 6。
子串 "barfoo" 开始位置是 0。它是 words 中以 ["bar","foo"] 顺序排列的连接。
子串 "foobar" 开始位置是 9。它是 words 中以 ["foo","bar"] 顺序排列的连接。
输出顺序无关紧要。返回 [9,0] 也是可以的。

示例 2:

输入:s = "wordgoodgoodgoodbestword", words = ["word","good","best","word"]
输出:[]
解释:因为 words.length == 4 并且 words[i].length == 4,所以串联子串的长度必须为 16。
s 中没有子串长度为 16 并且等于 words 的任何顺序排列的连接。
所以我们返回一个空数组。

示例 3:

输入:s = "barfoofoobarthefoobarman", words = ["bar","foo","the"]
输出:[6,9,12]
解释:因为 words.length == 3 并且 words[i].length == 3,所以串联子串的长度必须为 9。
子串 "foobarthe" 开始位置是 6。它是 words 中以 ["foo","bar","the"] 顺序排列的连接。
子串 "barthefoo" 开始位置是 9。它是 words 中以 ["bar","the","foo"] 顺序排列的连接。
子串 "thefoobar" 开始位置是 12。它是 words 中以 ["the","foo","bar"] 顺序排列的连接。

提示:

  • 1 <= s.length <= 104
  • 1 <= words.length <= 5000
  • 1 <= words[i].length <= 30
  • words[i] 和 s 由小写英文字母组成

通过次数

180.3K

提交次数

458.7K

通过率

39.3%

我们可以:首先计算了单个单词的长度和所有单词的总长度,然后使用一个循环来检查字符串中的每个可能的子串。

它维护两个映射,一个用于跟踪当前子串中的单词计数,另一个用于跟踪目标单词计数。如果这两个映射相等,那么当前子串是一个串联子串,将其起始位置添加到结果列表中来处理。

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;public class Solution {public List<Integer> findSubstring(String s, String[] words) {List<Integer> result = new ArrayList<>();if (s == null || s.length() == 0 || words == null || words.length == 0) {return result;}int wordLength = words[0].length();int totalLength = wordLength * words.length;Map<String, Integer> wordCountMap = new HashMap<>();for (String word : words) {wordCountMap.put(word, wordCountMap.getOrDefault(word, 0) + 1);}for (int i = 0; i <= s.length() - totalLength; i++) {String substring = s.substring(i, i + totalLength);Map<String, Integer> currentWordCountMap = new HashMap<>();for (int j = 0; j < totalLength; j += wordLength) {String word = substring.substring(j, j + wordLength);currentWordCountMap.put(word, currentWordCountMap.getOrDefault(word, 0) + 1);}if (currentWordCountMap.equals(wordCountMap)) {result.add(i);}}return result;}public static void main(String[] args) {Solution solution = new Solution();String s1 = "barfoothefoobarman";String[] words1 = {"foo", "bar"};System.out.println(solution.findSubstring(s1, words1));String s2 = "wordgoodgoodgoodbestword";String[] words2 = {"word", "good", "best", "word"};System.out.println(solution.findSubstring(s2, words2));String s3 = "barfoofoobarthefoobarman";String[] words3 = {"bar", "foo", "the"};System.out.println(solution.findSubstring(s3, words3));}
}

更多推荐

LeetCode【30. 串联所有单词的子串】

本文发布于:2023-11-15 19:33:51,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1605108.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:单词   LeetCode

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!