[Programmers] #181842 - 부분 문자열 [Java][C++][Python]
[Programmers] #181842 - 부분 문자열 [Java][C++][Python]
1. 아이디어
문자열 str1이 문자열 str2에 포함되는지만 판단하면 되는 간단한 문제다.
2. 복잡도
| 접근 | 시간 | 공간 |
|---|---|---|
| 풀이 | $O(N \times M)$ | $O(1)$ |
($N$ = str2의 길이, $M$ = str1의 길이. Python의 in은 CPython 구현상 $O(N + M)$)
3. 코드
풀이 [Java][C++][Python]
1
2
3
4
5
class Solution {
public int solution(String str1, String str2) {
return str2.contains(str1) ? 1 : 0;
}
}
1
2
3
4
5
6
#include <bits/stdc++.h>
using namespace std;
int solution(string str1, string str2) {
return str2.find(str1) != -1;
}
1
2
def solution(str1, str2):
return int(str1 in str2)
This post is licensed under CC BY 4.0 by the author.