File tree Expand file tree Collapse file tree 1 file changed +46
-0
lines changed
Leetcode/DailyChallenges/March2022 Expand file tree Collapse file tree 1 file changed +46
-0
lines changed Original file line number Diff line number Diff line change 1+ package LeetcodeDaily .March2022 ;
2+
3+ public class Day2IsSubsequenceLC392 {
4+
5+ /*
6+ * Input: s = "abc", t = "ahbgdc" Output: true
7+ */
8+
9+ public static boolean isSubsequence (String s , String t ) {
10+
11+ int lastIndex = 0 , matchcharcount = 0 ;
12+ int i = 0 ;
13+
14+ for (i = 0 ; i < s .length (); i ++) {
15+
16+ char currs = s .charAt (i );
17+
18+ if (lastIndex < t .length ()) {
19+ for (int j = lastIndex ; j < t .length (); j ++) {
20+
21+ char currt = t .charAt (j );
22+
23+ if (currs == currt ) {
24+ matchcharcount ++;
25+ lastIndex = j + 1 ;
26+ break ;
27+ }
28+ }
29+ }
30+ }
31+
32+ if (i == s .length () && matchcharcount == s .length ())
33+ return true ;
34+
35+ return false ;
36+ }
37+
38+ public static void main (String [] args ) {
39+
40+ String s = "abc" , t = "ahbgdc" ;
41+ System .out .println (isSubsequence (s , t ));
42+
43+ System .out .println (isSubsequence ("axc" , "ahbgdc" ));
44+
45+ }
46+ }
You can’t perform that action at this time.
0 commit comments