File tree Expand file tree Collapse file tree 1 file changed +28
-17
lines changed Expand file tree Collapse file tree 1 file changed +28
-17
lines changed Original file line number Diff line number Diff line change
1
+
1
2
class Solution {
2
3
public boolean isSubsequence (String s , String t ) {
3
4
4
5
// bruteforce soln
5
- // basically we have to check s is present in t in same sequence
6
- //edge cases
7
- // if s is null then return true
8
- if (s .length ()==0 )
9
- return true ;
10
- // if t is null then return false
11
- if (t .length ()==0 )
12
- return false ;
13
- // go to every cahrecter of t and search of every charecter of s
14
- int i =0 ,j =0 ;
15
- for (i =0 ;i <t .length () && j <s .length ();i ++){
16
- if (s .charAt (j )==t .charAt (i ))
17
- j ++;
6
+ // // basically we have to check s is present in t in same sequence
7
+ // //edge cases
8
+ // // if s is null then return true
9
+ // if(s.length()==0)
10
+ // return true;
11
+ // // if t is null then return false
12
+ // if(t.length()==0)
13
+ // return false;
14
+ // // go to every cahrecter of t and search of every charecter of s
15
+ // int i=0,j=0;
16
+ // for(i=0;i<t.length() && j<s.length();i++){
17
+ // if(s.charAt(j)==t.charAt(i))
18
+ // j++;
19
+ // }
20
+ // // if j will be equal to s length that means every character is
21
+ //present so return true else return false
22
+ // if(j==s.length())
23
+ // return true;
24
+ // return false;
25
+
26
+ int index = -1 ;
27
+ for (char c : s .toCharArray ()) { // for each loop
28
+ index = t .indexOf (c , index + 1 );
29
+ if (index == -1 ) return false ; // if not found return false
18
30
}
19
- // if j will be equal to s length that means every character is present so return true else return false
20
- if (j ==s .length ())
21
- return true ;
22
- return false ;
31
+ return true ;
32
+
33
+
23
34
24
35
}
25
36
}
You can’t perform that action at this time.
0 commit comments