你可以认为 s 和 t 中仅包含英文小写字母。字符串 t 可能会很长(长度n ~= 500,000),而 s 是个短字符串(长度 <=100)。
字符串的一个子序列是原始字符串删除一些(也可以不删除)字符而不改变剩余字符相对位置形成的新字符串。(例如,"ace"是"abcde"的一个子序列,而"aec"不是)。
进阶:时间复杂度
,空间复杂度%5C)
你可以认为 s 和 t 中仅包含英文小写字母。字符串 t 可能会很长(长度n ~= 500,000),而 s 是个短字符串(长度 <=100)。
共两行,第一行为字符串s, 第二行为字符串t
字符串t的长度 1<=n<=500000
字符串s的长度 1<=m<=100
输出true或者是false,true表示是s是t的子序列,false表示s不是t的子序列
abc ahbgdc
true
axc ahbgdc
false
public boolean isStr(String s, String t){
if(s.isEmpty())
return true;
int index = 0;
for (char c : t.toCharArray()) {
if (s.charAt(index)==c&&++index==s.length())
return true;
}
return false;
} import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String s = input.next();
String t = input.next();
if (s.length() > t.length()) {
System.out.print("false");
}
else {
int si = 0, ti = 0;
while (si < s.length() && ti < t.length()) {
if (s.charAt(si) == t.charAt(ti)) {
si++;
}
ti++;
}
if (si == s.length()) {
System.out.print("true");
} else {
System.out.print("false");
}
}
}
} 双指针,字符匹配时向后移动s的指针,且无论是否匹配,都移动 t 的指针;最后s的指针达到了s的末尾,证明s是t的子序列
import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String subString = in.next();
String fullString = in.next();
System.out.println(isSubsequence(subString, fullString));
}
public static boolean isSubsequence(String s, String t) {
int i = 0, j = 0;
int m = s.length(), n = t.length();
while (i < m && j < n) {
if (s.charAt(i) == t.charAt(j)) {
i++;
}
j++;
}
return i == m;
}
} import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// 注意 hasNext 和 hasNextLine 的区别
String sub = in.nextLine();
String str = in.nextLine();
int index = 0;
for (int i = 0; i < sub.length(); i++) {
for (; index < str.length(); index++) {
if (sub.charAt(i) == str.charAt(index)) {
if (i == sub.length() - 1) {
System.out.println(true);
return;
}
index++;
break;
}
}
}
System.out.println(false);
}
} /**
* 判断是不是子字符串
* 双指针法
* @return
*/
public static boolean isChildStr(String longStr, String shortStr) {
int ss = 0,ls = 0;
while (ls < longStr.length()){
if (shortStr.charAt(ss) == longStr.charAt(ls)){
if (ss == shortStr.length()-1) return true;
ss++;
ls++;
}else {
ls++;
}
}
if (ss == shortStr.length()-1){
return longStr.charAt(longStr.length()-1) == shortStr.charAt(shortStr.length()-1);
}else
return false;
} import java.util.Scanner;
public class Main21 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String t = sc.nextLine();
String s = sc.nextLine();
int p1=0;
int p2=0;
for(int i=0;i<t.length();i++){
if(t.charAt(p1)==s.charAt(p2)){
p1++;
p2++;
}else if(t.charAt(p1) != s.charAt(p2)) p1++;
if(p1>t.length()||p2>s.length()){
break;
}
}
System.out.println(p2>s.length()-1);
}
} public static void main(String[] args) { Scanner in =new Scanner(System.in); String s=in.nextLine(); String k=in.nextLine(); int i=0,j=0,sum=0; if(s.length()>k.length()){ System.out.println(false); return; } while (i<s.length()&&j<k.length()){ if(s.charAt(i)==k.charAt(j)){ i++; sum++; } j++; } if(sum==s.length()){ System.out.println(true); }else { System.out.println(false); } }
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("input zi chuan");
String childrenString = input.next();
System.out.println("input fu chuan");
String superString = input.next();
System.out.println(panduan(childrenString,superString));
}
public static boolean panduan(String zc, String fc){
String s = fc;
for(int i =0 ; i < zc.length();i++){
char ch = zc.charAt(i);
//在父串中组合子串
s = s.substring(s.indexOf(ch));
if(s.indexOf(ch) == -1 )
return false;
}
return true;
}
}