输入两个字符串S
和L
,都只包含小写字母,
S长度 <= 100
,L长度 <= 500000
,
判断S
是否是L
的有效子字符串,
判定规则:S
中的每个字符在L
中都能找到(可以不连续)
且S
在L
中字符的前后顺序与S
中顺序要保持一致
例如:
S="ace"
是L="abcde"
的一个子序列
且有效字符是a
、c
、e
,
而"aec"
不是有效子序列,且有效字符只有a、e
。
输入两个字符串S
和L
,都只包含小写字母,
S长度 <= 100
,L长度 <= 500000
,
先输入S
再输入L
每个字符串占一行
S
串最后一个有效字符在L
中的位置
首位从0
开始计算
无有效字符返回-1
acce
abcde
4
fgh
abcde
-1
import java.util.Scanner;
/**
* Created with IntelliJ IDEA.
* Author: Amos
* E-mail: amos@amoscloud.com
* Date: 2022/5/9
* Time: 16:48
* Description:
*/
public class Main0090 {
public static void main(String[] args) {
try (Scanner scanner = new Scanner(System.in)) {
String S = scanner.nextLine();
String L = scanner.nextLine();
solution(S, L);
}
}
public static void solution(String s, String l) {
int n = s.length(), m = l.length();
int i = 0, j = 0;
int pos = 0;
while (i < n && j < m) {
if (s.charAt(i) == l.charAt(j)) {
pos = j;
i++;
}
j++;
}
if (i == n) {
System.out.println(pos);
} else {
System.out.println(-1);
}
}
}