11level = " easy"
22name = " isomorphic_strings"
3- tags = [" strings " ]
4- time_to_solve_sec = 280
3+ tags = [" string " ]
4+ time_to_solve_sec = 240
55
66description_en = """
7- Given two strings, determine if they are isomorphic.
7+ Determine if two strings are isomorphic.
88
9- 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.
9+ Two strings are isomorphic if the characters in the first string can be replaced to get the second string . 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.
1010"""
1111
1212description_ru = """
13- Даны две строки, определите изоморфны ли они .
13+ Определите, являются ли две строки изоморфными .
1414
15- Две строки являются изоморфными, если символы в одной строке можно заменить так, чтобы получить другую строку. Все вхождения определенного символа должны быть заменены другим символом, с сохранением порядка символов. Два различных символа не могут быть отражены на один и тот же итоговый, но символ может быть отражен на самого себя.
15+ Две строки являются изоморфными, если символы в первой строке можно заменить так, чтобы получить вторую строку. Все вхождения определенного символа должны быть заменены другим символом, с сохранением порядка символов. Два различных символа не могут быть отражены на один и тот же итоговый, но символ может быть отражен на самого себя.
1616"""
1717
1818limits = """
19- - $0 \\ leq \\ text{len}(s1) \\ leq 1000 $
20- - $\\ text{len}(s1) = \\ text{len}(s2) $
19+ - $1 \\ leq |s_1|, |s_2| \\ leq 100 $
20+ - $|s_1| = |s_2| $
2121"""
2222
2323solution = """
2424def solution(s1: str, s2: str) -> bool:
25- if len(s1) != len(s2):
26- return False
27-
28- def get_pattern(s):
29- mapping = {}
30- pattern = []
31- for c in s:
32- if c not in mapping:
33- mapping[c] = len(mapping)
34- pattern.append(mapping[c])
35- return pattern
36-
37- return get_pattern(s1) == get_pattern(s2)
25+ def pattern(s):
26+ m = {}
27+ return [m.setdefault(c, len(m)) for c in s]
28+ return pattern(s1) == pattern(s2)
3829"""
3930
4031examples = """
41- solution("aa", "bb") == True
42- solution("ca", "ab") == True
4332solution("egg", "add") == True
4433solution("foo", "bar") == False
45- solution("aabb ", "abab ") == False
34+ solution("paper ", "title ") == True
4635"""
4736
4837[[input_signature ]]
@@ -60,27 +49,27 @@ name = "boolean"
6049
6150[[asserts ]]
6251arguments = [" egg" , " add" ]
63- comment = " Classic isomorphic example "
52+ comment = " Both have same pattern "
6453expected = true
6554
6655[[asserts ]]
6756arguments = [" paper" , " title" ]
68- comment = " Word isomorphism "
57+ comment = " Pattern match "
6958expected = true
7059
7160[[asserts ]]
7261arguments = [" ca" , " ab" ]
73- comment = " Simple two letter isomorphism "
62+ comment = " Two char match "
7463expected = true
7564
7665[[asserts ]]
7766arguments = [" aa" , " bb" ]
78- comment = " Same letter repeated "
67+ comment = " Repeated char "
7968expected = true
8069
8170[[asserts ]]
8271arguments = [" aedor" , " eiruw" ]
83- comment = " Five letter isomorphism "
72+ comment = " Five char isomorphic "
8473expected = true
8574
8675[[asserts ]]
@@ -90,120 +79,120 @@ expected = false
9079
9180[[asserts ]]
9281arguments = [" aabb" , " abab" ]
93- comment = " Different pattern "
82+ comment = " Different patterns "
9483expected = false
9584
9685[[asserts ]]
97- arguments = [" " , " " ]
98- comment = " Empty strings"
99- expected = true
100-
101- [[asserts ]]
102- arguments = [" a" , " a" ]
103- comment = " Single same character"
86+ arguments = [" abc" , " def" ]
87+ comment = " All unique same pattern"
10488expected = true
10589
10690[[asserts ]]
107- arguments = [" a " , " b " ]
108- comment = " Single different characters "
91+ arguments = [" abcd " , " abcd " ]
92+ comment = " Identical strings "
10993expected = true
11094
11195[[asserts ]]
112- arguments = [" abc " , " def " ]
113- comment = " Simple sequence "
114- expected = true
96+ arguments = [" ab " , " aa " ]
97+ comment = " Different pattern "
98+ expected = false
11599
116100[[asserts ]]
117- arguments = [" abc " , " abc " ]
118- comment = " Identical strings "
119- expected = true
101+ arguments = [" badc " , " baba " ]
102+ comment = " Not matching "
103+ expected = false
120104
121105[[asserts ]]
122- arguments = [" aba " , " bab " ]
106+ arguments = [" abba " , " cddc " ]
123107comment = " Palindrome pattern"
124108expected = true
125109
126110[[asserts ]]
127- arguments = [" abba " , " cddc " ]
128- comment = " Mirror pattern "
111+ arguments = [" abcabc " , " xyzxyz " ]
112+ comment = " Repeating sequence "
129113expected = true
130114
131115[[asserts ]]
132- arguments = [" abcd " , " abcd " ]
133- comment = " All unique same "
116+ arguments = [" abc " , " bca " ]
117+ comment = " Rotated still isomorphic "
134118expected = true
135119
136120[[asserts ]]
137- arguments = [" abcabc " , " xyzxyz " ]
138- comment = " Repeating pattern "
121+ arguments = [" aba " , " bab " ]
122+ comment = " Simple alternating "
139123expected = true
140124
141125[[asserts ]]
142- arguments = [" aabbcc " , " xxyyzz " ]
143- comment = " Paired pattern "
126+ arguments = [" abab " , " baba " ]
127+ comment = " Alternating patterns "
144128expected = true
145129
146130[[asserts ]]
147- arguments = [" abba " , " abab " ]
148- comment = " Different symmetry "
131+ arguments = [" aaa " , " abc " ]
132+ comment = " All same vs all different "
149133expected = false
150134
151135[[asserts ]]
152- arguments = [" abcdefg " , " hijklmn " ]
153- comment = " All unique mapping "
154- expected = true
136+ arguments = [" abc " , " aaa " ]
137+ comment = " All different vs all same "
138+ expected = false
155139
156140[[asserts ]]
157- arguments = [" aaa " , " bbb " ]
158- comment = " All same repeated "
141+ arguments = [" abcdef " , " uvwxyz " ]
142+ comment = " Six unique characters "
159143expected = true
160144
161145[[asserts ]]
162- arguments = [" abc " , " aaa " ]
163- comment = " Unique to same not valid "
164- expected = false
146+ arguments = [" aabbcc " , " xxyyzz " ]
147+ comment = " Paired pattern "
148+ expected = true
165149
166150[[asserts ]]
167- arguments = [" aaa " , " abc " ]
168- comment = " Same to unique not valid "
151+ arguments = [" hello " , " world " ]
152+ comment = " Different word patterns "
169153expected = false
170154
171155[[asserts ]]
172- arguments = [" ab " , " aa " ]
173- comment = " Two to one mapping invalid "
156+ arguments = [" abcd " , " aaaa " ]
157+ comment = " Unique vs repeated "
174158expected = false
175159
176160[[asserts ]]
177- arguments = [" aa " , " ab " ]
178- comment = " One to two mapping invalid "
179- expected = false
161+ arguments = [" aaaa " , " bbbb " ]
162+ comment = " All same both "
163+ expected = true
180164
181165[[asserts ]]
182- arguments = [" abcabc " , " defdef " ]
183- comment = " Repeating blocks "
166+ arguments = [" abcde " , " abced " ]
167+ comment = " All unique isomorphic "
184168expected = true
185169
186170[[asserts ]]
187- arguments = [" abcabc " , " defghi " ]
188- comment = " Repeat vs unique "
189- expected = false
171+ arguments = [" book " , " deer " ]
172+ comment = " Both have double letters "
173+ expected = true
190174
191175[[asserts ]]
192- arguments = [" hello " , " world " ]
193- comment = " Two l's vs unique "
194- expected = false
176+ arguments = [" title " , " paper " ]
177+ comment = " Reverse of test 2 "
178+ expected = true
195179
196180[[asserts ]]
197- arguments = [" hello " , " abccd " ]
198- comment = " Same double letter pattern"
181+ arguments = [" noon " , " peep " ]
182+ comment = " Palindrome pattern match "
199183expected = true
200184
201185[[asserts ]]
202- arguments = [" badc " , " baba " ]
203- comment = " Different occurrence patterns"
186+ arguments = [" test " , " best " ]
187+ comment = " Different patterns"
204188expected = false
205189
206190[[asserts ]]
207- arguments = [" abcdefghij" , " jihgfedcba" ]
208- comment = " All unique different order"
191+ arguments = [" rat" , " car" ]
192+ comment = " Three letter words"
193+ expected = true
194+
195+ [[asserts ]]
196+ arguments = [" abcabc" , " defdef" ]
197+ comment = " Double pattern"
209198expected = true
0 commit comments