harrytothemoon / leetcodeAplus

Leetcode meeting note
2 stars 0 forks source link

[1641] Count Sorted Vowel Strings #86

Open harrytothemoon opened 3 years ago

harrytothemoon commented 3 years ago
var countVowelStrings = function(n) {
    let vowelsLength = 5
    let res = 0

    function backtrack(i, arr) {
        if (arr.length === n) {
            res++
            return
        }

        for (let j = i; j < vowelsLength; j++) {
            arr.push(j)
            backtrack(j, arr)
            arr.pop()
        }
    }

    backtrack(0, [])
    return res
};
tsungtingdu commented 3 years ago
var countVowelStrings = function(n) {
    let ans = 0

    function recursion(index, cur) {
        if (cur === n) {
            ans++
        } else {
            for (let i = index; i < 5; i++) {
                recursion(i, cur + 1)
            }
        }
    }
    recursion(0, 0)
    return ans
};