Skip to content

Commit f6199a9

Browse files
authored
Merge pull request #1305 from zeesh49/SCALA-704-Isomorphic-String-ticket-code-refactoring
SCALA-704 Refactored code after received comment.
2 parents d20c61d + 9ae02e5 commit f6199a9

File tree

1 file changed

+10
-19
lines changed

1 file changed

+10
-19
lines changed

scala-core-modules/scala-core-9/src/main/scala/com/baeldung/scala/isomorphic/IsomorphicStringsChecker.scala

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,34 +2,25 @@ package com.baeldung.scala.isomorphic
22

33
object IsomorphicStringsChecker:
44
def checkIsomorphicBothWays(str1: String, str2: String): Boolean =
5-
if (str1.length == str2.length)
6-
checkIsomorphic(str1, str2) && checkIsomorphic(str2, str1)
7-
else
8-
false
5+
(str1.length == str2.length) && checkIsomorphic(
6+
str1,
7+
str2
8+
) && checkIsomorphic(str2, str1)
99

1010
def checkIsomorphic2BothWays(str1: String, str2: String): Boolean =
11-
if (str1.length == str2.length)
12-
checkIsomorphic2(str1, str2) && checkIsomorphic2(str2, str1)
13-
else
14-
false
11+
(str1.length == str2.length) && checkIsomorphic2(
12+
str1,
13+
str2
14+
) && checkIsomorphic2(str2, str1)
1515

1616
private def checkIsomorphic(str1: String, str2: String): Boolean =
1717
val z = str1.zip(str2)
18-
// for each distinct pair, count the number of occurrences of tup._1 char
1918
val distinctCounts = z.map(tup => {
2019
z.distinct.count(tupZ => tupZ._1 == tup._1)
2120
})
22-
// if all occurrence occur once only
23-
if (distinctCounts.count(_ > 1) == 0)
24-
true
25-
else
26-
false
21+
distinctCounts.count(_ > 1) == 0
2722

2823
private def checkIsomorphic2(str1: String, str2: String): Boolean =
2924
val z = str1.zip(str2)
3025
val m = z.groupMap(_._1)(_._2)
31-
// println(m)
32-
if (m.forall(_._2.distinct.length == 1))
33-
true
34-
else
35-
false
26+
m.forall(_._2.distinct.length == 1)

0 commit comments

Comments
 (0)