forked from neetcode-gh/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path567-Permutation-In-String.kt
More file actions
38 lines (29 loc) · 886 Bytes
/
567-Permutation-In-String.kt
File metadata and controls
38 lines (29 loc) · 886 Bytes
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
class Solution {
fun checkInclusion(s1: String, s2: String): Boolean {
val map = HashMap<Char, Int>()
for (c in s1.toCharArray())
map[c] = map.getOrDefault(c, 0)+1
var count = map.size
var st = 0
for (end in 0..s2.length-1) {
val curr = s2[end]
if (map.containsKey(curr)) {
map[curr] = map[curr]!!-1
if (map[curr] == 0)
count--
}
while (count == 0) {
val temp = s2[st]
if (map.containsKey(temp)) {
map[temp] = map[temp]!! + 1
if (map[temp]!! > 0)
count++
}
if (end - st + 1 == s1.length)
return true
st++
}
}
return false
}
}