-
Notifications
You must be signed in to change notification settings - Fork 1
/
Day 23
26 lines (18 loc) · 1.06 KB
/
Day 23
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
You are given two 0-indexed strings str1 and str2.
In an operation, you select a set of indices in str1, and for each index i in the set, increment str1[i] to the next character cyclically. That is 'a' becomes 'b', 'b' becomes 'c', and so on, and 'z' becomes 'a'.
Return true if it is possible to make str2 a subsequence of str1 by performing the operation at most once, and false otherwise.
Note: A subsequence of a string is a new string that is formed from the original string by deleting some (possibly none) of the characters without disturbing the relative positions of the remaining characters.
CODE:
class Solution {
public boolean canMakeSubsequence(String str1, String str2) {
int j=0;
for(int i=0;i<str1.length() && j<str2.length();i++){
char c1=str1.charAt(i);
char c2=str2.charAt(j);
if(c1==c2 || (c1=='z'? 'a' : (char)(c1 + 1)) == c2) {
j++; // Move to the next character in str2
}
}
return j == str2.length(); // Check if all characters in str2 are matched
}
}