Earlier Tail Matches
This function is good practice for finding a small pattern inside a string. Write a function last2 that takes a string s, looks at its last two characters, and counts how many times the same pair appeared earlier in the string. Do not count the final occurrence itself, because it is the ending you are comparing against.
Примеры
Ввод
"hixxhi"Вывод
1Ввод
"xaxxaxaxx"Вывод
1Ввод
"axxxaxaxx"Вывод
2Нужно реализовать
def last2(s: str) -> int:
Теги
stringssliding-window
Pieslēdzies, lai redzētu iesniegumu vēsturi
PieslēgtiesPieslēdzies, lai izmantotu AI mentoru
PieslēgtiesVisible tests
1
Input
"hixxhi"Expected
12
Input
"xaxxaxaxx"Expected
13
Input
"axxxaxaxx"Expected
24
Input
"xx"Expected
05
Input
""Expected
06
Input
" "Expected
0Focus radio
Paused · SomaFM · Fluid