We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 0f9a6fc commit ace1f2dCopy full SHA for ace1f2d
leetcode-scala/src/main/scala/LeetCode75/HashMap_Set/1657. Determine if Two Strings Are Close.sc
@@ -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