Skip to content

Commit 2930648

Browse files
authored
Merge pull request #504 from 0chnxxx/0chnxxx/ltc/392
[LTC] Lv1 / Is Subsequence / 7분
2 parents 682ef5e + f908c66 commit 2930648

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

leetcode/0chnxxx/Is_Subsequence.kt

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/**
2+
* Given two strings s and t, return true if s is a subsequence of t, or false otherwise.
3+
* A subsequence of a string is a new string that is formed from the original string by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters.
4+
* (i.e., "ace" is a subsequence of "abcde" while "aec" is not).
5+
*
6+
* Constraints:
7+
* 0 <= s.length <= 100
8+
* 0 <= t.length <= 10^4
9+
* s and t consist only of lowercase English letters.
10+
*
11+
* Follow up:
12+
* Suppose there are lots of incoming s, say s1, s2, ..., sk where k >= 109, and you want to check one by one to see if t has its subsequence.
13+
* In this scenario, how would you change your code?
14+
*/
15+
16+
fun main() {
17+
val s = "abc"
18+
val t = "ahbgdc"
19+
20+
val result = Solution().isSubsequence(s, t)
21+
22+
println(result)
23+
}
24+
25+
class Solution {
26+
// 시간 복잡도 : O(N)
27+
// 공간 복잡도 ; O(1)
28+
fun isSubsequence(s: String, t: String): Boolean {
29+
var i = 0
30+
var j = 0
31+
32+
while (i < s.length && j < t.length) {
33+
if (s[i] == t[j]) {
34+
i++
35+
}
36+
37+
j ++
38+
}
39+
40+
return i == s.length
41+
}
42+
}

0 commit comments

Comments
 (0)