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
20 changes: 16 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,20 @@
# solution algorithm problems
# Contest Kotlin

![Kotlin](https://img.shields.io/badge/kotlin-%237F52FF.svg?style=for-the-badge&logo=kotlin&logoColor=white)
![Android Studio](https://img.shields.io/badge/Android%20Studio-3DDC84.svg?style=for-the-badge&logo=android-studio&logoColor=white)

## Task
# Description

This repository contains problems of leetcode. We might use this repo how get solution for define
problem from leetcode. Also We might clone this repo and run or debug problems in android studio.

# How to use

1. Each problem will be in contest module
2. Each problem have a few solutions. Main solution with using down level code. Alternative Solution
with other approaches. Prod Variant - this code might be in prod in application or service. This
code using std lib kotlin
3. For search each problem have kotlin doc with name and number problem. Also each problem have tag
for commit for search in github.
4. Each Topic have package which contains problem

- [leetcode task](app/src/main/java/com/leetcode_kotlin/AlgorithmLeetcode.kt)
- [codeWars task](app/src/main/java/com/leetcode_kotlin/AlgorithmCodeWars.kt)
23 changes: 23 additions & 0 deletions contest/src/main/java/com/github/contest/math/MathLeetcode.kt
Original file line number Diff line number Diff line change
Expand Up @@ -191,3 +191,26 @@ fun numberOfPowerfulInt(start: Long, finish: Long, limit: Int, s: String): Long

return if (s.toLong() in start..finish) counter + 1L else counter
}

/**
* 1295. Find Numbers with Even Number of Digits
*/

val POW = intArrayOf(100_000, 10_000, 1_000, 100, 10, 1)

fun findNumbers(nums: IntArray): Int = nums.filter {
isEven(it)
}.count()

private fun isEven(num: Int): Boolean {
var even = true

for (p in POW) {
if (num >= p) break
even = !even
}

return even
}


Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,83 @@ private fun checkUniqueAnswer(store: MutableMap<Char, Int>, cache: MutableMap<Ch
}

return isUnique
}
}

/**
* 3392. Count Subarrays of Length Three With a Condition
*/

fun countSubArrays(nums: IntArray): Int {
var count = 0
val k = 3
var left = 0

for (right in nums.indices) {
if (right - left == k - 1) {
val sum = (nums[left] + nums[right]).toDouble()
val middle = nums[left + 1].toDouble() / 2.0
if (sum == middle) count++
left++
}
}

return count
}

/**
* 30. Substring with Concatenation of All Words
*/

fun findSubstring(s: String, words: Array<String>): List<Int> {
val res = mutableListOf<Int>()
val totalLen = words[0].length * words.size
val wordLen = words[0].length
val store = mutableMapOf<String, Int>()

if (totalLen > s.length) return res

for (word in words) store[word] = store.getOrDefault(word, 0) + 1

var left = 0

for (right in s.indices) {
if (right - left == totalLen - 1) {
val str = s.substring(left, right + 1)
if (isValidWord(str, store, wordLen)) res.add(left)
left++
}
}

return res
}

private fun isValidWord(s: String, map: Map<String, Int>, window: Int): Boolean {
val seen = mutableMapOf<String, Int>()
var left = 0

for (right in s.indices) {
if (right - left == window - 1) {
val str = s.substring(left, right + 1)
seen[str] = seen.getOrDefault(str, 0) + 1
left = right + 1
}
}

return when {
equalsMap(seen, map) -> true
else -> false
}
}

private fun equalsMap(comparable: Map<String, Int>, base: Map<String, Int>): Boolean {

for ((str, count) in comparable) {
if (!base.contains(str)) return false
else {
val amount = base[str]
if (amount != count) return false
}
}

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

/**
*
*/
* 30. Substring with Concatenation of All Words
* Prod Variant
*/

fun findSubstringProdVariant(s: String, words: Array<String>): List<Int> {
val res = mutableListOf<Int>()
val totalLen = words[0].length * words.size
val wordLen = words[0].length
val store = mutableMapOf<String, Int>()

if (totalLen > s.length) return res

for (word in words) store[word] = store.getOrDefault(word, 0) + 1

var left = 0

for (right in s.indices) {
if (right - left == totalLen - 1) {
val str = s.substring(left, right + 1)
if (isValidWord(str, store, wordLen)) res.add(left)
left++
}
}

return res
}

private fun isValidWord(s: String, map: Map<String, Int>, window: Int): Boolean {
val seen = mutableMapOf<String, Int>()

s.windowed(window, window) {
seen[it.toString()] = seen.getOrDefault(it, 0) + 1
}

return when {
equalsMap(seen, map) -> true
else -> false
}
}

private fun equalsMap(comparable: Map<String, Int>, base: Map<String, Int>): Boolean {

for ((str, count) in comparable) {
when {
!base.contains(str) || base[str] != count -> return false
else -> continue
}
}

return true
}


/**
* Prod Variant with More Kotlin Sugar
*/

fun findSubstringProdVariantII(s: String, words: Array<String>): List<Int> {
val wordLength = words.first().length
val totalLength = wordLength * words.size

if (s.length < totalLength) return listOf()

val wordCount = words.groupingBy { it }.eachCount()


return (0..s.length - totalLength).filter { start ->
val seen = mutableMapOf<String, Int>()
words.indices.all { i ->
val word = s.substring(start + i * wordLength, start + (i + 1) * wordLength)
wordCount.containsKey(word) && seen.merge(word, 1, Int::plus)!! <= wordCount[word]!!
}
}
}

/**
* 3392. Count Subarrays of Length Three With a Condition
* Prod Variant
*/

fun countSubArraysProdVariant(nums: IntArray): Int =
nums.toList().windowed(3).count {
(it.first() + it.last()).toDouble() == it[1].toDouble() / 2.0
}