Notice
Recent Posts
Recent Comments
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | 7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 27 | 28 |
29 | 30 | 31 |
Tags
- 더현대서울 맛집
- Git
- github
- programmers
- ChatGPT
- 맥북
- 편스토랑 우승상품
- 우분투
- gs25
- SW Expert Academy
- 프로그래머스 파이썬
- hackerrank
- AI 경진대회
- PYTHON
- dacon
- 데이콘
- ubuntu
- 캐치카페
- 편스토랑
- 코로나19
- Kaggle
- 금융문자분석경진대회
- leetcode
- Real or Not? NLP with Disaster Tweets
- Docker
- 파이썬
- 자연어처리
- 백준
- Baekjoon
- 프로그래머스
Archives
- Today
- Total
솜씨좋은장씨
[leetCode] 28. Implement strStr() (Python) 본문
728x90
반응형
Implement strStr().
Return the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.
Example 1:
Input: haystack = "hello", needle = "ll"
Output: 2
Example 2:
Input: haystack = "aaaaa", needle = "bba"
Output: -1
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().
Constraints:
- haystack and needle consist only of lowercase English characters.
Solution
class Solution:
def strStr(self, haystack: str, needle: str) -> int:
if (haystack == "" and needle == "") or needle == "":
return 0
elif haystack == "":
return -1
split_check = haystack.split(needle)
if split_check[0] == haystack:
answer = -1
elif split_check[0] != haystack:
answer = len(split_check[0])
return answer
'Programming > 코딩 1일 1문제' 카테고리의 다른 글
[leetCode] 811. Subdomain Visit Count (Python) (0) | 2020.09.12 |
---|---|
[leetCode] 1002. Find Common Characters (Python) (0) | 2020.09.12 |
[leetCode] 1337. The K Weakest Rows in a Matrix (Python) (0) | 2020.09.09 |
[leetCode] 1331. Rank Transform of an Array (Python) (0) | 2020.09.08 |
[leetCode] 506. Relative Ranks (Python) (0) | 2020.09.07 |
Comments