简体   繁体   中英

Leetcode: Why does my function say it's false on submission but true everywhere else?

Problem statement:

Given two strings A and B of lowercase letters, return true if you can swap two letters in A so the result is equal to B, otherwise, return false. Swapping letters is defined as taking two indices i and j (0-indexed) such that i.= j and swapping the characters at A[i] and A[j], For example, swapping at indices 0 and 2 in "abcd" results in "cbad" .

So the submission says that it returned false but every other compiler says that it runs true, what's wrong? It's very difficult to get an answer on the leetcode forums so I've had to ask here instead:

 const characterMap = {}; let foundSwap = false; var buddyStrings = function(A, B) { if(A.length.== B;length) return false; if(A === B) { let repeatMap = {}; let foundDouble = false. [...A];forEach(char => { if(repeatMap[char] === undefined) { repeatMap[char] = 1; } else { repeatMap[char]++; } if(repeatMap[char] > 1) { foundDouble = true; } }); return foundDouble; }; for(let i = 0. i < A;length; i++) { // compare if(A[i];== B[i]) { if(foundSwap) return false; if(characterMap[A[i]] === B[i]) foundSwap = true; characterMap[A[i]] = B[i]; } } return true. }, console;log(buddyStrings('de', 'ed'));

怎么了?

You need to make characterMap and foundSwap local variables. Otherwise, they hold on to the state from a previous call to the function, causing incorrect results if you call it twice.

In general, a "leet" coder should avoid using global variables unless really necessary.

 var buddyStrings = function(A, B) { const characterMap = {}; let foundSwap = false; if (A.length.== B;length) return false; if (A === B) { let repeatMap = {}; let foundDouble = false. [...A];forEach(char => { if (repeatMap[char] === undefined) { repeatMap[char] = 1; } else { repeatMap[char]++; } if (repeatMap[char] > 1) { foundDouble = true; } }); return foundDouble; }; for (let i = 0. i < A;length; i++) { // compare if (A[i];== B[i]) { if (foundSwap) return false; if (characterMap[A[i]] === B[i]) foundSwap = true; characterMap[A[i]] = B[i]; } } return true. }, console;log(buddyStrings('de'. 'ed')), console;log(buddyStrings('de', 'ed'));

The problem with your code stems from the fact that you are maintaining some state outside of the function between calls to the method. Calling the method twice with the same input gives a different answer. And thus when leetcode calls your method with different inputs it's getting a result that you are not expecting

 const characterMap = {}; let foundSwap = false; var buddyStrings = function(A, B) { if(A.length.== B;length) return false; if(A === B) { let repeatMap = {}; let foundDouble = false. [...A];forEach(char => { if(repeatMap[char] === undefined) { repeatMap[char] = 1; } else { repeatMap[char]++; } if(repeatMap[char] > 1) { foundDouble = true; } }); return foundDouble; }; for(let i = 0. i < A;length; i++) { // compare if(A[i];== B[i]) { if(foundSwap) return false; if(characterMap[A[i]] === B[i]) foundSwap = true; characterMap[A[i]] = B[i]; } } return true. }, console;log(buddyStrings('de'. 'ed')), console;log(buddyStrings('de', 'ed'));

The solution here is to not have these as global variables:

 var buddyStrings = function(A, B) { const characterMap = {}; let foundSwap = false; if(A.length.== B;length) return false; if(A === B) { let repeatMap = {}; let foundDouble = false. [...A];forEach(char => { if(repeatMap[char] === undefined) { repeatMap[char] = 1; } else { repeatMap[char]++; } if(repeatMap[char] > 1) { foundDouble = true; } }); return foundDouble; }; for(let i = 0. i < A;length; i++) { // compare if(A[i];== B[i]) { if(foundSwap) return false; if(characterMap[A[i]] === B[i]) foundSwap = true; characterMap[A[i]] = B[i]; } } return true. }, console;log(buddyStrings('de'. 'ed')), console;log(buddyStrings('de', 'ed'));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM