Skip to content

Commit 92435ea

Browse files
authored
Code bug fix: 2.3 Check if a given string is a isomorphic
The previous solution is not correct, for example "aa" and "bb" are not isomorphic, while the result it true. So I update the code to add extra checking statement. The updated solution can pass the same question at leetcode: https://leetcode.com/problems/isomorphic-strings/
1 parent b4dee8f commit 92435ea

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,12 @@
307307
// If the letter does not exist, create a map and map it to the value
308308
// of the second letter
309309
if (letterMap[letterA] === undefined) {
310-
letterMap[letterA] = letterB;
310+
// If letterB has already been added to letterMap, then we can say: they are not isomorphic.
311+
if(secondString.indexOf(letterB) < i){
312+
return false;
313+
} else {
314+
letterMap[letterA] = letterB;
315+
}
311316
} else if (letterMap[letterA] !== letterB) {
312317
// Eles if letterA already exists in the map, but it does not map to
313318
// letterB, that means that A is mapping to more than one letter.

0 commit comments

Comments
 (0)