File tree Expand file tree Collapse file tree 1 file changed +41
-0
lines changed
src/main/scala/com/kishan/scala/leetcode/juneChallenges Expand file tree Collapse file tree 1 file changed +41
-0
lines changed Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments