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 |
Tags
- github
- gs25
- Baekjoon
- 데이콘
- Git
- Real or Not? NLP with Disaster Tweets
- leetcode
- 우분투
- 캐치카페
- hackerrank
- Docker
- 자연어처리
- 프로그래머스 파이썬
- PYTHON
- 파이썬
- SW Expert Academy
- programmers
- 백준
- 코로나19
- ubuntu
- Kaggle
- 편스토랑
- AI 경진대회
- 더현대서울 맛집
- 맥북
- 프로그래머스
- 편스토랑 우승상품
- ChatGPT
- dacon
- 금융문자분석경진대회
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



SOMJANG/CODINGTEST_PRACTICE
1일 1문제 since 2020.02.07. Contribute to SOMJANG/CODINGTEST_PRACTICE development by creating an account on GitHub.
github.com
'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