Find the First Match
Substring search is used in editors, logs, and filters when you need to know where a piece of text first appears. The function contain_string receives two strings, haystack and needle. Return the zero-based index of the first occurrence of needle inside haystack. If it is not found, return -1, and if needle is empty, return 0.
Примеры
Ввод
["mississippi", "issipi"]Вывод
-1Ввод
["Hello World", ""]Вывод
0Ввод
["hello", "ll"]Вывод
2Нужно реализовать
def contain_string(haystack: str, needle: str) -> int:
Теги
stringstwo-pointers
Conectează-te pentru a vedea istoricul soluțiilor trimise
Conectează-teConectează-te pentru a folosi Mentorul AI
Conectează-teVisible tests
1
Input
"mississippi", "issipi"Expected
-12
Input
"Hello World", ""Expected
03
Input
"hello", "ll"Expected
24
Input
"abc", "c"Expected
25
Input
"hello", "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"Expected
-16
Input
"hello", ""Expected
0Focus radio
Paused · SomaFM · Fluid