Back
Posts List

# leetcode205 Isomorphic Strings

Given two strings s and t, determine if they are isomorphic.

Two strings are isomorphic if the characters in s can be replaced to get t.

All occurrences of a character must be replaced with another character while preserving the order of characters. No two characters may map to the same character but a character may map to itself.

Example 1:

##### 209 Word Pattern

python string split() method

solution 1: Two Hash Maps

`pattern`: ‘abba’

`str`: ‘dog cat fish dog’

`pattern_dict` = {a:0, b:1}

`str_dict` = {dog:0, cat:1, fish:2}

solution 2: Single Index Hash Map

`pattern`: ‘abba’

`str`: ‘dog cat fish dog’

`map_index`={ a:0 , dog:0 , b:1 , cat:1 , fish:2}

index of ‘b’ and index of ‘fish’ are NOT the same. Returns `False`.

pattern=”abc”
str=”b c a”

Differentiating between character and string: In Python there is no separate `char` type. And for cases such as:

pattern: ‘abba’ str: ‘b a a b’

Using the same hash map will not work properly. A workaround is to prefix each character in `pattern` with “char_” and each word in `str` with “word_”.

solution 3:

##### 205 Isomorphic Strings

209 和 205 同思路，同方法

Solution 1:

`s`=”foo”

`t`=”bar”

check={ f:true, o:true}

dic={ f:b, o:a}

When i =index2

o and r return false, since o is already in check

solution 3:

python zip() 用法

• 微信扫一扫
• 支付宝扫一扫