본문 바로가기
LeetCode

LeetCode 686. Repeated String Match

by 콩순이냉장고 2021. 7. 31.

문제 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

 

궁금한점 혹은 모르는점, 어떤 질문이라도 댓글은 언제나 환영입니다.