Skip to content

Commit 8f4e5a4

Browse files
committed
392. Is Subsequence
1 parent 10eb580 commit 8f4e5a4

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

is-subsequence.scala

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//Runtime: 396 ms
2+
object Solution {
3+
def isSubsequence(s: String, t: String): Boolean = {
4+
5+
def look(c:Char, t:String, idx:Int):Int = {
6+
if(idx >= t.size) -1
7+
else if(c == t(idx)) idx
8+
else look(c, t, idx+1)
9+
}
10+
11+
def solve(s:String , t:String, i:Int, idx:Int):Boolean = {
12+
if(i >= s.size) true
13+
else{
14+
val idx2 = look(s(i), t, idx+1)
15+
if(idx2 == -1) false
16+
else solve(s, t, i+1, idx2)
17+
}
18+
}
19+
solve(s, t, 0, -1)
20+
}
21+
}

0 commit comments

Comments
 (0)