알고리즘 공부 2020. 8. 24. 23:12

https://leetcode.com/problems/word-break/

 

Word Break - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

/**
 * @param {string} s
 * @param {string[]} wordDict
 * @return {boolean}
 */
var wordBreak = function(s, wordDict) {
    var res=false;
    var map=[];
    for( var i=0; i<s.length; ++i )     map[i] = 0;
    
    var find = function( p_str )
    {
        if( s.indexOf(p_str) != 0)      return false;        
        if( p_str.length > s.length )   return false;        
        
        if( p_str == s )
        {
            res=true;
            return true;
        }
        else
        {
            var result=''
            for( var i=0; i<wordDict.length; ++i )
            {
                if( s.indexOf(p_str+wordDict[i]) >= 0 )
                {                    
                    if( map[p_str+wordDict[i]] == 1 )   return false;
                    else                                result = find( p_str+wordDict[i] );

                    if( result == false )               map[p_str+wordDict[i]] = 1;
                    
                    if( result==true)                   return true;
                    else                                res=false;
                }
            }
            return res;
        }        
    }    
    find('');
    return res;
};
posted by Sense.J
: