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
47 changes: 40 additions & 7 deletions contest/src/main/java/com/github/contest/design/DesignLeetcode.kt
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@ class ProductOfNumbers() {
class WordDictionary() {

data class TrieNode(
val children: MutableMap<Char, TrieNode> = mutableMapOf(),
var isEndOfWord: Boolean = false
val children: MutableMap<Char, TrieNode> = mutableMapOf(), var isEndOfWord: Boolean = false
)

private val root = TrieNode()
Expand Down Expand Up @@ -226,11 +225,7 @@ class CombinationIterator(characters: String, combinationLength: Int) {
fun hasNext(): Boolean = store.isNotEmpty()

private fun combine(
index: Int,
str: String,
subset: StringBuilder,
store: MutableList<String>,
limit: Int
index: Int, str: String, subset: StringBuilder, store: MutableList<String>, limit: Int
) {

if (subset.length == limit) {
Expand Down Expand Up @@ -370,4 +365,42 @@ class StockSpanner() {
return span
}

}

/**
* 1865. Finding Pairs With a Certain Sum
*/

class FindSumPairs(private val nums1: IntArray, private val nums2: IntArray) {

private val second = mutableListOf<Long>()
private val freq = mutableMapOf<Long, Int>()

init {
for (elem in nums2) {
val e = elem.toLong()
second.add(e)
freq[e] = freq.getOrDefault(e, 0) + 1
}
}

fun add(index: Int, `val`: Int) {
val old = second[index]
second[index] = second[index] + `val`
val new = second[index]
freq[old] = freq.getOrDefault(old, 0) - 1
if (freq[old] == 0) freq.remove(old)
freq[new] = freq.getOrDefault(new, 0) + 1
}

fun count(tot: Int): Int {
var c = 0
for (elem in nums1) {
val target = tot - elem
if (freq.contains(target.toLong())) c += freq.getOrDefault(target.toLong(), 0)
}

return c
}

}
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package com.github.contest.strings

import java.math.BigInteger

/**
* 848. Shifting Letters
*/
Expand Down Expand Up @@ -389,7 +387,6 @@ fun lengthOfLastWord(s: String): Int {
}



/**
* 2138. Divide a String Into Groups of Size k
*/
Expand Down Expand Up @@ -438,4 +435,40 @@ private fun remainingFill(strs: Array<String>, pattern: Char, k: Int) {
}
}

/**
* 3136. Valid Word
*/

fun isValid(word: String): Boolean {
if (word.length < 3) return false

var vowels = 0
var consonants = 0

for (char in word) {
if (isNotDigit(char) && isNotLetter(char)) return false
if (isLetter(char)) {
if (isVowel(char)) vowels++
else consonants++
}
}

return !(vowels == 0 || consonants == 0)
}

private fun isNotDigit(char: Char) = when {
char in '0'..'9' -> false
else -> true
}

private fun isNotLetter(char: Char) = when {
char in 'a'..'z' || char in 'A'..'Z' -> false
else -> true
}

private fun isVowel(char: Char) = when {
char in "aeiou" || char in "AEIOU" -> true
else -> false
}