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
PostgreSQLv16

Tukaj bo rezultat izvajanja kode

Visible tests
1
Input
"hixxhi"
Expected1
2
Input
"xaxxaxaxx"
Expected1
3
Input
"axxxaxaxx"
Expected2
4
Input
"xx"
Expected0
5
Input
""
Expected0
6
Input
" "
Expected0
Focus radio
Paused · SomaFM · Fluid