Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
5821c4e
add 54
Ashwagandha-coder Aug 4, 2025
c8ed48d
add 563 problem
Ashwagandha-coder Sep 10, 2025
1e04fa1
add 3541
Ashwagandha-coder Sep 13, 2025
30731d8
add 3541 prod variant
Ashwagandha-coder Sep 13, 2025
eb1cb6d
add 3541 prod variant II
Ashwagandha-coder Sep 15, 2025
6c6d10b
add 993 problem
Ashwagandha-coder Sep 15, 2025
6eba473
add 993 prod variant
Ashwagandha-coder Sep 15, 2025
45b1404
add 1935 prod variant
Ashwagandha-coder Sep 15, 2025
190ed07
add 1935 prod variant
Ashwagandha-coder Sep 15, 2025
cc76341
Merge pull request #213
Ashwagandha-coder Sep 15, 2025
cddc035
change jdk location
Ashwagandha-coder Sep 15, 2025
6ed095a
add readable docs
Ashwagandha-coder Sep 15, 2025
067184b
Merge remote-tracking branch 'origin/develop' into workBranch
Ashwagandha-coder Sep 15, 2025
8ce70de
Merge remote-tracking branch 'origin/master' into workBranch
Ashwagandha-coder Sep 15, 2025
9dbfa04
add 2197 problem
Ashwagandha-coder Sep 16, 2025
95aa74c
change name
Ashwagandha-coder Sep 16, 2025
a0e2f36
Merge pull request #214
Ashwagandha-coder Sep 16, 2025
3f805b7
add 3516
Ashwagandha-coder Sep 17, 2025
de640c7
add 2331
Ashwagandha-coder Sep 17, 2025
cd24a08
Merge pull request #215
Ashwagandha-coder Sep 17, 2025
11cfa0a
add 1379 on java
Ashwagandha-coder Sep 18, 2025
02853d3
add 2353
Ashwagandha-coder Sep 18, 2025
5b34d87
Merge pull request #216
Ashwagandha-coder Sep 18, 2025
f294622
add 2641
Ashwagandha-coder Sep 20, 2025
940cffa
Merge pull request #217
Ashwagandha-coder Sep 20, 2025
fed6842
add 617
Ashwagandha-coder Sep 21, 2025
30242e3
add 617 prod variant
Ashwagandha-coder Sep 21, 2025
99f0518
add 3005
Ashwagandha-coder Sep 22, 2025
25ed040
Merge pull request #218
Ashwagandha-coder Sep 22, 2025
33d8f71
add 572
Ashwagandha-coder Sep 23, 2025
5f2d89c
add 572 alt sol with serialization
Ashwagandha-coder Sep 23, 2025
20401ca
Merge pull request #219
Ashwagandha-coder Sep 23, 2025
6a0610a
add 655
Ashwagandha-coder Sep 24, 2025
fee3fcb
Merge pull request #220
Ashwagandha-coder Sep 24, 2025
c04560e
add 652 and logic for creating tree
Ashwagandha-coder Sep 27, 2025
e9f4ae6
add 652 alt sol + prod variant
Ashwagandha-coder Sep 27, 2025
125a515
Merge pull request #221
Ashwagandha-coder Sep 27, 2025
d346282
add 606
Ashwagandha-coder Sep 27, 2025
274ab62
add 606 prod variant
Ashwagandha-coder Sep 28, 2025
e85d324
add 669
Ashwagandha-coder Sep 28, 2025
922e21d
Merge pull request #222
Ashwagandha-coder Sep 28, 2025
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
21 changes: 21 additions & 0 deletions .idea/appInsightsSettings.xml

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

1 change: 1 addition & 0 deletions .idea/gradle.xml

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

15 changes: 8 additions & 7 deletions .idea/misc.xml

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

90 changes: 78 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,20 +1,86 @@
# Contest Kotlin
# LeetCode Kotlin Project

![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)

# Description
This project is a curated collection of solutions to a wide variety of LeetCode problems, all
implemented in modern, idiomatic Kotlin. It's designed to be a valuable resource for anyone
preparing for technical interviews, learning Kotlin, or exploring different algorithmic approaches
to common problems.

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.
## Key Features

# How to use
* **Comprehensive Problem Coverage:** The project includes solutions for a diverse set of problems,
covering many important data structures and algorithms.
* **Organized by Topic:** Solutions are neatly organized into packages based on the primary data
structure or algorithm used, making it easy to find examples and study specific topics.
* **Idiomatic Kotlin:** The code is written in a clean, readable, and idiomatic Kotlin style,
demonstrating best practices and modern language features.
* **Educational Resource:** By studying the solutions, you can learn how to approach different types
of algorithmic problems and how to implement them effectively in Kotlin.

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
## Why Use This Project?

There are several great reasons to use this project:

* **Accelerate Your Learning:** If you're learning algorithms and data structures, this project
provides a rich library of examples that you can study and learn from.
* **Prepare for Interviews:** The problems in this collection are representative of what you might
encounter in a technical interview. You can use these solutions to practice and prepare.
* **Discover Kotlin Best Practices:** The code in this project demonstrates how to write clean,
efficient, and expressive Kotlin. It's a great way to see how the language is used in a practical
context.

## Getting Started: How to Use the Solutions

The solutions in this project are organized into functions. To use a solution, you can simply call
the function with the required input.

Here is an example of how you could call the `getCommon` function from the `HashTableLeetcode.kt`
file within a `main` function:

