Skip to content

Commit 962ee45

Browse files
committed
SCALA-704 Refactored code after received comment.
1 parent e03fa81 commit 962ee45

File tree

1 file changed

+4
-19
lines changed

1 file changed

+4
-19
lines changed

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

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,34 +2,19 @@ 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(str1, str2) && checkIsomorphic(str2, str1)
96

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

1610
private def checkIsomorphic(str1: String, str2: String): Boolean =
1711
val z = str1.zip(str2)
18-
// for each distinct pair, count the number of occurrences of tup._1 char
1912
val distinctCounts = z.map(tup => {
2013
z.distinct.count(tupZ => tupZ._1 == tup._1)
2114
})
22-
// if all occurrence occur once only
23-
if (distinctCounts.count(_ > 1) == 0)
24-
true
25-
else
26-
false
15+
distinctCounts.count(_ > 1) == 0
2716

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

0 commit comments

Comments
 (0)