Skip to content

Commit

Permalink
Kotlin: 567. Permutation in String
Browse files Browse the repository at this point in the history
  • Loading branch information
MaratKhakim committed Sep 3, 2022
1 parent 86f67c2 commit 20a0822
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions kotlin/567-Permutation-In-String.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,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
}
}

0 comments on commit 20a0822

Please sign in to comment.