-
Notifications
You must be signed in to change notification settings - Fork 3
/
[프로그래머스] 신재웅_불량 사용자.swift
38 lines (30 loc) · 1.17 KB
/
[프로그래머스] 신재웅_불량 사용자.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
import Foundation
func solution(_ user_id:[String], _ banned_id:[String]) -> Int {
var set: Set<[Int]> = []
let map = banned_id.map{ bannedId in
user_id.enumerated().filter { (index, id) -> Bool in
return isMatched(id: id, pattern: bannedId)
}.map{ $0.offset }
}
dfs(depth: 0, maxDepth: banned_id.count, visited: [], map: map, set: &set)
return set.count
}
func dfs(depth: Int, maxDepth: Int, visited: Set<Int>, map: [[Int]], set: inout Set<[Int]>) {
if depth == maxDepth {
set.update(with: visited.sorted())
return
}
for i in map[depth] {
if visited.contains(i) {
continue
}
var futureVisited = visited
futureVisited.update(with: i)
dfs(depth: depth+1, maxDepth: maxDepth, visited: futureVisited, map: map, set: &set)
}
}
func isMatched(id: String, pattern: String) -> Bool {
let regexString = "^" + pattern.replacingOccurrences(of: "*", with: "[a-zA-Z0-9]") + "$"
let regex = try? NSRegularExpression(pattern: regexString)
return regex?.firstMatch(in: id, options: [], range: NSRange(location: 0, length: id.count)) != nil
}