i don't know why In Leetcode, the result of TestCase and the result of Submit are different

Viewed 35

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;
}

enter image description here

1 Answers
console.log(root, currentChar);

This statement is adding an extra line of output, which the compiler might consider incorrect. Remove it and then try submitting once.

Related