Different Answer On OJ and Local Machine-JAVA


  • 0
    Y

    Get different answer On OJ and Local

    import java.util.ArrayList;
    import java.util.List;
    import java.util.Set;
    public class Solution {
        public boolean wordBreak(String s, Set<String> dict) {
            boolean retVal = false;
            if(s!=null&&dict.size()>0){
            boolean[] boolArray = new boolean[dict.size()];
            List<String> dictList = new ArrayList<String>();
            dictList.addAll(dict);
            
            boolean loopFlag = true;
            while(loopFlag){
            	int priorLength = s.trim().length();
            for(int i = 0;i<boolArray.length;i++){
            	if(!boolArray[i]){
            		if(s.indexOf(dictList.get(i))>=0){
            			s = s.replaceFirst(dictList.get(i), " ");
            		}else{
            			boolArray[i] = true;
            		}
            		}
            }
            if(s.trim().length()==priorLength){
            	loopFlag = false;
            }
            if(s.trim().length()==0){
            	retVal=true;
            	loopFlag = false;
            }
            }
        }
            return retVal;
        }
    }
    

    Get stuck with the following test case:
    "bccdbacdbdacddabbaaaadababadad" {"cbc","bcda","adb","ddca","bad","bbb","dad","dac","ba","aa","bd","abab","bb","dbda","cb","caccc","d","dd","aadb","cc","b","bcc","bcd","cd","cbca","bbd","ddd","dabb","ab","acd","a","bbcc","cdcbd","cada","dbca","ac","abacd","cba","cdb","dbac","aada","cdcda","cdc","dbc","dbcb","bdb","ddbdd","cadaa","ddbc","babb"};

    I get true locally ,but OJ get false.


Log in to reply
 

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