Spell words
topic
Give you a "Glossary" (array of strings) words and an "alphabet" (string) chars.
If you can spell out words in a "word" (string) with the chars in the "letter" (character),
then we think you've mastered the word.
Note: Each time the spelling, chars each letter can only be used once.
Return vocabulary words in all the words you have mastered the sum of the lengths.
Examples
示例 1:
输入:words = ["cat","bt","hat","tree"], chars = "atach"
输出:6
解释:
可以形成字符串 "cat" 和 "hat",所以答案是 3 + 3 = 6。
示例 2:
输入:words = ["hello","world","leetcode"], chars = "welldonehoneyr"
输出:10
解释:
可以形成字符串 "hello" 和 "world",所以答案是 5 + 5 = 10。
Source: stay button (LeetCode)
link: https: //leetcode-cn.com/problems/find-words-that-can-be-formed-by-charactersM
copyrighted by deduction from all networks. Commercial reprint please contact the authorized official, non-commercial reprint please indicate the source.
Problem-solving ideas
1、
第一层循环
循环 `字符串数组 words`,拿到 `单词字符串 word`
2、
第二层循环开始前,设置一个 `开关标志` ,一开始状态是 `true`
第二层循环
循环 `单词字符串 word` ,拿到 `单字符 w`
3、
第二层循环中
在给出的 `字母表字符串 chars` 中查找是否存在该 `单字符 w`
4、
4、1
如果不存在,则表示 `字母表字符串 chars` 中包含的字符组装不了当前的 `单词字符串 word` ,
`开关标志` 设置成 `false`
直接跳出第二层循环,继续第一层循环逻辑
4、2
如果存在,则去掉当前 `字母表字符串 chars` 中匹配到的字符,继续下一步第二层循环
若直到第二层循环结束,开关标志都没有变化,则表示, `字母表字符串 chars` 包含了当前 `单词字符串 word`
将当前单词字符串 word 的长度保存起来,继续第一层循环逻辑
answer:
let countCharacters = function(words, chars) {
let l = 0
words.forEach(word => {
// 循环 `字符串数组 words`,拿到 `单词字符串 word`
let s = chars, judge = true
for (let w of word) {
// 循环 `单词字符串 word` ,拿到 `单字符 w`
if (s.indexOf(w) === -1){
// 如果不存在,则表示 `字母表字符串 chars` 中包含的字符
// 组装不了当前的 `单词字符串 word`
// `开关标志` 设置成 `false`
// 直接跳出第二层循环,继续第一层循环逻辑
judge = false
break
}
// 如果存在,则去掉当前 `字母表字符串 chars` 中匹配到的字符
// 继续下一步第二层循环
s = s.replace(w,'')
}
// 若直到第二层循环结束,开关标志都没有变化
// 则表示, `字母表字符串 chars` 包含了当前 `单词字符串
if(judge) l += word.length
})
return l
};
Progressive solving the problem solving process
The first submission:
let countCharacters = function(words, chars) {
let arr = []
words.forEach(word => {
let len = word.length
let count = 0
let splitChars = chars.split('')
word.split('').forEach(char => {
let index = splitChars.indexOf(char)
if (index > -1) {
splitChars.splice(index, 1)
count++
}
})
if (count === len) {
arr.push(word)
}
})
let l = 0
arr.forEach(item => {
l += item.length
})
return l
};
Second submission:
let countCharacters = function(words, chars) {
let l = 0
words.forEach(word => {
let len = word.length
let count = 0
let splitChars = chars.split('')
word.split('').forEach(char => {
let index = splitChars.indexOf(char)
if (index > -1) {
splitChars.splice(index, 1)
count++
}
})
if (count === len) {
l += word.length
}
})
return l
};
the third time:
let countCharacters = function(words, chars) {
let l = 0
words.forEach(word => {
let len = word.length
let count = 0
let splitChars = chars
word.split('').forEach(char => {
if (splitChars.indexOf(char) > -1) {
splitChars = splitChars.replace(char, '')
count++
}
})
if (count === len) {
l += word.length
}
})
return l
};
the fourth time:
let countCharacters = function(words, chars) {
let l = 0
words.forEach(word => {
let len = word.length
let count = 0
let splitChars = chars
for (let w of word) {
if (splitChars.indexOf(w) > -1) {
splitChars = splitChars.replace(w, '')
count++
}
}
if (count === len) {
l += word.length
}
})
return l
};
the fifth time:
let countCharacters = function(words, chars) {
let l = 0
words.forEach(word => {
let splitChars = chars
let judge = true
for (let w of word) {
if (splitChars.indexOf(w) > -1) {
splitChars = splitChars.replace(w, '')
} else {
judge = false
break
}
}
if (judge) {
l += word.length
}
})
return l
};
the sixth time:
let countCharacters = function(words, chars) {
let l = 0
words.forEach(word => {
let s = chars, judge = true
for (let w of word) {
if (s.indexOf(w) === -1){
judge = false
break
}
s = s.replace(w,'')
}
if(judge) l += word.length
})
return l
};
2020-03-18 see better stay button Solution:
Note: The final answer draw solution to a problem
/**
* @param {string[]} words
* @param {string} chars
* @return {number}
*/
var countCharacters = function(words, chars) {
let sum = 0
for(let i=0 ; i<words.length; i++){
const w = words[i]
let s = chars, flag = true, j = 0
while( j < w.length ){
if( s.indexOf(w[j]) === -1 ){
flag = false
break
}
s = s.replace(w[j],'')
j++
}
if( flag ) sum += w.length
}
return sum
};