Sometimes people repeat letters to represent extra feeling. For example:
- "hello" -> "heeellooo"
- "hi" -> "hiiii"
In these strings like "heeellooo", we have groups of adjacent letters that are all the same: "h", "eee", "ll", "ooo".
You are given a string s and an array of query strings words. A query word is stretchy if it can be made to be equal to s by any number of applications of the following extension operation: choose a group consisting of characters c, and add some number of characters c to the group so that the size of the group is three or more.
- For example, starting with "hello", we could do an extension on the group "o" to get "hellooo", but we cannot get "helloo" since the group "oo" has a size less than three. Also, we could do another extension like "ll" -> "lllll" to get "helllllooo". If s = "helllllooo", then the query word "hello" would be stretchy because of these two extension operations: query = "hello" -> "hellooo" -> "helllllooo" = s.
Return the number of query strings that are stretchy.
Example 1:
Input: s = "heeellooo", words = ["hello", "hi", "helo"]
Output: 1
Explanation:
We can extend "e" and "o" in the word "hello" to get "heeellooo".
We can't extend "helo" to get "heeellooo" because the group "ll" is not size 3 or more.
Example 2:
Input: s = "zzzzzyyyyy", words = ["zzyy","zy","zyy"]
Output: 3
Constraints:
- 1 <= s.length, words.length <= 100
- 1 <= words[i].length <= 100
- s and words[i] consist of lowercase letters.
/**
* @param {string} s
* @param {string[]} words
* @return {number}
*/
func expressiveWords(s string, words []string) int {
var convertStrToCharMap = func(str string) map[string][]int {
lastChar := ""
charMap := make(map[string][]int)
for _, char := range strings.Split(str, "") {
if char != lastChar {
charMap[char] = append(charMap[char], 1)
} else {
charMap[char][len(charMap[char])-1]++
}
lastChar = char
}
return charMap
}
inputCharMap := convertStrToCharMap(s)
checkWord := func(charMap map[string][]int, word string) bool {
wordCharMap := convertStrToCharMap(word)
for key := range charMap {
if len(charMap[key]) != len(wordCharMap[key]) {
return false
}
for i := 0; i < len(charMap[key]); i++ {
if charMap[key][i] < wordCharMap[key][i] ||
(charMap[key][i] < 3 && charMap[key][i] != wordCharMap[key][i]) ||
wordCharMap[key][i] == 0 {
return false
}
}
}
return true
}
output := 0
for _, word := range words {
var wg sync.WaitGroup
wg.Add(1)
go func(w string) {
defer wg.Done()
if checkWord(inputCharMap, w) {
output++
}
}(word)
wg.Wait()
}
return output
}
呢題唔識做 寫得好差