Created
February 16, 2020 00:21
-
-
Save tuanna-hsp/7e44f8c3b5a0c74561bfe6d837cec74e to your computer and use it in GitHub Desktop.
Is subsequence
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public boolean isSubsequence(String s, String t) { | |
if (s.isEmpty()) { | |
return true; | |
} | |
int lastBreak = 0; | |
int sIndex = 0; | |
int tIndex = 0; | |
int sLength = s.length(); | |
int tLength = t.length(); | |
while (sIndex < sLength) { | |
char sChar = s.charAt(sIndex); | |
while (tIndex < tLength) { | |
if (t.charAt(tIndex) == sChar) { | |
if (sIndex == sLength - 1) { | |
return true; | |
} | |
tIndex++; | |
break; | |
} | |
tIndex++; | |
} | |
sIndex++; | |
} | |
return false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment