Leetcode Substring with Concatenation of All Words

本文介绍了一种使用HashMap实现的高效字符串匹配算法,该算法能够在给定字符串中找出由多个指定单词按顺序拼接而成的子串的所有起始位置。通过实例演示了如何利用队列和HashMap来跟踪单词出现的位置,并返回所有符合条件的索引。

You are given a string, s, and a list of words, words, that are all of the same length. Find all starting indices of substring(s) in s that is a concatenation of each word in words exactly once and without any intervening characters.

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

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


Difficulty: Hard

Solution: 

HashMap<String, Queue<Integer>>

public class Solution {
    List<Integer> res = new ArrayList<Integer>();
    String[] wordList;
    HashMap<String, Queue<Integer>> map = new HashMap<String, Queue<Integer>>();
    
    public List<Integer> findSubstring(String s, String[] words) {
        if(words.length == 0) return res;
        int len = words[0].length();
        
        for(int i = 0; i < words.length; i++){
            if(map.containsKey(words[i])){
                map.get(words[i]).offer(-1);
            }
            else{
               Queue<Integer> q = new LinkedList<Integer>();
               q.offer(-1);
               map.put(words[i], q);
            }
        }
        wordList = new String[s.length() - len + 1];
        for(int i = 0; i < wordList.length; i++){
            wordList[i] = s.substring(i, i + len);
        }
        for(int i = 0; i < len; i++){
            int begin = 0, index = i, count = 0;
            while(begin < s.length()){
                // System.out.println(index);
                // System.out.println(index - len * count);
                // System.out.println("---");
                if(words.length == count){
                    //System.out.println("ADD");
                    res.add(index - len * count);
                    count--;
                    continue;
                }
                if((words.length - count) * len > s.length() - index){
                    break;
                }
                String newWord = wordList[index];
                if(!map.containsKey(newWord)){
                    index += len;
                    count = 0;
                    continue;
                }
                if(map.get(newWord).peek() >= index - count * len && map.get(newWord).peek() < index && (index - map.get(newWord).peek()) % len == 0){
                    int temp = map.get(newWord).poll();
                    map.get(newWord).offer(index);
                    count = (index - temp)/len;
                    index += len;
                    continue;
                }
                else{
                    map.get(newWord).poll();
                    map.get(newWord).offer(index);
                    index += len;
                    count++;
                }
            }
            if(words.length == count)
                res.add(index - len * count);
        }
        return res;
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值