문제를 이해하고 있다면 바로 풀이를 보면 됨
전체 코드로 바로 넘어가도 됨
마음대로 번역해서 오역이 있을 수 있음
문자열 배열 words와 문자 separator가 주어지고, separator를 기준으로 words의 각 문자열을 나눠라.
분할을 통해 생성된 새로운 문자열들을 포함하는 문자열 배열을 빈 문자열은 제외하고 반환해라.
#1
Input: words = ["one.two.three", "four.five", "six"], separator = "."
Output: ["one", "two", "three", "four", "five", "six"]
Explanation: 예시에서 다음과 같이 나눈다.
"one.two.three"는 "one", "two", "three"로 나눈다.
"four.five"는 "four", "five"로 나눈다.
"six"는 "six"로 나눈다.
그래서 결과 배열은 ["one", "two", "three", "four", "five", "six"]가 된다.
#2
Input: words = ["", ""], separator = "$"
Output: ["easy", "problem"]
#3
Input: words = ["|||"], separator = "|"
Output: []
class Solution {
public List<String> splitWordsBySeparator(List<String> words, char separator) {
List<String> result = new ArrayList<>(words.size() * 3);
for(String word : words){
int sIdx = 0;
int n = word.length();
for(int i = 0; i < n; i++){
if(word.charAt(i) == separator){
if(sIdx < i){
result.add(word.substring(sIdx, i));
}
sIdx = i + 1;
}
}
if(sIdx < n){
result.add(word.substring(sIdx));
}
}
return result;
}
}