[Programmers] #181841 - 꼬리 문자열 [Java][C++][Python]
[Programmers] #181841 - 꼬리 문자열 [Java][C++][Python]
1. 아이디어
문자열 리스트 str_list의 각 문자열에 대해 문자열 ex를 포함하지 않는 경우만 전부 연결하면 되는 간단한 문제다.
2. 복잡도
| 접근 | 시간 | 공간 |
|---|---|---|
| 풀이 | $O(N \times L \times M)$ | $O(N \times L)$ |
($N$ = str_list의 길이, $L$ = 각 문자열의 길이, $M$ = ex의 길이)
3. 코드
풀이 [Java][C++][Python]
1
2
3
4
5
6
7
8
9
10
class Solution {
public String solution(String[] str_list, String ex) {
StringBuilder sb = new StringBuilder();
for (String s : str_list) {
if (!s.contains(ex)) sb.append(s);
}
return sb.toString();
}
}
문자열 덧셈이 반복되므로 StringBuilder를 활용해 가변으로 처리했다.
1
2
3
4
5
6
7
8
9
10
11
#include <bits/stdc++.h>
using namespace std;
string solution(vector<string> str_list, string ex) {
string res;
for (string& s : str_list) {
if (s.find(ex) == -1) res += s;
}
return res;
}
1
2
def solution(str_list, ex):
return "".join(s for s in str_list if ex not in s)
This post is licensed under CC BY 4.0 by the author.