I got the exceed time limit error but was able to get it accepted by simply reversing the order


  • 1
    X

    My following recursive solution in java received the ETL error

    public boolean wordBreak(String s, Set<String> dict) {
        if(dict.contains(s))
        {
            return true;
        }
    
        for(String word : dict)
        {
            if(s.startsWith(word))
            {
                if(wordBreak(s.substring(word.length()), dict))
                {
                    return true;
                }
            }
            
        }
        return false;
    }
    

    for failing the test case: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaab", ["a","aa","aaa","aaaa","aaaaa","aaaaaa","aaaaaaa","aaaaaaaa","aaaaaaaaa","aaaaaaaaaa"]

    But I was able to get it to be accepted by simply reversing the order as shown below.

    public boolean wordBreak(String s, Set<String> dict) {
        if(dict.contains(s))
        {
            return true;
        }
    
        for(String word : dict)
        {
            if(s.endsWith(word))
            {
                if(wordBreak(s.substring(0, s.length() - word.length()), dict))
                {
                    return true;
                }
            }
            
        }
        return false;
    }
    

    But this would certainly fail the test case "baaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", ["a","aa","aaa","aaaa","aaaaa","aaaaaa","aaaaaaa","aaaaaaaa","aaaaaaaaa","aaaaaaaaaa"]

    Maybe you want to add that as one of your test cases.


  • 0
    S

    Thanks @xin.lin.33483. We have updated test case. Both solution are TLE now.


Log in to reply
 

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