|
276 | 276 | ``` |
277 | 277 | **View on Codepen:** http://codepen.io/kennymkchan/pen/xgNNNB?editors=0012 |
278 | 278 |
|
279 | | - <a name="string--isIsomorphic"></a><a name="2.3"></a> |
280 | | - - **[2.3](#string--palindrome) Check if a given string is a isomorphic** |
281 | | - |
282 | | - ``` |
283 | | - For two strings to be isomorphic, all occurrences of a character in string A can be replaced with another character |
284 | | - to get string B. The order of the characters must be preserved. There must be one-to-one mapping for ever char of |
285 | | - string A to every char of string B. |
286 | | - |
287 | | - `paper` and `title` would return true. |
288 | | - `egg` and `sad` would return false. |
289 | | - `dgg` and `add` would return true. |
290 | | - ``` |
291 | | - ```javascript |
292 | | - isIsomorphic("egg", 'add'); // true |
293 | | - isIsomorphic("paper", 'title'); // true |
294 | | - isIsomorphic("kick", 'side'); // false |
295 | | - |
296 | | - function isIsomorphic(firstString, secondString) { |
297 | | - |
298 | | - // Check if the same lenght. If not, they cannot be isomorphic |
299 | | - if (firstString.length !== secondString.length) return false |
300 | | - |
301 | | - var letterMap = {}; |
302 | | - |
303 | | - for (var i = 0; i < firstString.length; i++) { |
304 | | - var letterA = firstString[i], |
305 | | - letterB = secondString[i]; |
306 | | - |
307 | | - // If the letter does not exist, create a map and map it to the value |
308 | | - // of the second letter |
309 | | - if (letterMap[letterA] === undefined) { |
310 | | - letterMap[letterA] = letterB; |
311 | | - } else if (letterMap[letterA] !== letterB) { |
312 | | - // Eles if letterA already exists in the map, but it does not map to |
313 | | - // letterB, that means that A is mapping to more than one letter. |
314 | | - return false; |
315 | | - } |
| 279 | +<a name="string--isIsomorphic"></a><a name="2.3"></a> |
| 280 | +- **[2.3](#string--palindrome) Check if a given string is a isomorphic** |
| 281 | + |
| 282 | + ``` |
| 283 | + For two strings to be isomorphic, all occurrences of a character in string A can be replaced with another character |
| 284 | + to get string B. The order of the characters must be preserved. There must be one-to-one mapping for ever char of |
| 285 | + string A to every char of string B. |
| 286 | + |
| 287 | + `paper` and `title` would return true. |
| 288 | + `egg` and `sad` would return false. |
| 289 | + `dgg` and `add` would return true. |
| 290 | + ``` |
| 291 | + ```javascript |
| 292 | + isIsomorphic("egg", 'add'); // true |
| 293 | + isIsomorphic("paper", 'title'); // true |
| 294 | + isIsomorphic("kick", 'side'); // false |
| 295 | + |
| 296 | + function isIsomorphic(firstString, secondString) { |
| 297 | + |
| 298 | + // Check if the same lenght. If not, they cannot be isomorphic |
| 299 | + if (firstString.length !== secondString.length) return false |
| 300 | + |
| 301 | + var letterMap = {}; |
| 302 | + |
| 303 | + for (var i = 0; i < firstString.length; i++) { |
| 304 | + var letterA = firstString[i], |
| 305 | + letterB = secondString[i]; |
| 306 | + |
| 307 | + // If the letter does not exist, create a map and map it to the value |
| 308 | + // of the second letter |
| 309 | + if (letterMap[letterA] === undefined) { |
| 310 | + letterMap[letterA] = letterB; |
| 311 | + } else if (letterMap[letterA] !== letterB) { |
| 312 | + // Eles if letterA already exists in the map, but it does not map to |
| 313 | + // letterB, that means that A is mapping to more than one letter. |
| 314 | + return false; |
316 | 315 | } |
317 | | - // If after iterating through and conditions are satisfied, return true. |
318 | | - // They are isomorphic |
319 | | - return true; |
320 | 316 | } |
321 | | - ``` |
322 | | - **View on Codepen:** http://codepen.io/kennymkchan/pen/mRZgaj?editors=0012 |
| 317 | + // If after iterating through and conditions are satisfied, return true. |
| 318 | + // They are isomorphic |
| 319 | + return true; |
| 320 | + } |
| 321 | + ``` |
| 322 | + **View on Codepen:** http://codepen.io/kennymkchan/pen/mRZgaj?editors=0012 |
323 | 323 |
|
324 | 324 | **[⬆ back to top](#table-of-contents)** |
325 | 325 |
|
|
0 commit comments