
문제해석
문자열 s와 t가 주어졌을 때 s가 t의 일부가 될 수 있는지 체크
다만 s의 순서는 지켜져야하며 t의 일부가 사라지는 형태로 대체될 수 있음
정답 예
public boolean isSubsequence(String s, String t) {
// s의 길이, 마지막 문자 도달 시점과 비교
int sLength = s.length() - 1;
// s가 ""인 경우 true
if (sLength == -1) {
return true;
}
// s의 순서
int sIndex = 0;
char[] schar = s.toCharArray();
char[] tchar = t.toCharArray();
int tLength = t.length();
for (int i = 0; i < tLength; i++) {
// s의 문자열과 t의 문자열이 같으면
if (tchar[i] == schar[sIndex]) {
// s의 다음 순서 문자열
sIndex++;
}
// s의 모든 문자열이 전부 비교 된 경우
if (sIndex > sLength) {
return true;
}
}
return false;
}'Algorithm' 카테고리의 다른 글
| [leetcode 75] 206. Reverse Linked List (0) | 2023.02.18 |
|---|---|
| [leetcode 75] 21. Merge Two Sorted Lists (0) | 2023.02.18 |
| [leetcode 75] 205. Isomorphic Strings (1) | 2023.02.17 |
| [leetcode 75] 724. Find Pivot Index (0) | 2023.02.16 |
| [leetcode 75] 1480. Running Sum of 1d Array (0) | 2023.02.15 |