diff --git a/README.md b/README.md index 58a05f626..728cb8896 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/src/main/kotlin/g0601_0700/s0620_not_boring_movies/script.sql b/src/main/kotlin/g0601_0700/s0620_not_boring_movies/script.sql index fa4a301df..3d77c150c 100644 --- a/src/main/kotlin/g0601_0700/s0620_not_boring_movies/script.sql +++ b/src/main/kotlin/g0601_0700/s0620_not_boring_movies/script.sql @@ -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; diff --git a/src/main/kotlin/g1301_1400/s1309_decrypt_string_from_alphabet_to_integer_mapping/Solution.kt b/src/main/kotlin/g1301_1400/s1309_decrypt_string_from_alphabet_to_integer_mapping/Solution.kt index edad2f94e..c8f934973 100644 --- a/src/main/kotlin/g1301_1400/s1309_decrypt_string_from_alphabet_to_integer_mapping/Solution.kt +++ b/src/main/kotlin/g1301_1400/s1309_decrypt_string_from_alphabet_to_integer_mapping/Solution.kt @@ -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 = 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()) } } diff --git a/src/main/kotlin/g1301_1400/s1392_longest_happy_prefix/Solution.kt b/src/main/kotlin/g1301_1400/s1392_longest_happy_prefix/Solution.kt index 7f46c563c..17b226582 100644 --- a/src/main/kotlin/g1301_1400/s1392_longest_happy_prefix/Solution.kt +++ b/src/main/kotlin/g1301_1400/s1392_longest_happy_prefix/Solution.kt @@ -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]) } } diff --git a/src/test/kotlin/g1301_1400/s1392_longest_happy_prefix/SolutionTest.kt b/src/test/kotlin/g1301_1400/s1392_longest_happy_prefix/SolutionTest.kt index 93c047ac2..27ef4f323 100644 --- a/src/test/kotlin/g1301_1400/s1392_longest_happy_prefix/SolutionTest.kt +++ b/src/test/kotlin/g1301_1400/s1392_longest_happy_prefix/SolutionTest.kt @@ -14,4 +14,9 @@ internal class SolutionTest { fun longestPrefix2() { assertThat(Solution().longestPrefix("ababab"), equalTo("abab")) } + + @Test + fun longestPrefix3() { + assertThat(Solution().longestPrefix("babbb"), equalTo("b")) + } }