-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0030.go
83 lines (75 loc) · 2.2 KB
/
0030.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
// Source: https://leetcode.com/problems/substring-with-concatenation-of-all-words
// Title: Substring with Concatenation of All Words
// Difficulty: Hard
// Author: Mu Yang <http://muyang.pro>
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// You are given a string s and an array of strings words of the same length. Return all starting indices of substring(s) in s that is a concatenation of each word in words exactly once, in any order, and without any intervening characters.
//
// You can return the answer in any order.
//
// 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: []
//
// Example 3:
//
// Input: s = "barfoofoobarthefoobarman", words = ["bar","foo","the"]
// Output: [6,9,12]
//
// Constraints:
//
// 1 <= s.length <= 10^4
// 1 <= words.length <= 5000
// 1 <= words[i].length <= 30
// s and words[i] consist of lowercase English letters.
//
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
package main
func findSubstring(s string, words []string) []int {
wordCount := make(map[string]int, len(words))
for _, word := range words {
wordCount[word]++
}
sLen := len(s)
wLen := len(words[0])
nWords := len(words)
subLen := nWords * wLen
res := make([]int, 0, nWords)
for i := 0; i <= sLen-subLen; i++ {
tmp := _copyMap(wordCount)
ok := true
for j := i; j < i+subLen; j += wLen {
word := s[j : j+wLen]
tmp[word]--
if tmp[word] < 0 {
ok = false
break
}
}
if ok {
res = append(res, i)
}
}
return res
}
func _copyMap(src map[string]int) map[string]int {
dst := make(map[string]int, len(src))
for k, v := range src {
dst[k] = v
}
return dst
}
func main() {
findSubstring(
"lingmindraboofooowingdingbarrwingmonkeypoundcake",
[]string{"fooo", "barr", "wing", "ding", "wing"},
)
}