I tried to use swift to solve an algorithm problem, but it gave me an error
Problem Link: https://leetcode.com/problems/number-of-matching-subsequences/
class Solution {
func bisect_right(arr: [Int], num: Int) -> Int {
var l = 0, r = arr.count - 1
while l < r {
var m = (l+r) / 2
if arr[m] <= num {
l = m + 1
} else {
r = m
}
}
return l
}
func numMatchingSubseq(_ s: String, _ words: [String]) -> Int {
var pos: [Character: [Int]] = [:]
for (i, c) in s.enumerated() {
pos[c]!.append(i)
}
var cnt = 0
for word in words {
var prev = -1, ok = true
for c in word {
let ps = pos[c]!
var res = bisect_right(arr: ps, num: prev)
if res == ps.count {
ok = false; break
}
prev = ps[res]
}
if ok {
cnt += 1
}
}
return cnt
}
}
i.e. s= "abcde" words = ["a","bb","acd","ace"]