[LeetCode] 30. Substring with Concatenation of All Words

Series all the words substring. Is intended to questions given a string s and a word length of some of the same words. Find the starting position of the substring s may be formed in exactly words all words in series form. Note that exactly match the substring to words in a word, there are no intervening other characters, but does not need to consider the order of words in a series of words. example,

Example 1:

Input:
  s = "barfoothefoobarman",
  words = ["foo","bar"]
Output: [0,9]
Explanation: Substrings starting at index 0 and 9 are "barfoo" and "foobar" respectively.
The output order does not matter, returning [9,0] is fine too.

Example 2:

Input:
  s = "wordgoodgoodgoodbestword",
  words = ["word","good","best","word"]
Output: []

The idea is the sliding window (sliding window), but not before applying the template. Note that this problem given conditions, words which each word is of equal length, this condition will make the subject simpler. First create a hashmap all the words inside of words and number of occurrences are recorded. Then start traversing the string s, every move a character, it is necessary to copy once before hashmap, because the idea here is the need to look substring from the beginning of i hashmap contains all the words kept inside (their number). When it finds a substring when the need to determine whether the substring is still hashmap years, the number of times if the substring does not exist or has been 0 of the break, the current i as a starting point to explain the substring is not valid; if this substring exist in the hashmap -, while the number of words have K -, so that when K == 0 can know when all the words are traversed over, this can be the starting position of a substring i added the result set.

Time O (n ^ 2)

Space O (n)

Java implementation

 1 class Solution {
 2     public List<Integer> findSubstring(String s, String[] words) {
 3         // corner case
 4         if (s == null || words == null || words.length == 0) {
 5             return new ArrayList<>();
 6         }
 7 
 8         // normal case
 9         List<Integer> res = new ArrayList<>();
10         int n = words.length;
11         int m = words[0].length();
12         HashMap<String, Integer> map = new HashMap<>();
13         for (String str : words) {
14             map.put(str, map.getOrDefault(str, 0) + 1);
15         }
16 
17         for (int i = 0; i <= s.length() - n * m; i++) {
18             HashMap<String, Integer> copy = new HashMap<>(map);
19             int k = n;
20             int j = i;
21             while (k > 0) {
22                 String str = s.substring(j, j + m);
23                 if (!copy.containsKey(str) || copy.get(str) < 1) {
24                     break;
25                 }
26                 copy.put(str, copy.get(str) - 1);
27                 k--;
28                 j += m;
29             }
30             if (k == 0) {
31                 res.add(i);
32             }
33         }
34         return res;
35     }
36 }

 

JavaScript implementation

 1 /**
 2  * @param {string} s
 3  * @param {string[]} words
 4  * @return {number[]}
 5  */
 6 var findSubstring = function (s, words) {
 7     // corner case
 8     if (s == null || words == null || words.length == 0) {
 9         return [];
10     }
11 
12     // normal case
13     let res = [];
14     let n = words.length;
15     let m = words[0].length;
16     let map = {};
17     for (let str of words) {
18         map[str] = (map[str] || 0) + 1;
19     }
20 
21     for (let i = 0; i <= s.length - n * m; i++) {
22         let copy = { ...map };
23         let k = n;
24         let j = i;
25         while (k > 0) {
26             let str = s.substring(j, j + m);
27             if (!copy[str] || copy[str] < 1) {
28                 break;
29             }
30             copy[str]--;
31             k--;
32             j += m;
33         }
34         if (k == 0) {
35             res.push(i);
36         }
37     }
38     return res;
39 };

 

Guess you like

Origin www.cnblogs.com/aaronliu1991/p/12630245.html