Substring with Concatenation of All Words Solutions in Go
Number 30
Difficulty Hard
Acceptance 25.4%
Link LeetCode
Other languages C++
Solutions
Go solution by halfrost/LeetCode-Go
package leetcodefunc findSubstring(s string, words []string) []int {if len(words) == 0 {return []int{}}res := []int{}counter := map[string]int{}for _, w := range words {counter[w]++}length, totalLen, tmpCounter := len(words[0]), len(words[0])*len(words), copyMap(counter)for i, start := 0, 0; i < len(s)-length+1 && start < len(s)-length+1; i++ {//fmt.Printf("sub = %v i = %v lenght = %v start = %v tmpCounter = %v totalLen = %v\n", s[i:i+length], i, length, start, tmpCounter, totalLen)if tmpCounter[s[i:i+length]] > 0 {tmpCounter[s[i:i+length]]--//fmt.Printf("******sub = %v i = %v lenght = %v start = %v tmpCounter = %v totalLen = %v\n", s[i:i+length], i, length, start, tmpCounter, totalLen)if checkWords(tmpCounter) && (i+length-start == totalLen) {res = append(res, start)continue}i = i + length - 1} else {start++i = start - 1tmpCounter = copyMap(counter)}}return res}func checkWords(s map[string]int) bool {flag := truefor _, v := range s {if v > 0 {flag = falsebreak}}return flag}func copyMap(s map[string]int) map[string]int {c := map[string]int{}for k, v := range s {c[k] = v}return c}