题解 | #String Matching#

String Matching

https://www.nowcoder.com/practice/00438b0bc9384ceeb65613346b42e88a

//Java版代码
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String t = sc.next();
        String p = sc.next();
        int count = 0;
        int pos = 0;
        while (pos != -1) {
            pos = t.indexOf(p, pos);
            if (pos != -1) {
                count++;
                pos++;
            }
        }
        System.out.println(count);
    }
}
#Python版代码
import re
t, p = input().split()
pattern = '(?=' + p + ')'
count = len(re.findall(pattern, t))
print(count)

全部评论

相关推荐

07-07 12:47
门头沟学院 Java
码农索隆:竟然还真有卡体检报告的
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务