Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Найти анаграмму S в тексте Text
- func findAnagrams(_ text: String, _ s: String) -> [Int] {
- let text: [String] = Array(text).map{String($0)}
- let s: [String] = Array(s).map{String($0)}
- if s.count > text.count {
- return []
- }
- var result: [Int] = []
- var diff: [String: Int] = [:]
- for i in 0..<s.count {
- diff[s[i], default: 0] += 1
- }
- var l = 0
- var r = 0
- while r < text.count {
- if diff[ text[r] ] ?? 0 > 0 {
- diff[ text[r] ]! -= 1
- if r - l + 1 == s.count {
- result.append(l)
- }
- r += 1
- } else if l == r {
- l += 1
- r += 1
- } else {
- diff[ text[l] ]! += 1
- l += 1
- }
- }
- return result
- }
- findAnagrams("baeidbaooo", "ab")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement