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
2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions contest/src/main/java/com/github/contest/Execute.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.github.contest


import com.github.contest.hashTable.minimumOperations
import com.github.contest.slidingWindow.minWindowOptimumSolution
import java.util.TreeMap


Expand All @@ -11,7 +11,9 @@ import java.util.TreeMap

fun main() {

minimumOperations(intArrayOf(1, 2, 3, 4, 2, 3, 3, 5, 7)).also { println(it) }
val s = "ADOBECODEBANC"
val t = "ABC"
minWindowOptimumSolution(s, t).also { println(it) }
}

fun testing() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.github.contest.slidingWindow


/**
*
*/

fun minWindowOptimumSolution(s: String, t: String): String {
if (s.isEmpty() || t.isEmpty()) return ""

val targetMap = mutableMapOf<Char, Int>()
for (char in t) {
targetMap[char] = targetMap.getOrDefault(char, 0) + 1
}

var left = 0
var right = 0
var minLength = Int.MAX_VALUE
var minStart = 0
val required = targetMap.size
var formed = 0
val windowCounts = mutableMapOf<Char, Int>()

while (right < s.length) {
val char = s[right]
windowCounts[char] = windowCounts.getOrDefault(char, 0) + 1

if (targetMap.containsKey(char) && windowCounts[char] == targetMap[char]) {
formed++
}

while (left <= right && formed == required) {
if (right - left + 1 < minLength) {
minLength = right - left + 1
minStart = left
}

val leftChar = s[left]
windowCounts[leftChar] = windowCounts.getOrDefault(leftChar, 0) - 1
if (targetMap.containsKey(leftChar) && windowCounts[leftChar]!! < targetMap[leftChar]!!) {
formed--
}
left++
}

right++
}

return if (minLength == Int.MAX_VALUE) "" else s.substring(minStart, minStart + minLength)
}
Original file line number Diff line number Diff line change
@@ -1,2 +1,56 @@
package com.github.contest.slidingWindow

/**
* 76. Minimum Window Substring
*/

fun minWindow(s: String, t: String): String {
if (t.length > s.length) return ""
val store = mutableMapOf<Char, Int>()

store.fillMapFromString(t)

for (k in t.length..s.length) {
val cache = mutableMapOf<Char, Int>()
var left = 0
for (right in s.indices) {
val key = s[right]
cache[key] = cache.getOrDefault(key, 0) + 1
if ((right - left) == (k - 1)) {
val isUnique = checkUniqueAnswer(store, cache)
if (isUnique) return s.substring(left, right + 1)
cache.reduceOrRemove(s[left])
left++
}
}
}

return ""
}

private fun MutableMap<Char, Int>.fillMapFromString(str: String) {
for (char in str) this[char] = this.getOrDefault(char, 0) + 1
}

private fun MutableMap<Char, Int>.reduceOrRemove(key: Char) {
this[key] = this.getOrDefault(key, 0) - 1
if (this[key] == 0) this.remove(key)
}

private fun checkUniqueAnswer(store: MutableMap<Char, Int>, cache: MutableMap<Char, Int>): Boolean {
var isUnique = true
for ((char, count) in store) {
if (cache.contains(char)) {
val cacheCount = cache.getOrDefault(char, 0)
if (cacheCount < count) {
isUnique = false
break
}
} else {
isUnique = false
break
}
}

return isUnique
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.github.contest.slidingWindow

/**
*
*/