문제 URL : https://leetcode.com/problems/repeated-string-match/
Repeated String Match - LeetCode
Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
leetcode.com
문제 접근법 : a의 문자열 길이를 b보다 클때까지 만들어준다음 a가 b에 속한것이 있는지 확인해주면 끝인문제입니다.
소스코드 :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
int repeatedStringMatch(string a, string b) {
string s = a;
int cnt = 1;
while (s.size() < b.size()) {
s += a;
cnt++;
}
if (s.find(b) != -1)
return cnt;
else {
s += a;
if (s.find(b) != -1)
return cnt + 1;
}
return -1;
}
};
|
cs |
궁금한점 혹은 모르는점, 어떤 질문이라도 댓글은 언제나 환영입니다.
'LeetCode' 카테고리의 다른 글
LeetCode 257 Binary Tree Paths (0) | 2021.11.14 |
---|---|
LeetCode 103. Binary Tree Zigzag Level Order Traversal (0) | 2021.08.03 |
LeetCode 572. Subtree of Another Tree (0) | 2021.07.31 |
LeetCode 459 Repeated Substring Pattern (0) | 2021.07.31 |
LeetCode 38 Count and Say (0) | 2021.07.15 |