Why my code can not pass?

  • 0
    public class Solution {
        public boolean wordBreak(String s, Set<String> dict) {
    			return true;
    		else {
    			for(int i = 0; i < s.length(); i++){
    				String sstr = s.substring(0, i);
    					return wordBreak(s.substring(i), dict);
    		return false;

    I wrote a simple recursive function, and it falls in the test case, but I can not identify where is the problem. Could someone help me?

  • 0

    I used the same idea initially and got "Time Limit Exceeded" for one of the cases with a lot of repeated sequences which are all present in the dictionary and just the last character missing. That was causing endless recursive calls, and while it may have eventually finished I realised it was the wrong approach and switched to an iterative version.

Log in to reply

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