i 'm solving the problem. 139. Word Break. but i have met a problem. i don't know why the result of TestCase and the result of Submit are different
below is my code, when i submit my code, it say WrongAnswer for the reason.
Input: "a" ["b"]
Output: true
Expected: false
but when i pressed Run Code, there wasn't problem. I can see Accepted. also in my VScode Editor, no problem. i think my code works properly i don't know why always When submitting code, problem occur.
let Trie = {};
var wordBreak = function (s, wordDict) {
buildTrie(wordDict);
let answer = searchInTrie(s);
return answer;
};
function buildTrie(wordDict) {
for (let i = 0; i < wordDict.length; i++) {
let root = Trie;
const currentWord = wordDict[i];
for (let j = 0; j < currentWord.length; j++) {
if (!root[currentWord[j]]) {
root[currentWord[j]] = {};
}
root = root[currentWord[j]];
}
root["*"] = currentWord;
}
}
function searchInTrie(target) {
let root = Trie;
for (let i = 0; i < target.length; i++) {
const currentChar = target[i];
if (root["*"]) {
root = Trie;
}
// console.log(root, currentChar);
if (!root[currentChar]) {
console.log(root, currentChar);
return false;
}
root = root[currentChar];
}
return true;
}
