Skip to content

Commit bd99267

Browse files
committed
Is Subsequence - Day 9
1 parent 2ed61e0 commit bd99267

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.kishan.scala.leetcode.juneChallenges
2+
3+
import scala.util.matching.Regex
4+
5+
/*
6+
* Given a string s and a string t, check if s is subsequence of t.
7+
* A subsequence of a string is a new string which is formed from the original string by
8+
* deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters.
9+
* (ie, "ace" is a subsequence of "abcde" while "aec" is not).
10+
*
11+
* */
12+
13+
/*
14+
* Input: s = "abc", t = "ahbgdc"
15+
Output: true
16+
* Input: s = "axc", t = "ahbgdc"
17+
Output: false
18+
* */
19+
20+
/*
21+
* Approach:
22+
*
23+
* Regex:
24+
* 1. Build Regex Pattern.
25+
* 2. Check if we have matching string. If nonEmpty true. Else False
26+
* */
27+
28+
29+
object IsSubsequence {
30+
31+
def isSubsequence(s: String, t: String): Boolean = {
32+
val regex = new Regex(s.map(a => s".*${a}").mkString)
33+
regex.findPrefixOf(t).nonEmpty
34+
}
35+
36+
def main(args: Array[String]): Unit = {
37+
val s = "rjufvjafbxnbgriwgokdgqdqewn"
38+
val t = "mjmqqjrmzkvhxlyruonekhhofpzzslupzojfuoztvzmmqvmlhgqxehojfowtrinbatjujaxekbcydldglkbxsqbbnrkhfdnpfbuaktupfftiljwpgglkjqunvithzlzpgikixqeuimmtbiskemplcvljqgvlzvnqxgedxqnznddkiujwhdefziydtquoudzxstpjjitmiimbjfgfjikkjycwgnpdxpeppsturjwkgnifinccvqzwlbmgpdaodzptyrjjkbqmgdrftfbwgimsmjpknuqtijrsnwvtytqqvookinzmkkkrkgwafohflvuedssukjgipgmypakhlckvizmqvycvbxhlljzejcaijqnfgobuhuiahtmxfzoplmmjfxtggwwxliplntkfuxjcnzcqsaagahbbneugiocexcfpszzomumfqpaiydssmihdoewahoswhlnpctjmkyufsvjlrflfiktndubnymenlmpyrhjxfdcq"
39+
println(isSubsequence(s,t))
40+
}
41+
}

0 commit comments

Comments
 (0)