Skip to content

Commit c9eb5f2

Browse files
Merge pull request #211
add new easy problem 15.07
2 parents bb67318 + 0316fb6 commit c9eb5f2

File tree

2 files changed

+76
-10
lines changed

2 files changed

+76
-10
lines changed

contest/src/main/java/com/github/contest/design/DesignLeetcode.kt

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,7 @@ class ProductOfNumbers() {
4040
class WordDictionary() {
4141

4242
data class TrieNode(
43-
val children: MutableMap<Char, TrieNode> = mutableMapOf(),
44-
var isEndOfWord: Boolean = false
43+
val children: MutableMap<Char, TrieNode> = mutableMapOf(), var isEndOfWord: Boolean = false
4544
)
4645

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

228227
private fun combine(
229-
index: Int,
230-
str: String,
231-
subset: StringBuilder,
232-
store: MutableList<String>,
233-
limit: Int
228+
index: Int, str: String, subset: StringBuilder, store: MutableList<String>, limit: Int
234229
) {
235230

236231
if (subset.length == limit) {
@@ -370,4 +365,42 @@ class StockSpanner() {
370365
return span
371366
}
372367

368+
}
369+
370+
/**
371+
* 1865. Finding Pairs With a Certain Sum
372+
*/
373+
374+
class FindSumPairs(private val nums1: IntArray, private val nums2: IntArray) {
375+
376+
private val second = mutableListOf<Long>()
377+
private val freq = mutableMapOf<Long, Int>()
378+
379+
init {
380+
for (elem in nums2) {
381+
val e = elem.toLong()
382+
second.add(e)
383+
freq[e] = freq.getOrDefault(e, 0) + 1
384+
}
385+
}
386+
387+
fun add(index: Int, `val`: Int) {
388+
val old = second[index]
389+
second[index] = second[index] + `val`
390+
val new = second[index]
391+
freq[old] = freq.getOrDefault(old, 0) - 1
392+
if (freq[old] == 0) freq.remove(old)
393+
freq[new] = freq.getOrDefault(new, 0) + 1
394+
}
395+
396+
fun count(tot: Int): Int {
397+
var c = 0
398+
for (elem in nums1) {
399+
val target = tot - elem
400+
if (freq.contains(target.toLong())) c += freq.getOrDefault(target.toLong(), 0)
401+
}
402+
403+
return c
404+
}
405+
373406
}

contest/src/main/java/com/github/contest/strings/StringsLeetcode.kt

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package com.github.contest.strings
22

3-
import java.math.BigInteger
4-
53
/**
64
* 848. Shifting Letters
75
*/
@@ -389,7 +387,6 @@ fun lengthOfLastWord(s: String): Int {
389387
}
390388

391389

392-
393390
/**
394391
* 2138. Divide a String Into Groups of Size k
395392
*/
@@ -438,4 +435,40 @@ private fun remainingFill(strs: Array<String>, pattern: Char, k: Int) {
438435
}
439436
}
440437

438+
/**
439+
* 3136. Valid Word
440+
*/
441+
442+
fun isValid(word: String): Boolean {
443+
if (word.length < 3) return false
444+
445+
var vowels = 0
446+
var consonants = 0
447+
448+
for (char in word) {
449+
if (isNotDigit(char) && isNotLetter(char)) return false
450+
if (isLetter(char)) {
451+
if (isVowel(char)) vowels++
452+
else consonants++
453+
}
454+
}
455+
456+
return !(vowels == 0 || consonants == 0)
457+
}
458+
459+
private fun isNotDigit(char: Char) = when {
460+
char in '0'..'9' -> false
461+
else -> true
462+
}
463+
464+
private fun isNotLetter(char: Char) = when {
465+
char in 'a'..'z' || char in 'A'..'Z' -> false
466+
else -> true
467+
}
468+
469+
private fun isVowel(char: Char) = when {
470+
char in "aeiou" || char in "AEIOU" -> true
471+
else -> false
472+
}
473+
441474

0 commit comments

Comments
 (0)