Scala implementation


  • 0
    Q
    def wordBreak(s: String, wordDict: List[String]): Boolean = {
    	val res = (1 to s.length).foldLeft(List(0)){
    		(acc, i) =>
    			if(acc.exists(x => wordDict.contains(s.substring(x, i)))) //substring is [...)
    				i :: acc
    			else
    				acc
    	}  
    
    	res.head == s.length
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.