Description
Implement strStr().
Return the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.
Clarification:
What should we return when needle is an empty string? This is a great question to ask during an interview.
For the purpose of this problem, we will return 0 when needle is an empty string. This is consistent to C's strstr() and Java's indexOf().
My solution
class Solution:
def strStr(self, haystack: str, needle: str) -> int:
if needle == "":
return 0
elif needle not in haystack:
return -1
else:
haystack = haystack.replace(needle, "*")
return haystack.index("*")
'Algorithm > Daily Coding Tests Challenge' 카테고리의 다른 글
[Leetcode] Easy : Search Insert Position (0) | 2021.09.05 |
---|---|
[프로그래머스] level2. JadenCase 문자열 만들기 (0) | 2021.09.05 |
[프로그래머스] level2. 카펫 (0) | 2021.09.04 |
[프로그래머스] level2. 문자열 압축 (0) | 2021.09.03 |
[Leetcode] Easy : Remove Element (0) | 2021.09.03 |