-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.scala
executable file
·50 lines (33 loc) · 1.2 KB
/
Solution.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package FindAnagrams
import scala.collection.mutable.HashMap
// https://leetcode.com/problems/find-all-anagrams-in-a-string/description/
object Solution {
def findAnagrams(s: String,
p: String): List[Int] = {
def occMap(str: String, occs: HashMap[Char, Int] = HashMap()): HashMap[Char, Int] = {
if (str.isEmpty) occs
else {
occs.get(str.head) match {
case Some(nr) => occMap(str.tail, occs + (str.head -> (nr + 1)))
case None => occMap(str.tail, occs + (str.head -> 1))
}
}
}
def areOccsEqual(occ1: HashMap[Char, Int], occ2: HashMap[Char, Int]): Boolean = {
occ1.filter(_._2 > 0) == occ2
}
val S_LENGTH: Int = s.length
val P_LENGTH: Int = p.length
val FIRST_SLICE: String = s.slice(0, P_LENGTH)
val sliceMap: HashMap[Char, Int] = occMap(FIRST_SLICE)
val pMap: HashMap[Char, Int] = occMap(p)
var res: List[Int] = List()
if (sliceMap == pMap) res ::= 0
for (i <- 1 to S_LENGTH - P_LENGTH) {
sliceMap.update(s(i - 1), sliceMap.getOrElse(s(i - 1), 1) - 1)
sliceMap.update(s(i + P_LENGTH - 1), sliceMap.getOrElse(s(i + P_LENGTH - 1), 0) + 1)
if (areOccsEqual(sliceMap, pMap)) res ::= i
}
res
}
}