-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathContents.swift
122 lines (89 loc) · 3.31 KB
/
Contents.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
//: [上一道题](@previous)
/*:
# 最长快乐字符串
- 题号:[5195](https://leetcode-cn.com/problems/longest-happy-string/)
- 题解:[链接](https://leetcode-cn.com/problems/longest-happy-string/solution/er-wei-shu-zu-by-rakuyomo/)
- 难度:中等
- 描述:
如果字符串中不含有任何 `'aaa'`,`'bbb'` 或 `'ccc'` 这样的字符串作为子串,那么该字符串就是一个「快乐字符串」。
给你三个整数 `a`,`b`,`c`,请你返回 任意一个 满足下列全部条件的字符串 `s`:
- `s` 是一个尽可能长的快乐字符串。
- `s` 中 最多 有 `a` 个字母 `'a'`、`b` 个字母 `'b'`、`c` 个字母 `'c'` 。
- `s` 中只含有 `'a'`、`'b'` 、`'c'` 三种字母。
如果不存在这样的字符串 `s` ,请返回一个空字符串 `""`。
*/
//: ## Code
import Foundation
func longestDiverseString(_ a: Int, _ b: Int, _ c: Int) -> String {
if a >= b && a >= c {
if b >= c {
return helper(max: (a, "a"), other: (b, "b"), another: (c, "c"))
} else {
return helper(max: (a, "a"), other: (c, "c"), another: (b, "b"))
}
}
if b >= a && b >= c {
if a >= c {
return helper(max: (b, "b"), other: (a, "a"), another: (c, "c"))
} else {
return helper(max: (b, "b"), other: (c, "c"), another: (a, "a"))
}
}
if c >= a && c >= b {
if a >= b {
return helper(max: (c, "c"), other: (a, "a"), another: (b, "b"))
} else {
return helper(max: (c, "c"), other: (b, "b"), another: (a, "a"))
}
}
return ""
}
func helper(
max: (count: Int, str: String),
other: (count: Int, str: String),
another: (count: Int, str: String)
) -> String {
var arrays: [[String]] = []
var tmp: [String] = []
for _ in 0 ..< max.count {
tmp.append(max.str)
if tmp.count == 2 {
arrays.append(tmp)
tmp = []
}
}
// 处理单数个的情况
if !tmp.isEmpty {
arrays.append(tmp)
}
// other 和 another 共用1个 j,所以放在 `handle(tuple:)` 之外
var j = 0
func handle(tuple: (count: Int, str: String)) {
for _ in 0 ..< tuple.count {
arrays[j].append(tuple.str)
j += 1
if j >= arrays.count {
j = 0
}
}
}
handle(tuple: other)
handle(tuple: another)
// 标记此后出现的长度小于等于2的元素都需要被舍弃掉
var isDiscard = false
return arrays.filter {
// 过滤掉长度小于等于 2 的元素
if $0.count <= 2 && isDiscard == true { return false }
// 标记出现过长度小于等于2个的元素
if $0.count <= 2 { isDiscard = true }
return true
}.map { $0.joined() }.joined()
}
//: ## Test
print(longestDiverseString(1, 8, 12)) // "ccbccbccbbccbbccbbcca"
print(longestDiverseString(1, 1, 7)) // "ccaccbcc"
print(longestDiverseString(0, 0, 0)) // "abc"
print(longestDiverseString(1, 1, 1)) // "abc"
print(longestDiverseString(2, 2, 1)) // "aabbc"
print(longestDiverseString(7, 1, 0))
//: [下一道题](@next)