LeetCode Entry

2264. Largest 3-Same-Digit Number in String

04.12.2023 easy 2023 kotlin

Largest 3-same-digit number in a string

2264. Largest 3-Same-Digit Number in String easy blog post substack youtube image.png

Join me on Telegram

https://t.me/leetcode_daily_unstoppable/427

Problem TLDR

Largest 3-same-digit number in a string

Intuition

There are totally 10 such numbers: 000, 111, ..., 999.

Approach

Let’s use Kotlin’s API

Complexity

  • Time complexity: \(O(n)\)

  • Space complexity: \(O(n)\), can be O(1) with asSequence()

Code

    fun largestGoodInteger(num: String): String =
      num.windowed(3)
      .filter { it[0] == it[1] && it[0] == it[2] }
      .maxByOrNull { it[0] } ?: ""