题解 | #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)

全部评论

相关推荐

码农索隆:单休一个月少休息4天,一年就是48天,平时节假日,别人3天假期,单休的两天
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
今天 11:31
点赞 评论 收藏
分享
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

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