-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathIsomorphicStrings.kt
43 lines (39 loc) · 1.42 KB
/
IsomorphicStrings.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package questions
import _utils.UseCommentAsDocumentation
import kotlin.test.assertFalse
import kotlin.test.assertTrue
/**
* Given two strings s and t, determine if they are isomorphic.
* Two strings s and t 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.
*
* [Source](https://leetcode.com/problems/isomorphic-strings/)
*/
@UseCommentAsDocumentation
fun isIsomorphic(s: String, t: String): Boolean {
val translateMap = mutableMapOf<Char, Char>()
val seen = mutableSetOf<Char>()
for (i in 0..s.lastIndex) {
val left = translateMap[s[i]]
// Already seen value in s should be equal in t
if (left != null && left != t[i]) {
return false
}
// badc -> baba
// d has no mapping but b is already mapped. That's why see if b is already "seen"
if (seen.contains(t[i]) && !translateMap.containsKey(s[i])) {
return false
}
seen.add(t[i])
translateMap[s[i]] = t[i]
}
return true
}
fun main() {
assertTrue(isIsomorphic(s = "paper", t = "title"))
assertFalse(isIsomorphic(s = "badc", t = "baba"))
assertTrue(isIsomorphic(s = "egg", t = "add"))
assertFalse(isIsomorphic(s = "foo", t = "bar"))
}