題目
這題看似不難,但第一次的做法錯了,第一次的程式碼如下,想法是用 mapping 組一個新字串 t2,假如跟 t 相同就 return True
Python 3 wrong solution:
1 | def isIsomorphic(self, s: str, t: str) -> bool: |
測出錯誤的 input:
1 | s = "badc" |
錯誤點在於沒有檢查這個條件:No two characters may map to the same character
檢討如下:
- 寫完要 run 之前應該最後 check 一次是否有滿足題目提到的所有條件
- 以這題來說,比較好的做法是一個個 check 每個 character
- 這樣只需跑一次 for-loop,不需要再跑第二次組一個新字串
- 每個 character 都 check 就不會漏掉上面那個條件
修正解法如下
Python 3 solution:
1 | def isIsomorphic(self, s: str, t: str) -> bool: |