Javascript Codewars Challenge(isMerge)

后端 未结 4 1381
天涯浪人
天涯浪人 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:21

    Here is a non-recursive approach.

    function checkIt (str, str1, str2){
        if(str.length !== str1.length + str2.length){
            return false;
        }
    
        var str1_index = -1
        var str2_index = -1;
        var curr_index1, curr_index2;
    
        for(var i=0; i str1_index){
                str1_index = curr_index1;
            }
            else if(curr_index1 !== -1 && curr_index1 < str1_index) {
                return false;
            }
    
            if(curr_index2 > str2_index){
                str2_index = curr_index2;
            }
            else if(curr_index2 !== -1 && curr_index2 < str2_index) {
                return false;
            }   
        }
        return true;
    }
    
    
    console.log(checkIt('codewars', 'cdw', 'oears')); // true
    console.log(checkIt('codewars', 'cdw', 'ears'));  // false
    console.log(checkIt('codewars', 'cwd', 'oears')); // false
    console.log(checkIt('codewars', 'cdw', 'oeasr')); // false
    

提交回复
热议问题