Skip to content

Commit be6e46b

Browse files
authored
Merge pull request #154 from HmmCorn/liv
튜플
2 parents 3f610ae + c470bdb commit be6e46b

2 files changed

Lines changed: 99 additions & 0 deletions

File tree

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// 프로그래머스 - 불량 사용자
2+
3+
import Foundation
4+
5+
func solution(_ user_id: [String], _ banned_id: [String]) -> Int {
6+
var matched = [[String]]()
7+
8+
for ban in banned_id {
9+
var list = [String]()
10+
for user in user_id where isMatched(user, ban) {
11+
list.append(user)
12+
}
13+
matched.append(list)
14+
}
15+
16+
var answer = Set<Set<String>>()
17+
18+
func dfs(_ index: Int, _ list: Set<String>) {
19+
if index == matched.count {
20+
answer.insert(list)
21+
return
22+
}
23+
24+
for user in matched[index] where !list.contains(user) {
25+
var list = list
26+
list.insert(user)
27+
dfs(index + 1, list)
28+
}
29+
}
30+
31+
dfs(0, [])
32+
33+
return answer.count
34+
}
35+
36+
func isMatched(_ user: String, _ banned: String) -> Bool {
37+
guard user.count == banned.count else { return false }
38+
39+
for (a, b) in zip(user, banned) {
40+
if b == "*" { continue }
41+
if a != b { return false }
42+
}
43+
44+
return true
45+
}
46+
return true
47+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// 프로그래머스 - 튜플
2+
import Foundation
3+
4+
func solution(_ s: String) -> [Int] {
5+
let sets = getSets(s).sorted { $0.count < $1.count }
6+
var tuple = [Int]()
7+
8+
for group in sets {
9+
for number in group where !tuple.contains(number) {
10+
tuple.append(number)
11+
}
12+
}
13+
return tuple
14+
}
15+
16+
func getSets(_ s: String) -> [[Int]] {
17+
let sets = s.split(separator: "}")
18+
var numbers = [[Int]]()
19+
20+
for s in sets {
21+
let chars = Array(s)
22+
var number = [Int]()
23+
var index = 0
24+
25+
while index < chars.count {
26+
var item = ""
27+
28+
while index < chars.count {
29+
if chars[index].isNumber {
30+
item += String(chars[index])
31+
index += 1
32+
}
33+
else { break }
34+
}
35+
36+
if let word = Int(item) { number.append(word) }
37+
index += 1
38+
}
39+
40+
numbers.append(number)
41+
}
42+
43+
return numbers
44+
}
45+
46+
// func getSets(_ s: String) -> [[Int]] {
47+
// let trimmed = s.replacingOccurrences(of: "{{", with: "")
48+
// .replacingOccurrences(of: "}}", with: "")
49+
// return trimmed.components(separatedBy: "},{").map {
50+
// $0.components(separatedBy: ",").compactMap { Int($0) }
51+
// }
52+
// }

0 commit comments

Comments
 (0)