Back
Close

Longest substring in Python

tsv
1,868 views

Задача

Реализовать функцию принимающую на вход строку и возвращающую самую длинную подстроку состоящую не более чем из двух различных символов.

Решение

Используем хэшмапу, трекая уникальные символы
# k is maximum number of unique chars
def longest_substring(input, k = 2):
if input is None:
return input
start = 0
result = ""
freq = {}
i = 0
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Тесты

Используем Python assert
from impl import longest_substring
def send_msg(channel, msg):
print("TECHIO> message --channel \"{}\" \"{}\"".format(channel, msg))
def success():
print("TECHIO> success true")
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Create your playground on Tech.io
This playground was created on Tech.io, our hands-on, knowledge-sharing platform for developers.
Go to tech.io
codingame x discord
Join the CodinGame community on Discord to chat about puzzle contributions, challenges, streams, blog articles - all that good stuff!
JOIN US ON DISCORD
Online Participants