Skip to content

Improved tasks 620, 1309, 1392 #803

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -498,7 +498,7 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.36'
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
|-|-|-|-|-|-
| 0709 |[To Lower Case](src/main/kotlin/g0701_0800/s0709_to_lower_case/Solution.kt)| Easy | String | 142 | 98.68
| 1309 |[Decrypt String from Alphabet to Integer Mapping](src/main/kotlin/g1301_1400/s1309_decrypt_string_from_alphabet_to_integer_mapping/Solution.kt)| Easy | String | 129 | 95.45
| 1309 |[Decrypt String from Alphabet to Integer Mapping](src/main/kotlin/g1301_1400/s1309_decrypt_string_from_alphabet_to_integer_mapping/Solution.kt)| Easy | String | 0 | 100.00
| 0953 |[Verifying an Alien Dictionary](src/main/kotlin/g0901_1000/s0953_verifying_an_alien_dictionary/Solution.kt)| Easy | Array, String, Hash_Table | 137 | 100.00

#### Day 10 Linked List and Tree
Expand Down
11 changes: 5 additions & 6 deletions src/main/kotlin/g0601_0700/s0620_not_boring_movies/script.sql
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# Write your MySQL query statement below
# #Easy #Database #2023_02_06_Time_305_ms_(59.80%)_Space_0B_(100.00%)
select id, movie, description, rating from Cinema
WHERE mod(id,2) = 1
and
description not LIKE '%boring%'
order by rating DESC;
# #Easy #Database #2025_04_23_Time_259_ms_(64.69%)_Space_0.0_MB_(100.00%)
SELECT id, movie, description, rating
FROM Cinema
WHERE description != 'boring' AND id % 2 != 0
ORDER BY rating DESC;
Original file line number Diff line number Diff line change
@@ -1,51 +1,26 @@
package g1301_1400.s1309_decrypt_string_from_alphabet_to_integer_mapping

// #Easy #String #Programming_Skills_I_Day_9_String
// #2023_06_05_Time_129_ms_(95.45%)_Space_34.8_MB_(63.64%)
// #2025_04_24_Time_0_ms_(100.00%)_Space_40.80_MB_(77.78%)

class Solution {
fun freqAlphabets(s: String): String {
val map: MutableMap<String, String> = HashMap()
map["1"] = "a"
map["2"] = "b"
map["3"] = "c"
map["4"] = "d"
map["5"] = "e"
map["6"] = "f"
map["7"] = "g"
map["8"] = "h"
map["9"] = "i"
map["10#"] = "j"
map["11#"] = "k"
map["12#"] = "l"
map["13#"] = "m"
map["14#"] = "n"
map["15#"] = "o"
map["16#"] = "p"
map["17#"] = "q"
map["18#"] = "r"
map["19#"] = "s"
map["20#"] = "t"
map["21#"] = "u"
map["22#"] = "v"
map["23#"] = "w"
map["24#"] = "x"
map["25#"] = "y"
map["26#"] = "z"
val sb = StringBuilder()
var i = 0
while (i < s.length) {
if ((("" + s[i]).toInt() == 1 || ("" + s[i]).toInt() == 2) &&
i + 1 < s.length && i + 2 < s.length &&
s[i + 2] == '#'
) {
sb.append(map[s.substring(i, i + 3)])
i += 3
val builder = StringBuilder()
var i = s.length - 1
while (i >= 0) {
if (s[i] == '#') {
decryptor(builder, i - 1, i - 2, s)
i -= 3
} else {
sb.append(map["" + s[i]])
i++
val ch = (s[i].code - '0'.code + 96).toChar()
builder.append(ch)
i--
}
}
return sb.toString()
return builder.reverse().toString()
}

private fun decryptor(builder: StringBuilder, a: Int, b: Int, s: String) {
builder.append((((s[b].code - '0'.code) * 10 + s[a].code - '0'.code) + 96).toChar())
}
}
34 changes: 19 additions & 15 deletions src/main/kotlin/g1301_1400/s1392_longest_happy_prefix/Solution.kt
Original file line number Diff line number Diff line change
@@ -1,25 +1,29 @@
package g1301_1400.s1392_longest_happy_prefix

// #Hard #String #Hash_Function #String_Matching #Rolling_Hash
// #2023_06_06_Time_291_ms_(50.00%)_Space_38.1_MB_(100.00%)
// #2025_04_24_Time_7_ms_(100.00%)_Space_47.37_MB_(25.00%)

class Solution {
fun longestPrefix(s: String): String {
val times = 2
var prefixHash: Long = 0
var suffixHash: Long = 0
var multiplier: Long = 1
var len: Long = 0
// use some large prime as a modulo to avoid overflow errors, e.g. 10 ^ 9 + 7.
val mod: Long = 1000000007
for (i in 0 until s.length - 1) {
prefixHash = (prefixHash * times + s[i].code.toLong()) % mod
suffixHash = (multiplier * s[s.length - i - 1].code.toLong() + suffixHash) % mod
if (prefixHash == suffixHash) {
len = i.toLong() + 1
val c = s.toCharArray()
val n = c.size
val a = IntArray(n)
var max = 0
var i = 1
while (i < n) {
if (c[max] == c[i]) {
max++
a[i] = max
i++
} else {
if (max > 0) {
max = a[max - 1]
} else {
a[i] = 0
i++
}
}
multiplier = multiplier * times % mod
}
return s.substring(0, len.toInt())
return s.substring(0, a[n - 1])
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,9 @@ internal class SolutionTest {
fun longestPrefix2() {
assertThat(Solution().longestPrefix("ababab"), equalTo("abab"))
}

@Test
fun longestPrefix3() {
assertThat(Solution().longestPrefix("babbb"), equalTo("b"))
}
}