```kotlin

fun main() {
// Example usage of the getCommon function
val nums1 = intArrayOf(1, 2, 3)
val nums2 = intArrayOf(2, 4)
val common = getCommon(nums1, nums2)

if (common != -1) {
println("The minimum common value is: $common")
} else {
println("No common value was found.")
}
}
```

## Table of Contents by Topic

Here is a list of the topics covered in this project, with a reference to the corresponding package:

| Topic | Package |
|----------------------|--------------------------------------|
| **Array** | `com.github.contest.array` |
| **Backtracking** | `com.github.contest.backtracking` |
| **Binary Search** | `com.github.contest.binarySearch` |
| **Binary Tree** | `com.github.contest.binaryTree` |
| **Bit Manipulation** | `com.github.contest.bitManipulation` |
| **Design** | `com.github.contest.design` |
| **Dynamic Prog.** | `com.github.contest.dp` |
| **Graph** | `com.github.contest.graph` |
| **Hash Table** | `com.github.contest.hashTable` |
| **Heap** | `com.github.contest.heap` |
| **Linked List** | `com.github.contest.linkedList` |
| **Math** | `com.github.contest.math` |
| **Priority Queue** | `com.github.contest.priorityqueue` |
| **Queue** | `com.github.contest.queue` |
| **Recursion** | `com.github.contest.recursion` |
| **Sliding Window** | `com.github.contest.slidingWindow` |
| **Sorting** | `com.github.contest.sorting` |
| **Stack** | `com.github.contest.stack` |
| **Strings** | `com.github.contest.strings` |
| **Two Pointer** | `com.github.contest.twoPointer` |

``

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


import com.github.contest.bitManipulation.hammingWeight
import com.github.contest.array.isTriangle
import com.github.contest.binaryTree.toTreeNode
import com.github.contest.binaryTree.tree2str
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 java.util.TreeMap


Expand All @@ -15,9 +16,33 @@ import java.util.TreeMap
*/

fun main() {
//largestPerimeter(intArrayOf(3, 2, 3, 10, 2, 1, 4, 4)).also { println(it) }

isTriangle(3, 4, 4).also { println(it) }
}

val str: String? = "ghdirfghdi"
fun treeLaunch() {
val tree1 = listOf(1, 2, 3, 4).toTreeNode()
val tree2 = listOf(1).toTreeNode()
val tree3 = listOf(1, 2, 3, null, 4).toTreeNode()
//tree1.printTree()

tree2str(tree3).also {
println(it)
}
}


class Example(val param: String) { // Primary constructor parameter
init {
println("init block: $param") // Can access param!
}

val property = "Property: $param".also { println(it) } // Property initializer

constructor(secondary: Int) : this("Secondary:$secondary") { // Secondary constructor
println("Secondary constructor")
}
}


Expand Down Expand Up @@ -151,7 +176,7 @@ fun workWithTreeMap() {


fun IntArray.printArray() {
var s = when (this.size) {
val s = when (this.size) {
0 -> "[]"
1 -> "[${this[0]}]"
2 -> "[${this[0]}, ${this[1]}]"
Expand Down
39 changes: 39 additions & 0 deletions contest/src/main/java/com/github/contest/array/ArrayLeetcode.kt
Original file line number Diff line number Diff line change
Expand Up @@ -439,4 +439,43 @@ fun maximumDifference(nums: IntArray): Int {
return diff
}

/**
* 54. Spiral Matrix
*/

fun spiralOrder(matrix: Array<IntArray>): List<Int> = when (matrix.size) {
1 -> matrix[0].toList()
2 -> matrix[0].toList() + matrix[1].reversed()

else -> {

val res = mutableListOf<Int>()
var top = 0
var bottom = matrix.size - 1
var left = 0
var right = matrix[0].size - 1

while (top <= bottom && left <= right) {
for (i in left..right) res.add(matrix[top][i])
top++

for (i in top..bottom) res.add(matrix[i][right])
right--

if (top <= bottom) {
for (i in right downTo left) res.add(matrix[bottom][i])
bottom--
}

if (left <= right) {
for (i in bottom downTo top) res.add(matrix[i][left])
left++
}

}

res
}
}


Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,66 @@ class FindElementsAlternativeSolution(root: TreeNode?) {
fun find(target: Int): Boolean {
return values.contains(target)
}
}

/**
* 572. Subtree of Another Tree
* Alternative Solution
* Serialization
*/

fun isSubtreeSerialization(root: TreeNode?, subRoot: TreeNode?): Boolean {

val stringRoot = serialize(root)
val stringSubRoot = serialize(subRoot)

return stringRoot.contains(stringSubRoot)
}


fun serialize(root: TreeNode?): String {
if (root == null) return "null"

return "#${root.`val`} ${serialize(root.left)} ${serialize(root.right)}"
}

/**
* 652. Find Duplicate Subtrees
* Alternative Solution
* Using Serialize + Traverse
*/

fun findDuplicateSubtreesOtherSolution(root: TreeNode?): List<TreeNode?> {
val result = mutableListOf<TreeNode?>()
val subtreeMap = mutableMapOf<String, Int>() // serialization -> count
serialize(root, subtreeMap, result)
return result
}

private fun serialize(
node: TreeNode?,
subtreeMap: MutableMap<String, Int>,
result: MutableList<TreeNode?>
): String {
if (node == null) return "#"

// Postorder serialization: left + right + root
val serial = buildString {
append(serialize(node.left, subtreeMap, result))
append(",")
append(serialize(node.right, subtreeMap, result))
append(",")
append(node.`val`)
}


val count = subtreeMap.getOrDefault(serial, 0)
subtreeMap[serial] = count + 1


if (count == 1) {
result.add(node)
}

return serial
}
Loading