I got the EXCEED TIME LIMIT error but it is OK running on my IDE


  • 0
    W

    Here is my Java code.

    public class Solution {
    
    	public boolean wordBreak(String s, Set<String> dict) {
    		List<String> list = new ArrayList<String>();
    		List<String> temp = new ArrayList<String>();
    		list.add(s);
    		while (!list.isEmpty()) {
    			for (int i = 0; i < list.size(); i++) {
    				String remain = list.get(i);
    				if (remain.isEmpty())
    					return true;
    
    				Iterator<String> iter = dict.iterator();
    				while (iter.hasNext()) {
    					String word = iter.next();
    					if (remain.startsWith(word))
    						temp.add(remain.replaceFirst(word, ""));
    				}
    			}
    			list = temp;
    			temp = new ArrayList<String>();
    
    			for (int i = 0; i < list.size(); i++) {
    				for (int j = i; j < list.size(); j++) {
    					if (list.get(i).startsWith(list.get(j)))
    						list.remove(j);
    				}
    			}
    		}
    
    		return false;
    	}
    }
    

    I got An "Exceed Time Limit" when running the following test case, but it costs only 2 milliseconds running on my IDE:
    "baaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", ["a","aa","aaa","aaaa","aaaaa","aaaaaa","aaaaaaa","aaaaaaaa","aaaaaaaaa","aaaaaaaaaa"]

    Can anyone tell me WHY?


  • 0
    W

    what confuses me is that, when I put the following code before my original code, i got "Exceed Time Limit" error.

    if (s.equalsIgnoreCase("baaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"))
    return false;


Log in to reply
 

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