Leetcode30--->Substring with Concatenation of All Words(主串中找出连接给定所有单词的子串的位置)

2023-05-13,,

题目:给定一个字符串S(主串),一个字符串数组words,其中的字符串的长度相同。找到所有的子串位置,要求是words中字符串的一个连接;

举例:

For example, given:
s: "barfoothefoobarman"
words: ["foo", "bar"]

You should return the indices: [0,9].

解题思路:

1. 采用窗口机制,假设此时每个单词的长度为wordlen;

2.   先将words中的单词存储在hashmap中,key为单词,value为单词出现的次数;

3. 在S中以单词长遍历每个单词,看其在hashmap中是否出现,若出现,则窗口的左边界即可确定,然后依次向后遍历,若其中有单词不出现在hashmap中,则直接看下一个单词,窗口的左边界也会更新。若从窗口的左边界遍历找到了与words中单词相拼接的字符串,则将左窗口位置加入到结果集中,左窗口移向下一个单词。

代码如下:

 public class Solution {
public List<Integer> findSubstring(String s, String[] words) {
List<Integer> list = new ArrayList<Integer>();
if(s == null || words == null || s.length() < 1 || words.length < 1)
return list;
HashMap<String, Integer> hm = new HashMap<String, Integer>();
for(int i = 0, len = words.length; i < len; i++) // 将数组中的单词放入到hashmap中,由于数组中有可能有多个相同的单词,所以还需要计数
{
if(hm.containsKey(words[i]))
hm.put(words[i], hm.get(words[i]) + 1);
else
hm.put(words[i], 1);
}
int wordlen = words[0].length(); // 单词的长度
int strlen = words.length; // 单词所组成串的长度
int i = 0; // 循环的次数
int len = s.length();
while(i < wordlen)
{
int left = i; // 窗口的左边界
int count = 0; // 匹配了hm中的单词数目
HashMap<String, Integer> curr = new HashMap<String, Integer>(); // 记录窗口中已经匹配的单词及其出现的次数
for(int j = i; j <= len - wordlen; j = j + wordlen)
{
String str = s.substring(j, j + wordlen); // 取一个单词
if(hm.containsKey(str)) // 如果字典中包含该单词
{
if(curr.containsKey(str)) // 将单词加入到当前遍历的字典中
curr.put(str, curr.get(str) + 1);
else
curr.put(str, 1);
if(hm.get(str) >= curr.get(str)) // str在主串中出现的次数不能小于当前窗口的str出现的次数,否则窗口就要缩小
count ++;
else
{
while(hm.get(str) < curr.get(str)) // 如果当前窗口的单词出现次数大于给定的数组中的单词次数,窗口需要缩小
{
String temp = s.substring(left, left + wordlen);
if(curr.containsKey(temp))
{
curr.put(temp, curr.get(temp) - 1);
if(curr.get(temp) < hm.get(temp))
count--;
}
left += wordlen;
}
}
if(count == strlen) // 如果此时curr中所保存的单词数量与给定的words中的单词数目是一样的,则将当前窗口的左边缘加入结果
{
list.add(left);
String ss = s.substring(left, left + wordlen);
if(curr.containsKey(ss))
{
curr.put(ss, curr.get(ss) - 1);
count -- ;
}
left += wordlen;
}
}
else // 如果字典中不包含该单词,则直接看下一个单词
{
left = j + wordlen;
count = 0;
curr.clear();
}
}
i ++;
}
return list;
}
}

Leetcode30--->Substring with Concatenation of All Words(主串中找出连接给定所有单词的子串的位置)的相关教程结束。

《Leetcode30--->Substring with Concatenation of All Words(主串中找出连接给定所有单词的子串的位置).doc》

下载本文的Word格式文档,以方便收藏与打印。