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) 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 +} + + 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..9a6deacb 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,83 @@ private fun checkUniqueAnswer(store: MutableMap, cache: MutableMap): 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 +} 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..5202631f 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,88 @@ 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]!! + } + } +} + +/** + * 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 + } +