Local code runs good, but cannot pass the testcase


  • 0
    Y
    public class WordBreak {
    
    public static void main(String[] args) {
    	WordBreak wb = new WordBreak();
    
    	Set<String> dict = new HashSet<String>();
    	
    	dict.add("a"); 
    	System.out.println(wb.wordBreak("a", dict));
    }
    
    public boolean wordBreak(String s, Set<String> dict) {
    	if (storeMap == null) {
    		initMap(dict);
    	}
    
    	int len = s.length();
    
    	String prefixStr;
    	for (int i = 1; i <= len; i++) {
    		prefixStr = s.substring(0, i);
    
    		if (!storeMap.containsKey(prefixStr)) {
    			continue;
    		}
    
    		if (!wordBreak(s.substring(i), dict)) {
    			continue;
    		}else{
    			return true ; 
    		}
    	}
    	if(len == 0){
    		return true ; 
    	}
    	return storeMap.containsKey(s);
    }
    
    static Map<String, Object> storeMap;
    
    public void initMap(Set<String> dict) {
    
    	storeMap = new HashMap<String, Object>();
    	Iterator it = dict.iterator();
    
    	while (it.hasNext()) {
    		storeMap.put(it.next().toString(), "");
    	}
    
    }}
    

    it cannot pass the testcase ;
    input "a",["a"];
    ouput false;
    expected: true;
    but when i run the main method , the output is true;
    Anyone can help me ?


Log in to reply
 

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