Skip to content

Commit 1671f66

Browse files
Merge pull request #199
add new problems 25.05
2 parents 30e4860 + 2fdee3e commit 1671f66

File tree

3 files changed

+74
-3
lines changed

3 files changed

+74
-3
lines changed

contest/src/main/java/com/github/contest/Execute.kt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
package com.github.contest
22

33

4+
import com.github.contest.hashTable.longestPalindromeAlternativeSolution
45
import com.github.contest.math.numberOfPowerfulInt
56
import com.github.contest.slidingWindow.customStructure.rabinKarpMultiPattern
67
import com.github.contest.slidingWindow.customStructure.slidingWindowClassic
7-
import com.github.contest.slidingWindow.maximumRobots
88
import com.github.contest.strings.fullJustify
99
import com.github.contest.strings.subStrHash
1010
import java.util.TreeMap
@@ -15,7 +15,11 @@ import java.util.TreeMap
1515
*/
1616

1717
fun main() {
18-
maximumRobots(intArrayOf(3, 6, 1, 3, 4), intArrayOf(2, 1, 3, 4, 5), 25).also { println(it) }
18+
19+
val arr = arrayOf("em", "pe", "mp", "ee", "pp", "me", "ep", "em", "em", "me")
20+
longestPalindromeAlternativeSolution(
21+
arr
22+
).also { println(it) }
1923
}
2024

2125

contest/src/main/java/com/github/contest/hashTable/HashTableAlternativeSolution.kt

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,4 +114,32 @@ fun findEvenNumbersAlternativeSolution(digits: IntArray): IntArray {
114114
}
115115

116116
return result.sorted().toIntArray()
117-
}
117+
}
118+
119+
/**
120+
* 2131. Longest Palindrome by Concatenating Two Letter Words
121+
*/
122+
123+
fun longestPalindromeAlternativeSolution(words: Array<String>): Int {
124+
val freq = words.groupingBy { it }.eachCount().toMutableMap()
125+
var len = 0
126+
var centralPair = 0
127+
128+
for ((word, count) in freq) {
129+
if (word[0] == word[1]) {
130+
if (count % 2 == 0) {
131+
len += (count * 2)
132+
} else {
133+
len += (count - 1) * 2
134+
centralPair = 2
135+
}
136+
} else {
137+
val mirror = "${word[1]}${word[0]}"
138+
val minPairs = minOf(count, freq.getOrDefault(mirror, 0))
139+
len += (minPairs * 4)
140+
freq[word] = 0
141+
}
142+
}
143+
144+
return len + centralPair
145+
}

contest/src/main/java/com/github/contest/hashTable/HashTableLeetcode.kt

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -556,3 +556,42 @@ fun findEvenNumbers(digits: IntArray): IntArray {
556556

557557
return result.sorted().toIntArray()
558558
}
559+
560+
/**
561+
* 2131. Longest Palindrome by Concatenating Two Letter Words
562+
*/
563+
564+
fun longestPalindrome(words: Array<String>): Int {
565+
var len = 0
566+
val freq = mutableMapOf<String, Int>()
567+
568+
for (origin in words) {
569+
val reverse = "${origin[1]}${origin[0]}"
570+
571+
when {
572+
origin == reverse -> {
573+
if (freq.contains(origin)) {
574+
len += 4
575+
freq.remove(origin)
576+
} else freq[origin] = freq.getOrDefault(origin, 0) + 1
577+
}
578+
579+
freq.contains(reverse) -> {
580+
freq[reverse] = freq.getOrDefault(reverse, 0) - 1
581+
len += 4
582+
if (freq[reverse] == 0) freq.remove(reverse)
583+
}
584+
585+
else -> freq[origin] = freq.getOrDefault(origin, 0) + 1
586+
}
587+
}
588+
589+
for ((key, value) in freq) {
590+
if ((key[0] == key[1]) && value == 1) {
591+
len += 2
592+
break
593+
}
594+
}
595+
596+
return len
597+
}

0 commit comments

Comments
 (0)