- Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathPermutation-In-String.scala
22 lines (19 loc) · 855 Bytes
/
Permutation-In-String.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
//almost the same as Question 17 , we use exists instead of filter to find the first permutation
objectSolution {
importscala.collection.mutable._
defcheckInclusion(s1: String, s2: String):Boolean= {
valdiffMap=HashMap.empty[Char, Int]
s1.foreach(updateMap(diffMap, _, 1))
(0 until s2.length).toList.exists(index => {
if (index >= s1.length) updateMap(diffMap, s2(index - s1.length), 1)
updateMap(diffMap, s2(index), -1)
diffMap.isEmpty
})
}
defupdateMap(map: HashMap[Char, Int], char: Char, incrementer: Int):Unit=
map.get(char) match {
caseSome(number) if number + incrementer ==0=> map -= char
caseSome(number) => map += (char -> (number + incrementer))
caseNone=> map += (char -> incrementer)
}
}