s与t的指针从0开始往右遍历一趟,相等就都下移一位,不等就仅s的指针下移一位:
import java.util.*; public class Main { public static void main(String[] args) throws Exception{ Scanner sc = new Scanner(System.in); String s = sc.nextLine(), t = sc.nextLine(); int i = 0, j = 0; while(i < s.length()){ if(j >= t.length()) break; if(s.charAt(i) == t.charAt(j)) { if(j == t.length() - 1){ System.out.println("Yes"); return; } i++; j++; } else i++; } System.out.println("No"); } }