Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit ace1f2d

Browse files
a-khakimova.khakimov
authored and
a.khakimov
committed
1657. Determine if Two Strings Are Close
1 parent 0f9a6fc commit ace1f2d

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
object Solution {
2+
3+
private def occurrences(word: String): Map[Char, Int] = {
4+
word
5+
.zipWithIndex
6+
.groupBy(_._1)
7+
.map { case (k, v) => k -> v.map(_._2).length }
8+
}
9+
10+
def closeStrings(word1: String, word2: String): Boolean = {
11+
val occ1 = occurrences(word1)
12+
val occ2 = occurrences(word2)
13+
(occ1.keys == occ2.keys) && (occ1.values.toList.sorted == occ2.values.toList.sorted)
14+
}
15+
}
16+
17+
Solution.closeStrings("abc", "bca") // true
18+
Solution.closeStrings("a", "aa") // false
19+
Solution.closeStrings("cabbba", "abbccc") // true
20+
Solution.closeStrings("aaabbbbccddeeeeefffff", "aaaaabbcccdddeeeeffff") // false

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /