코딩테스트 풀이/JAVA
[LeetCode/Easy] 392. Is Subsequence
무지맘
2023. 5. 18. 23:38
1. Input
1) String s
2) String t
2. Output
1) s가 t의 subsequence라면 true, 아니면 false를 반환
- subsequence란 문자가 연속적이지 않아도 차례대로 다른 문자열에 순서대로 있는 것을 말한다. 예를 들면 “ace"는 "abcde"의 subsequence지만 ”aec"는 아니다.
3. Constraint
1) 0 <= s.length <= 100
2) 0 <= t.length <= 10^4
3) s와 t는 영어 소문자로만 이루어져 있다.
4. Example
Input: s = "abc", t = "ahbgdc" -> Output: true
Input: s = "axc", t = "ahbgdc" -> Output: false
5. Code
1) 첫 코드(2023/05/18)
class Solution {
public boolean isSubsequence(String s, String t) {
if(s.length()>t.length())
return false;
else if(t.equals("") || s.equals(""))
return true;
else if(s.length()==t.length())
return s.equals(t);
int pre = t.indexOf(s.charAt(0)), index = 1;
while(index<s.length()){
boolean find = false;
for(int i=pre+1 ; i<t.length() && !find ; i++)
if(t.charAt(i)==s.charAt(index)){
pre = i; index++; find = true;
}
if(!find)
return false;
}
return true;
}
}