Skip to content

Commit 2bb936c

Browse files
authoredApr 4, 2024
Merge pull request #248 from rajeshsantha/feature/from-mac
add IsomorphicStrings
2 parents 660f49a + b8e9e97 commit 2bb936c

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed
 
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.concept.scala.leetcodeGeneralProblems
2+
3+
/** *
4+
* Given two strings s and t, determine if they are isomorphic.
5+
*
6+
* Two strings s and t are isomorphic if the characters in s can be replaced to get t.
7+
*
8+
* 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+
*
10+
*
11+
*
12+
* Example 1:
13+
*
14+
* Input: s = "egg", t = "add"
15+
* Output: true
16+
* Example 2:
17+
*
18+
* Input: s = "foo", t = "bar"
19+
* Output: false
20+
* Example 3:
21+
*
22+
* Input: s = "paper", t = "title"
23+
* Output: true
24+
*
25+
*/
26+
object IsomorphicStrings extends App {
27+
def isIsomorphic1(s1: String, s2: String): Boolean = {
28+
val register = scala.collection.mutable.Map[Char, Char]()
29+
var isIsoMorphicString = false
30+
31+
if (s1.length != s2.length)
32+
isIsoMorphicString = false
33+
else if (s1 == s2)
34+
isIsoMorphicString = true
35+
else {
36+
for (i <- 0 until s1.length) {
37+
if (!register.contains(s1(i))) {
38+
register.put(s1(i), s2(i))
39+
40+
} else {
41+
isIsoMorphicString = register(s1(i)) == s2(i)
42+
}
43+
}
44+
}
45+
isIsoMorphicString
46+
}
47+
48+
}

0 commit comments

Comments
 (0)
Please sign in to comment.