Javascript Codewars Challenge(isMerge)

后端 未结 4 1382
天涯浪人
天涯浪人 2021-01-29 15:53

I have asked this question before (the first link to my question) but couldn\'t get the complete solution for the question. Write an algorithm to check if a given string, s, can

4条回答
  •  温柔的废话
    2021-01-29 16:37

    function isMerge(s, part1, part2) {
                if (part1.length === 0) {
                    return (s == part2);
                }
                if (part2.length === 0) {
                    return (s == part1);
                }
                if (s.length === 0) {
                    return false;
                } /*because if you are still here, part 1 and part 2 are not empty*/
                /*handle empty strings first both as "base case" and to not have to worry about expressions like s[0]*/
                return ((s[0] == part1[0]) && isMerge(s.substr(1), part1.substr(1), part2)) || ((s[0] == part2[0]) && isMerge(s.substr(1), part1, part2.substr(1)));
            }
            // example 1
            {
                let str = 'codewars'
                let part1 = 'cdw'
                let part2 = 'oears'
                console.log(isMerge(str, part1, part2)) // true
            }
    
            // example 2
            {
                let str = 'codewars'
                let part1 = 'cdb'
                let part2 = 'oears'
                console.log(isMerge(str, part1, part2)) // false
            }
    

提交回复
热议问题