题目地址:
https://www.lintcode.com/problem/delete-characters/description
给定两个字符串 s s s和 t t t,判断 t t t是否是 s s s的子序列。
代码如下:
public class Solution {
/**
* @param s: The string s
* @param t: The string t
* @return: Return if can get the string t
*/
public boolean canGetString(String s, String t) {
// Write your code here
int idx1 = 0, idx2 = 0;
while (idx1 < s.length() && idx2 < t.length()) {
if (s.charAt(idx1) == t.charAt(idx2)) {
idx2++;
}
idx1++;
}
return idx2 == t.length();
}
}
时间复杂度 O ( l s ) O(l_s) O(ls),空间 O ( 1 ) O(1) O(1)。