From 19895a11bfe0caca4ba6de478cd887c3392af94d Mon Sep 17 00:00:00 2001 From: Ashwagandha-coder Date: Sun, 22 Jun 2025 09:50:02 +0100 Subject: [PATCH 1/6] add 2138 --- .../main/java/com/github/contest/Execute.kt | 3 +- .../github/contest/strings/StringsLeetcode.kt | 49 +++++++++++++++++++ 2 files changed, 50 insertions(+), 2 deletions(-) diff --git a/contest/src/main/java/com/github/contest/Execute.kt b/contest/src/main/java/com/github/contest/Execute.kt index b943336c..150ab22e 100644 --- a/contest/src/main/java/com/github/contest/Execute.kt +++ b/contest/src/main/java/com/github/contest/Execute.kt @@ -15,8 +15,7 @@ import java.util.TreeMap fun main() { - val list = mutableListOf(-1) - println(list) + } infix fun Int.myRange(to: Int): IntRange { diff --git a/contest/src/main/java/com/github/contest/strings/StringsLeetcode.kt b/contest/src/main/java/com/github/contest/strings/StringsLeetcode.kt index b4b6f093..0058d9af 100644 --- a/contest/src/main/java/com/github/contest/strings/StringsLeetcode.kt +++ b/contest/src/main/java/com/github/contest/strings/StringsLeetcode.kt @@ -425,3 +425,52 @@ private fun hash(str: String, power: Int, modulo: Int): BigInteger { return res % modulo.toBigInteger() } +/** + * 2138. Divide a String Into Groups of Size k + */ + +fun divideString(s: String, k: Int, fill: Char): Array { + val res = s.split(k, fill) + return res +} + +private fun String.split(delimeterSize: Int, fill: Char): Array { + val res = mutableListOf() + var temp = "" + var count = delimeterSize + + for (char in this) { + if (count != 0) { + temp += char + count-- + } else { + res.add(temp) + temp = "" + temp += char + count = delimeterSize - 1 + } + } + + res.add(temp) + val arr = res.toTypedArray() + remainingFill(arr, fill, delimeterSize) + + return arr +} + +private fun remainingFill(strs: Array, pattern: Char, k: Int) { + var last = strs[strs.size - 1] + var count = 0 + + if (last.length == k) return + else { + count = last.length + while (count != k) { + last += pattern + count++ + } + strs[strs.size - 1] = last + } +} + + From 2fe89b7f9fb04ecbd58ff45691d1b9a9f6c36d0a Mon Sep 17 00:00:00 2001 From: Ashwagandha-coder Date: Sun, 22 Jun 2025 09:50:32 +0100 Subject: [PATCH 2/6] clear old problem --- .../github/contest/strings/StringsLeetcode.kt | 35 ------------------- 1 file changed, 35 deletions(-) diff --git a/contest/src/main/java/com/github/contest/strings/StringsLeetcode.kt b/contest/src/main/java/com/github/contest/strings/StringsLeetcode.kt index 0058d9af..76e5a1bd 100644 --- a/contest/src/main/java/com/github/contest/strings/StringsLeetcode.kt +++ b/contest/src/main/java/com/github/contest/strings/StringsLeetcode.kt @@ -389,41 +389,6 @@ fun lengthOfLastWord(s: String): Int { } -/** - * - */ - -fun subStrHash(s: String, power: Int, modulo: Int, k: Int, hashValue: Int): String { - var left = 0 - var temp = "" - - for (right in s.indices) { - temp += s[right] - if (right - left == k - 1) { - val hash = hash(temp, power, modulo) - if (hash.intValueExact() == hashValue.toBigInteger().intValueExact()) return temp - temp = temp.substring(1, temp.length) - left++ - } - } - - return "" -} - -private fun hash(str: String, power: Int, modulo: Int): BigInteger { - var res = 0.toBigInteger() - var pow = 0 - val power = power.toBigInteger() - for (element in str) { - val index = (element - 'a' + 1).toBigInteger() - val base = (power.pow(pow)) - val calc = index * base - res += calc - pow++ - } - - return res % modulo.toBigInteger() -} /** * 2138. Divide a String Into Groups of Size k From edd5cbc59f9f0c7e4aa8646567cf0f961be9f356 Mon Sep 17 00:00:00 2001 From: Ashwagandha-coder Date: Sun, 22 Jun 2025 09:52:50 +0100 Subject: [PATCH 3/6] reformat code --- contest/src/main/java/com/github/contest/Execute.kt | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/contest/src/main/java/com/github/contest/Execute.kt b/contest/src/main/java/com/github/contest/Execute.kt index 150ab22e..b2f34822 100644 --- a/contest/src/main/java/com/github/contest/Execute.kt +++ b/contest/src/main/java/com/github/contest/Execute.kt @@ -5,7 +5,7 @@ import com.github.contest.math.numberOfPowerfulInt import com.github.contest.slidingWindow.customStructure.rabinKarpMultiPattern import com.github.contest.slidingWindow.customStructure.slidingWindowClassic import com.github.contest.strings.fullJustify -import com.github.contest.strings.subStrHash + import java.util.TreeMap @@ -95,9 +95,7 @@ fun fullJustifyData() { } } -fun subStrHashData() { - subStrHash("xxterzixjqrghqyeketqeynekvqhc", 15, 94, 4, 16).also { println(it) } -} + fun numberOfPowerfulIntData() { val start = 141L From ac0a10dd1d8382b62f3efa5c56c1d373cee422bc Mon Sep 17 00:00:00 2001 From: Ashwagandha-coder Date: Sun, 22 Jun 2025 10:04:49 +0100 Subject: [PATCH 4/6] add 2138 prod variant --- .../src/main/java/com/github/contest/Execute.kt | 5 ++++- .../github/contest/strings/StringsProdVariant.kt | 14 +++++++++++++- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/contest/src/main/java/com/github/contest/Execute.kt b/contest/src/main/java/com/github/contest/Execute.kt index b2f34822..39684a79 100644 --- a/contest/src/main/java/com/github/contest/Execute.kt +++ b/contest/src/main/java/com/github/contest/Execute.kt @@ -15,9 +15,13 @@ import java.util.TreeMap fun main() { + val str = "abcdefrt" + + str.chunked(3).also { println(it) } } + infix fun Int.myRange(to: Int): IntRange { return this..to } @@ -96,7 +100,6 @@ fun fullJustifyData() { } - fun numberOfPowerfulIntData() { val start = 141L val finish = 148L diff --git a/contest/src/main/java/com/github/contest/strings/StringsProdVariant.kt b/contest/src/main/java/com/github/contest/strings/StringsProdVariant.kt index a92aa82a..3e5c103f 100644 --- a/contest/src/main/java/com/github/contest/strings/StringsProdVariant.kt +++ b/contest/src/main/java/com/github/contest/strings/StringsProdVariant.kt @@ -65,4 +65,16 @@ fun smallestStringProdVariant(s: String): String { * Prod Variant */ -fun lengthOfLastWordProdVariant(s: String): Int = s.trim().split(" ").lastOrNull()?.length ?: 0 \ No newline at end of file +fun lengthOfLastWordProdVariant(s: String): Int = s.trim().split(" ").lastOrNull()?.length ?: 0 + +/** + * 2138. Divide a String Into Groups of Size k + * Prod Variant + */ + +fun divideStringProdVariant(s: String, k: Int, fill: Char): Array = when { + s.length % k == 0 -> s.chunked(k).toTypedArray() + else -> s.chunked(k).toMutableList().apply { + this[this.lastIndex] = this.last().padEnd(k, fill) + }.toTypedArray() +} \ No newline at end of file From 159612bbc78914aa95f2f8a77e93f1de7cedafe9 Mon Sep 17 00:00:00 2001 From: Ashwagandha-coder Date: Sun, 22 Jun 2025 12:46:05 +0100 Subject: [PATCH 5/6] add 713 --- .../slidingWindow/SlidingWindowLeetcode.kt | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/contest/src/main/java/com/github/contest/slidingWindow/SlidingWindowLeetcode.kt b/contest/src/main/java/com/github/contest/slidingWindow/SlidingWindowLeetcode.kt index 701eaf1f..3c2ca578 100644 --- a/contest/src/main/java/com/github/contest/slidingWindow/SlidingWindowLeetcode.kt +++ b/contest/src/main/java/com/github/contest/slidingWindow/SlidingWindowLeetcode.kt @@ -768,7 +768,30 @@ private fun String.eachCount(): MutableMap { return count } +/** + * 713. Subarray Product Less Than K + */ + +fun numSubarrayProductLessThanK(nums: IntArray, k: Int): Int = atMostKProduct(nums, k - 1) + +private fun atMostKProduct(nums: IntArray, k: Int): Int { + var left = 0 + var product = 1 + var count = 0 + + for (right in 0 until nums.size) { + product *= nums[right] + while (left <= right && product > k) { + product /= nums[left] + left++ + } + + count += (right - left + 1) + } + + return count +} From 4086e060e2b4fcf5a59fc35b074716cb55ea2182 Mon Sep 17 00:00:00 2001 From: Ashwagandha-coder Date: Sun, 22 Jun 2025 13:14:59 +0100 Subject: [PATCH 6/6] small change code --- .../com/github/contest/slidingWindow/SlidingWindowLeetcode.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/contest/src/main/java/com/github/contest/slidingWindow/SlidingWindowLeetcode.kt b/contest/src/main/java/com/github/contest/slidingWindow/SlidingWindowLeetcode.kt index 3c2ca578..936bae28 100644 --- a/contest/src/main/java/com/github/contest/slidingWindow/SlidingWindowLeetcode.kt +++ b/contest/src/main/java/com/github/contest/slidingWindow/SlidingWindowLeetcode.kt @@ -779,7 +779,7 @@ private fun atMostKProduct(nums: IntArray, k: Int): Int { var product = 1 var count = 0 - for (right in 0 until nums.size) { + for (right in nums.indices) { product *= nums[right] while (left <= right && product > k) {