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
- 백준
- 자연어처리
- hackerrank
- 더현대서울 맛집
- Docker
- 우분투
- ubuntu
- 코로나19
- 맥북
- leetcode
- 편스토랑
- AI 경진대회
- programmers
- 파이썬
- github
- Git
- SW Expert Academy
- Baekjoon
- gs25
- 캐치카페
- 프로그래머스 파이썬
- ChatGPT
- 데이콘
- 금융문자분석경진대회
- PYTHON
- 프로그래머스
- Real or Not? NLP with Disaster Tweets
- Kaggle
- 편스토랑 우승상품
- dacon
Archives
- Today
- Total
솜씨좋은장씨
[leetCode] 686. Repeated String Match (Python) 본문
728x90
반응형
Given two strings a and b, return the minimum number of times you should repeat string a so that string b is a substring of it. If it is impossible for b to be a substring of a after repeating it, return -1.
Notice: string "abc" repeated 0 times is "", repeated 1 time is "abc" and repeated 2 times is "abcabc".
Example 1:
Input: a = "abcd", b = "cdabcdab"
Output: 3
Explanation: We return 3 because by repeating a three times "abcdabcdabcd", b is a substring of it.
Example 2:
Input: a = "a", b = "aa"
Output: 2
Example 3:
Input: a = "a", b = "a"
Output: 1
Example 4:
Input: a = "abc", b = "wxyz"
Output: -1
Constraints:
- 1 <= a.length <= 104
- 1 <= b.length <= 104
- a and b consist of lower-case English letters.
Solution
class Solution:
def repeatedStringMatch(self, A: str, B: str) -> int:
A_len = len(A)
B_len = len(B)
answer = B_len // A_len + 2
temp = A * answer
if B not in temp:
return -1
while B in A * (answer-1):
answer = answer - 1
return answer
'Programming > 코딩 1일 1문제' 카테고리의 다른 글
[leetCode] 1051. Height Checker (Python) (0) | 2020.09.27 |
---|---|
[leetCode] 925. Long Pressed Name (Python) (0) | 2020.09.25 |
[leetCode] 1207. Unique Number of Occurrences (Python) (0) | 2020.09.23 |
[leetCode] 657. Robot Return to Origin (Python) (0) | 2020.09.22 |
[leetCode] 703. Kth Largest Element in a Stream (Python) (0) | 2020.09.21 |
Comments