https://nsios.tistory.com/118
func solution(_ orders:[String], _ course:[Int]) -> [String] {
var menuSet = [String: Int]()
var result = [String]()
func combination(origin: [Character], n: Int, result: String) {
if result.count > 1 && course.contains(result.count) {
menuSet[result, default: 0] += 1
print(result)
}
for i in n+1..<origin.count {
combination(origin: origin, n: i, result: "\(result)\(origin[i])")
}
}
for order in orders {
let menus = order.map({ $0 }).sorted()
for i in menus.indices {
combination(origin: menus, n: i, result: "\(menus[i])")
}
}
for n in course {
let max = menuSet.filter{ $0.key.count == n && $0.value > 1}.max { $0.value < $1.value }
if let max = max {
menuSet.filter({ $0.key.count == n }).forEach{
if $0.value == max.value {
result.append($0.key)
}
}
}
}
return result
}
func solution(_ orders:[String], _ course:[Int]) -> [String] {
var menuSet = [String: Int]()
var result = [String]()
func combination(total: [String], shouldSelect: Int, current index: Int, selected: [String]) {
if shouldSelect == 0 && selected.count > 1 {
menuSet[selected.joined(), default: 0] += 1
} else if index == total.count {
return
} else {
var newSelected = selected
newSelected.append(total[index])
combination(total: total, shouldSelect: shouldSelect-1, current: index+1, selected: newSelected)
combination(total: total, shouldSelect: shouldSelect, current: index+1, selected: selected)
}
}
let sortedOrderArrs = orders.map({ $0.sorted().map({ String($0) }) })
for order in sortedOrderArrs {
for n in course {
combination(total: order, shouldSelect: n, current: 0, selected: [String]())
}
}
for n in course {
let max = menuSet.filter{ $0.key.count == n && $0.value > 1}.max { $0.value < $1.value }
if let max = max {
menuSet.filter({ $0.key.count == n }).forEach{
if $0.value == max.value {
result.append($0.key)
}
}
}
}
return result
}
import Foundation
func solution(_ orders:[String], _ course:[Int]) -> [String] {
var menuSet = [String: Int]()
var result = [String]()
func combination(total: [String], shouldSelect: Int, current index: Int, selected: [String]) {
if shouldSelect == 0 && selected.count > 1 {
menuSet[selected.joined(), default: 0] += 1
} else if index == total.count {
return
} else {
var newSelected = selected
newSelected.append(total[index])
combination(total: total, shouldSelect: shouldSelect-1, current: index+1, selected: newSelected)
combination(total: total, shouldSelect: shouldSelect, current: index+1, selected: selected)
}
}
let sortedOrderArrs = orders.map({ $0.sorted().map({ String($0) }) })
for order in sortedOrderArrs {
for n in course {
combination(total: order, shouldSelect: n, current: 0, selected: [String]())
}
}
for n in course {
let max = menuSet.filter{ $0.key.count == n && $0.value > 1}.max { $0.value < $1.value }?.value
let maxCom = menuSet.filter({ $0.key.count == n && $0.value == max }).keys.map({ String($0) })
result.append(contentsOf: maxCom)
}
return result.sorted()
}
print(solution(["ABCFG", "AC", "CDE", "ACDE", "BCFG", "ACDEH"], [2,3,4]))
// ["AC", "ACDE", "BCFG", "CDE"]
print(solution(["ABCDE", "AB", "CD", "ADE", "XYZ", "XYZ", "ACD"], [2,3,5]))
// ["ACD", "AD", "ADE", "CD", "XYZ"]
print(solution(["XYZ", "XWY", "WXA"], [2,3,4]))
// ["WX", "XY"]