LeetCode --- --- deletrear la serie de la cadena palabra

palabras de hechizo

título

darle una cuerda) (caracteres palabras "Glosario" (matriz de cadenas) y un "alfabeto".

Si usted puede escribir palabras en una "palabra" (cadena) los caracteres de la "letra" (carácter),

entonces pensamos que ha dominado la palabra.


Nota: Cada vez que el deletreo, char de cada letra sólo puede ser usado una vez.
Regresar palabras de vocabulario en todas las palabras que ha dominado la suma de las longitudes.


Ejemplos

示例 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。

Fuente: Botón de estancia (LeetCode)

enlace: https: //leetcode-cn.com/problems/find-words-that-can-be-formed-by-charactersM

propiedad de la deducción de todas las redes. reimpresión comercial póngase en contacto con la reimpresión autorizada oficial, no comercial por favor indique la fuente.


Ideas de resolución de problemas

1、
第一层循环
循环 `字符串数组 words`,拿到 `单词字符串 word`
2、
第二层循环开始前,设置一个 `开关标志` ,一开始状态是 `true`
第二层循环
循环 `单词字符串 word` ,拿到 `单字符 w`
3、
第二层循环中
在给出的 `字母表字符串 chars` 中查找是否存在该 `单字符 w`
4、
4、1
如果不存在,则表示 `字母表字符串 chars` 中包含的字符组装不了当前的 `单词字符串 word` ,
`开关标志` 设置成 `false`
直接跳出第二层循环,继续第一层循环逻辑
4、2
如果存在,则去掉当前 `字母表字符串 chars` 中匹配到的字符,继续下一步第二层循环
若直到第二层循环结束,开关标志都没有变化,则表示, `字母表字符串 chars` 包含了当前 `单词字符串 word`
将当前单词字符串 word 的长度保存起来,继续第一层循环逻辑


solución:

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
};


Progresiva resolver el proceso de resolución de problemas

La primera presentación:

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
};

Segunda presentación:

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
};

En tercer lugar:

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
};

cuarto:

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
};

quinto:

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
};

sexto:

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
};



18/03/2020 ver mejor botón de estancia Solución:

Nota: La solución final empate respuesta a un problema

/**
 * @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
};

Supongo que te gusta

Origin www.cnblogs.com/linjunfu/p/12517333.html
Recomendado
Clasificación