-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMadeSubsequenceCyclically.java
More file actions
27 lines (26 loc) · 1.05 KB
/
MadeSubsequenceCyclically.java
File metadata and controls
27 lines (26 loc) · 1.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
class Solution {
// isme subsequence bnana hai str2 ko toh baari baari str1 pr loop krenge hrr ek character jo str2 se match ho rha hai usko skip krke jo match nahi ho rhe unko dekhenge or +1 increment krenge cycly and agr pointer2 hmara last tk pahuch jata hai means usne saare characters ko convert kr dia hai cyclically vrna nai kiya
public boolean canMakeSubsequence(String str1, String str2) {
int pointer1=0;
int pointer2=0;
while(pointer1<str1.length() && pointer2<str2.length()){
char c1=str1.charAt(pointer1);
char c2=str2.charAt(pointer2);
if(c1==c2){
pointer1++;
pointer2++;
}
else{
char nextChar=(char)((str1.charAt(pointer1)-'a'+1)%26 +'a');
if(nextChar==str2.charAt(pointer2)){
pointer1++;
pointer2++;
}
else{
pointer1++;
}
}
}
return pointer2==str2.length();
}
}