zoukankan      html  css  js  c++  java
  • Substring with Concatenation of All Words

    问题描述

    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 wordsexactly once and without any intervening characters.

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

    You should return the indices: [0,9].
    (order does not matter).

    解决思路

    最直观的办法:

    1. 用一个boolean[] flag,大小和输入的字符串相同,flag[i]代表s[i...i+lenOfToken]是否在words中;

    2. 计算出拼接字符串的长度,然后依次检查是否完全覆盖words(用一个HashMap记录)。

    时间复杂度为O(n),空间复杂度为O(n).

    注意:words中的word有可能有重复。

    程序

    public 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;
    		}
            
                    // <word, count>
    		HashMap<String, Integer> map = new HashMap<String, Integer>(); 
    		for (String w : words) {
    			if (map.containsKey(w)) {
    				map.put(w, map.get(w) + 1);
    			} else {
    				map.put(w, 1);
    			}
    		}
    
    		int lenOfToken = words[0].length();
    		int numOfToken = words.length;
    		int len = s.length();
    		boolean[] flag = new boolean[len]; // speed up
    
    		int i = 0;
    		while (i + lenOfToken <= len) {
    			String sub = s.substring(i, i + lenOfToken);
    			if (map.containsKey(sub)) {
    				flag[i] = true;
    			}
    			++i;
    		}
    
    		int totalLen = lenOfToken * numOfToken;
    		for (i = 0; i + totalLen <= len; i++) {
    			if (!flag[i]) {
    				continue;
    			}
    			int k = numOfToken;
    			int j = i;
    			HashMap<String, Integer> map_tmp = new HashMap<String, Integer>(map);
    			while (k > 0) {
    				String word_tmp = s.substring(j, j + lenOfToken);
    				if (!flag[j] || !map_tmp.containsKey(word_tmp)
    						|| map_tmp.get(word_tmp) == 0) {
    					break;
    				}
    				map_tmp.put(word_tmp, map_tmp.get(word_tmp) - 1);
    				j += lenOfToken;
    				--k;
    			}
    			if (k == 0) {
    				res.add(i);
    			}
    		}
    
    		return res;
    	}
    }
    
  • 相关阅读:
    oracle参数文件(1)
    提高HTML5 canvas性能的几种方法(转)
    基于TouchVG开发的Windows矢量图形编辑器
    使用rapidjson实现了TouchVG的序列化适配器类
    关于用例的几个问题分析
    重温《UML风格》
    API设计准则(转)
    UML初级培训录音内容
    暂定的UML培训大纲
    基于Android平台多个Icon的APk——实现多程序入口总结(转)
  • 原文地址:https://www.cnblogs.com/harrygogo/p/4722127.html
Copyright © 2011-2022 走看看