TGStat
TGStat
Type to search
Advanced channel search
  • flag English
    Site language
    flag Russian flag English flag Uzbek
  • Sign In
  • Catalog
    Channels and groups catalog Regional compilations Thematic compilations Платные каналы Search for channels
    Add a channel/group
  • Ratings
    Rating of channels Rating of groups Posts rating
    Ratings of brands and people
  • Analytics
  • Search by posts
  • Telegram monitoring
  • Promotion
    Advertising through Yandex Business Advertising in channels through TGStat Agency Advertising on TGStat.ru website
ЮШП Южная школа программирования

27 Apr, 16:28

Open in Telegram Share Report

Друзья, публикуем эталонное простое решение (O(n), один проход):

def compress(s: str) -> str:
if not s:
return s

res = []
count = 1

for i in range(1, len(s)):
if s[i] == s[i - 1]:
count += 1
else:
res.append(s[i - 1] + str(count))
count = 1

# последний блок
res.append(s[-1] + str(count))

compressed = "".join(res)

return compressed if len(compressed) < len(s) else s


🧠 Суть решения:
•
идём по строке один раз
•считаем подряд идущие символы
•собираем результат в список (не строку — важно для скорости)
•в конце сравниваем длины

76 0 0 2
Catalog
Channels and groups catalog Channels compilations Search for channels Add a channel/group
Ratings
Rating of Telegram channels Rating of Telegram groups Posts rating Ratings of brands and people
API
API statistics Search API of posts API Callback
Our channels
@TGStat @TGStat_Chat @telepulse @TGStatAPI
Read
Академия TGStat Telegram Research 2019 Telegram Research 2021 Telegram Research 2023
Contacts
Справочный центр Support Email Jobs
Miscellaneous
Terms and conditions Privacy policy Public offer
Our bots
@TGStat_Bot @SearcheeBot @TGAlertsBot @tg_analytics_bot @TGStatChatBot