Skip to content

Commit 45e5f1d

Browse files
authored
Update IsSubSequence.java
1 parent 4bba7c1 commit 45e5f1d

File tree

1 file changed

+28
-17
lines changed

1 file changed

+28
-17
lines changed

IsSubSequence.java

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,36 @@
1+
12
class Solution {
23
public boolean isSubsequence(String s, String t) {
34

45
// 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
1830
}
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+
2334

2435
}
2536
}

0 commit comments

Comments
 (0)