From b505a521c317747bc4e63183eec7b520f3927c23 Mon Sep 17 00:00:00 2001 From: Ashwagandha-coder Date: Mon, 28 Apr 2025 19:05:52 +0100 Subject: [PATCH 1/6] add 3392 --- .../slidingWindow/SlidingWindowLeetcode.kt | 23 ++++++++++++++++++- 1 file changed, 22 insertions(+), 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 915681b5..f912ba9a 100644 --- a/contest/src/main/java/com/github/contest/slidingWindow/SlidingWindowLeetcode.kt +++ b/contest/src/main/java/com/github/contest/slidingWindow/SlidingWindowLeetcode.kt @@ -53,4 +53,25 @@ private fun checkUniqueAnswer(store: MutableMap, cache: MutableMap Date: Wed, 30 Apr 2025 11:30:42 +0100 Subject: [PATCH 2/6] add 30 --- .../slidingWindow/SlidingWindowLeetcode.kt | 60 ++++++++++++++++++- 1 file changed, 59 insertions(+), 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 f912ba9a..9a6deacb 100644 --- a/contest/src/main/java/com/github/contest/slidingWindow/SlidingWindowLeetcode.kt +++ b/contest/src/main/java/com/github/contest/slidingWindow/SlidingWindowLeetcode.kt @@ -64,7 +64,7 @@ fun countSubArrays(nums: IntArray): Int { val k = 3 var left = 0 - for (right in 0 until nums.size) { + 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 @@ -75,3 +75,61 @@ fun countSubArrays(nums: IntArray): Int { return count } + +/** + * 30. Substring with Concatenation of All Words + */ + +fun findSubstring(s: String, words: Array): List { + val res = mutableListOf() + val totalLen = words[0].length * words.size + val wordLen = words[0].length + val store = mutableMapOf() + + 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, window: Int): Boolean { + val seen = mutableMapOf() + 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, base: Map): Boolean { + + for ((str, count) in comparable) { + if (!base.contains(str)) return false + else { + val amount = base[str] + if (amount != count) return false + } + } + + return true +} From 0289d5f4594e03e17082fefe01ecbc7952bd6176 Mon Sep 17 00:00:00 2001 From: Ashwagandha-coder Date: Wed, 30 Apr 2025 12:47:21 +0100 Subject: [PATCH 3/6] add prod variants for 30 --- .../slidingWindow/SlidingWindowProdVariant.kt | 76 ++++++++++++++++++- 1 file changed, 74 insertions(+), 2 deletions(-) diff --git a/contest/src/main/java/com/github/contest/slidingWindow/SlidingWindowProdVariant.kt b/contest/src/main/java/com/github/contest/slidingWindow/SlidingWindowProdVariant.kt index 0cac09ab..b7d4c4f5 100644 --- a/contest/src/main/java/com/github/contest/slidingWindow/SlidingWindowProdVariant.kt +++ b/contest/src/main/java/com/github/contest/slidingWindow/SlidingWindowProdVariant.kt @@ -1,5 +1,77 @@ package com.github.contest.slidingWindow /** - * - */ \ No newline at end of file + * 30. Substring with Concatenation of All Words + * Prod Variant + */ + +fun findSubstringProdVariant(s: String, words: Array): List { + val res = mutableListOf() + val totalLen = words[0].length * words.size + val wordLen = words[0].length + val store = mutableMapOf() + + 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, window: Int): Boolean { + val seen = mutableMapOf() + + 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, base: Map): 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): List { + 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() + 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]!! + } + } +} From a4b6d5a49dcf96afa60b206d6cb337e2e3a7edc5 Mon Sep 17 00:00:00 2001 From: Ashwagandha-coder Date: Wed, 30 Apr 2025 12:52:23 +0100 Subject: [PATCH 4/6] add 3392 prod variant --- .../contest/slidingWindow/SlidingWindowProdVariant.kt | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/contest/src/main/java/com/github/contest/slidingWindow/SlidingWindowProdVariant.kt b/contest/src/main/java/com/github/contest/slidingWindow/SlidingWindowProdVariant.kt index b7d4c4f5..5202631f 100644 --- a/contest/src/main/java/com/github/contest/slidingWindow/SlidingWindowProdVariant.kt +++ b/contest/src/main/java/com/github/contest/slidingWindow/SlidingWindowProdVariant.kt @@ -75,3 +75,14 @@ fun findSubstringProdVariantII(s: String, words: Array): List { } } } + +/** + * 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 + } + From bcaeacfe7aa731e3e3ba0a78742db6376803066a Mon Sep 17 00:00:00 2001 From: Danil M Date: Wed, 30 Apr 2025 09:23:29 -0400 Subject: [PATCH 5/6] add 1295 --- .../com/github/contest/math/MathLeetcode.kt | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/contest/src/main/java/com/github/contest/math/MathLeetcode.kt b/contest/src/main/java/com/github/contest/math/MathLeetcode.kt index e018a27f..1603c1b0 100644 --- a/contest/src/main/java/com/github/contest/math/MathLeetcode.kt +++ b/contest/src/main/java/com/github/contest/math/MathLeetcode.kt @@ -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 +} + + From 7adaf0fc592fe0ee372644479e52046620b9dd22 Mon Sep 17 00:00:00 2001 From: Danil M Date: Wed, 30 Apr 2025 09:35:34 -0400 Subject: [PATCH 6/6] change README.md --- README.md | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 139172aa..eb0d9e98 100644 --- a/README.md +++ b/README.md @@ -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)