Largest 3-Same-Digit Number in String - Leetcode 2264 - Python

preview_player
Показать описание


0:00 - Read the problem
0:30 - Drawing Explanation
3:20 - Coding Explanation

leetcode 2264

#neetcode #leetcode #python
Рекомендации по теме
Комментарии
Автор

class Solution:
def largestGoodInteger(self, num: str) -> str:
for i in range(9, -1, -1):
if str(i) * 3 in num:
return str(i) * 3
return ''
another way

chrischika
Автор

class Solution:
def largestGoodInteger(self, num: str) -> str:
ans = ''

for i in range(len(num)-2):
if num[i] == num[i+1] == num[i+2]:
ans = max(ans, num[i])

return ans*3

This is a bit cleaner imo and takes advantage of string multiplication built into python. I also got 100% on the run time which is a plus

Andrew_J
Автор

What if there is a follow up of n consecutive integers? Do i use a long if statement, or switch to sliding window approach?

kushagrasaxena
Автор

max_res = ""
idx = 0

while idx < len(num):
res = ""

if idx + 2 < len(num) and num[idx] == num[idx + 1] and num[idx] == num[idx + 2]:
res += num[idx] + num[idx] + num[idx]
max_res = max(res, max_res)

idx += 1

return max_res

akshayiithyd
Автор

What are your thoughts on using regular expressions based solution?

Watt_dude
Автор

Your voice sounds bit different, did you get cold?

shreehari
Автор

Can't you just set the res = "" and just return the res at the end?

williamjz
Автор

difficult questions take time to solve
easy questions take time to figure am I missing a more performant way
So, DSA can only be mugged up

ngneerin
Автор

i overthought it and use sliding window in it lol

kawaljeetsingh
Автор

I first thought you were Indian, then I thought you may be Mexican. But my first thought was spot on

vidhya
welcome to shbcf.ru