forked from neetcode-gh/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
0567-permutation-in-string.swift
55 lines (46 loc) · 1.48 KB
/
0567-permutation-in-string.swift
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
51
52
53
54
55
/**
* Question Link: https://leetcode.com/problems/permutation-in-string/
*/
class PermutationInString {
func checkInclusion(_ s1: String, _ s2: String) -> Bool {
if s1.count > s2.count {
return false
}
var s1Array = Array(s1)
var s2Array = Array(s2)
var arr1 = [Int](repeating: 0, count: 26)
var arr2 = [Int](repeating: 0, count: 26)
for i in 0..<s1Array.count {
arr1[Int(s1Array[i].asciiValue! - Character("a").asciiValue!)] += 1
arr2[Int(s2Array[i].asciiValue! - Character("a").asciiValue!)] += 1
}
var matches = 0
for i in 0..<26 {
if arr1[i] == arr2[i] {
matches += 1
}
}
var l = 0
for r in s1Array.count..<s2Array.count {
if matches == 26 {
return true
}
var index = Int(s2Array[r].asciiValue! - Character("a").asciiValue!)
arr2[index] += 1
if arr1[index] == arr2[index] {
matches += 1
} else if arr1[index] + 1 == arr2[index] {
matches -= 1
}
index = Int(s2Array[l].asciiValue! - Character("a").asciiValue!)
arr2[index] -= 1
if arr1[index] == arr2[index] {
matches += 1
} else if arr1[index] - 1 == arr2[index] {
matches -= 1
}
l += 1
}
return matches == 26
}
}