-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_20220528.kt
295 lines (253 loc) · 8.58 KB
/
_20220528.kt
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
@file:Suppress("ClassName")
package _2022._05
import utils.contentEquals
import utils.runTimedTests
// https://github.com/youngyangyang04/leetcode-master/blob/master/problems/0001.%E4%B8%A4%E6%95%B0%E4%B9%8B%E5%92%8C.md
// https://leetcode.cn/problems/two-sum/
private class Leetcode_1 {
// 57 / 57 test cases passed.
// Status: Accepted
// Runtime: 342 ms
// Memory Usage: 42.2 MB
fun twoSum(nums: IntArray, target: Int): IntArray {
val map = HashMap<Int, Int>()
for (i in nums.indices) {
val complement = target - nums[i]
if (map.containsKey(complement)) {
return intArrayOf(map[complement]!!, i)
}
map[nums[i]] = i
}
return intArrayOf()
}
companion object {
fun test() = Leetcode_1().apply {
twoSum(intArrayOf(2, 7, 11, 15), 9).run {
assert(this.contentEquals(intArrayOf(0, 1)))
}
twoSum(intArrayOf(3, 3), 6).run {
assert(this.contentEquals(intArrayOf(0, 1)))
}
twoSum(intArrayOf(3, 2, 4), 6).run {
assert(this.contentEquals(intArrayOf(1, 2)))
}
}
}
}
// https://leetcode.cn/problems/valid-anagram/
private class Leetcode_242{
companion object {
fun test() = Leetcode_242().run {
val methods = arrayOf(/*this::isAnagram2,*/ this::isAnagram)
methods.forEach {
assert(it.invoke("anagram", "nagaram"))
assert(!it.invoke("anagram", "nagarama"))
assert(!it.invoke("anagramb", "nagarama"))
}
}
}
@Deprecated("fails", level = DeprecationLevel.ERROR)
fun isAnagram2(s: String, t: String): Boolean {
if (s.length != t.length) return false
val map = HashMap<Char, Int>()
for (c1 in s) {
map[c1] = 0
}
for (c2 in t) {
if (map.containsKey(c2)) {
map.remove(c2)
} else {
map.put(c2, 1)
}
}
return map.size == 0
}
// https://leetcode.com/submissions/detail/708280934/
// 36 / 36 test cases passed.
// Status: Accepted
// Runtime: 287 ms
// Memory Usage: 38.5 MB
fun isAnagram(s: String, t: String): Boolean {
if (s.length != t.length) return false
val chars = IntArray(26)
for (c in s) {
val code = c.code - 'a'.code
chars[code] = chars[code]+1
}
for (c in t) {
val code = c.code - 'a'.code
chars[code] = chars[code]-1
}
return !chars.any { it != 0 }
}
// sample 164 ms submission
fun isAnagram_0(s: String, t: String): Boolean {
if (s.length != t.length) return false
val sCount = IntArray(26) { 0 }
val tCount = IntArray(26) { 0 }
for (c in s) {
++sCount[c.toInt() - 97]
}
for (c in t) {
++tCount[c.toInt() - 97]
}
for (i in 0 until 26) {
if (sCount[i] != tCount[i]) return false
}
return true
}
}
// https://leetcode.com/problems/intersection-of-two-arrays/
private class Leetcode_349 {
companion object {
fun test() = Leetcode_349().run {
listOf(this::intersection, this::_intersection).runTimedTests() {
invoke(intArrayOf(1,2,2,1), intArrayOf(2,2)).contentEquals(intArrayOf(2))
invoke(intArrayOf(4,9,5), intArrayOf(9,4,9,8,4)).contentEquals(intArrayOf(4,9))
}
}
}
// https://leetcode.com/submissions/detail/708288985/
// 55 / 55 test cases passed.
// Status: Accepted
// Runtime: 313 ms
// Memory Usage: 38 MB
fun intersection(nums1: IntArray, nums2: IntArray): IntArray {
val map = HashMap<Int, Boolean>()
for (i in nums1) {
map[i] = false
}
for (i in nums2) {
if (map.containsKey(i)) {
map[i] = true
}
}
return map.filter { it.value }.keys.toIntArray()
}
// sample 188 ms submission
fun _intersection(nums1: IntArray, nums2: IntArray): IntArray {
// Using an ordered HashSet (binary insert)
val set = mutableSetOf<Int>()
val list = mutableListOf<Int>()
for (num in nums1) {
set.add(num)
}
for (num in nums2) {
if (set.contains(num)) {
// Do not added twice, consume it
set.remove(num)
//
list.add(num)
}
}
return list.toIntArray()
}
}
// https://leetcode.com/problems/group-anagrams/
private class Leetcode_49 {
companion object {
fun test() = Leetcode_49().apply {
listOf(
this::groupAnagrams,
this::groupAnagrams_0,
this::groupAnagrams_1,
).runTimedTests {
listOf(
invoke(arrayOf("eat","tea","tan","ate","nat","bat")) to listOf(
listOf("bat"), listOf("nat","tan"), listOf("ate","eat","tea")
),
invoke(arrayOf("hhhhu","tttti","tttit","hhhuh","hhuhh","tittt")) to listOf(
listOf("tittt","tttit","tttti"), listOf("hhhhu","hhhuh","hhuhh")
),
invoke(arrayOf("")) to listOf(listOf("")),
invoke(arrayOf("a")) to listOf(listOf("a")),
).forEach {
val result = it.first
val expected = it.second
assert(result.size == expected.size){
"assertion failed, expected size:${expected.size}, actual size:${result.size}"
}
for (r in result) {
assert(expected.any { it.contentEquals(r, true) }) {
"assertion failed on actual:${r}, not in expected: ${expected.joinToString()}"
}
}
}
}
}
}
//https://leetcode.com/submissions/detail/708318321/
// 117 / 117 test cases passed.
// Status: Accepted
// Runtime: 1633 ms
// Memory Usage: 44.1 MB
fun groupAnagrams(strs: Array<String>): List<List<String>> {
val isAnagrams = Leetcode_242()::isAnagram
val map = mutableMapOf<String, MutableList<String>>()
for (i in strs.indices) {
val str = strs[i]
if (map.values.any { it.contains(str) }) continue
map[str] = mutableListOf(str)
for (j in i + 1 until strs.size) {
val check = strs.getOrNull(j) ?: continue
if (isAnagrams.invoke(str, check)) {
map[str]!!.add(check)
}
}
}
return map.values.toList()
}
// sample 264 ms submission
fun groupAnagrams_0(strs: Array<String>): List<List<String>> {
fun getHashValue(s: String): String {
var value = ""
val count = Array(26) { 0 }
for (c in s) {
count[c - 'a']++
}
for (i in 0 until 26) {
if (count[i] != 0) {
value += "${count[i]}" + ('a' + i)
}
}
return value
}
val result = HashMap<String, ArrayList<String>>()
for (str in strs) {
val value = getHashValue(str)
val list = if (!result.contains(value)) {
val temp = ArrayList<String>()
result[value] = temp
temp
} else {
result[value]
}
list?.add(str)
}
return result.values.toList()
}
// https://leetcode.com/submissions/detail/736310459/
// Runtime: 510 ms, faster than 82.55% of Kotlin online submissions for Group Anagrams.
// Memory Usage: 45.4 MB, less than 86.38% of Kotlin online submissions for Group Anagrams.
fun groupAnagrams_1(strs: Array<String>): List<List<String>> {
val map = HashMap<HashMap<Char, Int>, MutableList<String>>()
strs.forEach {
val cMap = HashMap<Char, Int>()
it.forEach {
cMap[it] = (cMap[it] ?: 0) + 1
}
if (map.containsKey(cMap)) {
map[cMap]!! += it
} else {
map[cMap] = mutableListOf(it)
}
}
return map.values.toList()
}
}
fun main() {
// Leetcode_1.test()
// Leetcode_242.test()
// Leetcode_349.test()
Leetcode_49.test()
}