Skip to content
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
12 changes: 6 additions & 6 deletions contest/src/main/java/com/github/contest/Execute.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand All @@ -15,10 +15,13 @@ import java.util.TreeMap

fun main() {

val list = mutableListOf<Int>(-1)
println(list)
val str = "abcdefrt"

str.chunked(3).also { println(it) }

}


infix fun Int.myRange(to: Int): IntRange {
return this..to
}
Expand Down Expand Up @@ -96,9 +99,6 @@ fun fullJustifyData() {
}
}

fun subStrHashData() {
subStrHash("xxterzixjqrghqyeketqeynekvqhc", 15, 94, 4, 16).also { println(it) }
}

fun numberOfPowerfulIntData() {
val start = 141L
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -768,7 +768,30 @@ private fun String.eachCount(): MutableMap<Char, Int> {
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 nums.indices) {
product *= nums[right]

while (left <= right && product > k) {
product /= nums[left]
left++
}

count += (right - left + 1)
}

return count
}



Expand Down
60 changes: 37 additions & 23 deletions contest/src/main/java/com/github/contest/strings/StringsLeetcode.kt
Original file line number Diff line number Diff line change
Expand Up @@ -389,39 +389,53 @@ fun lengthOfLastWord(s: String): Int {
}



/**
*
* 2138. Divide a String Into Groups of Size k
*/

fun subStrHash(s: String, power: Int, modulo: Int, k: Int, hashValue: Int): String {
var left = 0
fun divideString(s: String, k: Int, fill: Char): Array<String> {
val res = s.split(k, fill)
return res
}

private fun String.split(delimeterSize: Int, fill: Char): Array<String> {
val res = mutableListOf<String>()
var temp = ""
var count = delimeterSize

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++
for (char in this) {
if (count != 0) {
temp += char
count--
} else {
res.add(temp)
temp = ""
temp += char
count = delimeterSize - 1
}
}

return ""
res.add(temp)
val arr = res.toTypedArray()
remainingFill(arr, fill, delimeterSize)

return arr
}

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++
}
private fun remainingFill(strs: Array<String>, pattern: Char, k: Int) {
var last = strs[strs.size - 1]
var count = 0

return res % modulo.toBigInteger()
if (last.length == k) return
else {
count = last.length
while (count != k) {
last += pattern
count++
}
strs[strs.size - 1] = last
}
}


Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,16 @@ fun smallestStringProdVariant(s: String): String {
* Prod Variant
*/

fun lengthOfLastWordProdVariant(s: String): Int = s.trim().split(" ").lastOrNull()?.length ?: 0
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<String> = when {
s.length % k == 0 -> s.chunked(k).toTypedArray()
else -> s.chunked(k).toMutableList().apply {
this[this.lastIndex] = this.last().padEnd(k, fill)
}.toTypedArray()
}