Sort by

recency

|

129 Discussions

|

  • + 0 comments

    Python 3

    # Enter your code here. Read input from STDIN. Print output to STDOUT
    
    import re
    
    sentences = []
    N = int(input())
    for _ in range(N):
        sentences.append(str(input()))
    T = int(input())
    for _ in range(T):
        total = 0
        word = str(input())
        pattern = r'(?:(?<=\b)|(?<=\W))'+word+r'(?=\b|\W)'
        for sentence in sentences:
            wfound = re.findall(pattern, sentence)
            total += len(wfound)
        print(total)
    
  • + 0 comments

    Java 15

    import java.io.*;
    import java.util.*;
    import java.util.regex.Pattern;
    import java.util.regex.Matcher;
    
    public class Solution {
        public static void main(String[] args) {
            Scanner scanner = new Scanner(System.in);
            int n = scanner.nextInt(); scanner.nextLine(); 
            String sentences[] = new String [n];
            for(int i=0;i<n;i++)    sentences[i]=scanner.nextLine();
            int t = scanner.nextInt(); scanner.nextLine(); 
            int output[] = new int [t];
            for(int i=0;i<t;i++)
            {   String word = scanner.nextLine(); 
                output[i]=0;
                String regex = "(?<=^|[\\W])" + word + "(?=$|[\\W])";
                // String regex = "\\b"+ word +"\\b";          //WORKS
                Pattern pattern = Pattern.compile(regex);
                for(int j=0;j<n;j++)
                {   Matcher matcher = pattern.matcher(sentences[j]);
                    while(matcher.find())   output[i]++;
                }
            }
            for(int i=0;i<t;i++)    System.out.println(output[i]);
        }
    }
    
  • + 1 comment

    This code passes only the first test case. Can't see anything wrong with the code or the pattern:

    import re
    
    sentences = "".join([input() for _ in range(int(input()))])
    words = [input() for _ in range(int(input()))]
    
    for w in words:
        regex = f"\\b{w}\\b"
        try:
            print(len(re.findall(regex, sentences)))
        except TypeError:
            print(0)
    
  • + 0 comments

    nyünyünyű vau:

    import re
    from collections import Counter
    
    word_regex = re.compile(r'[a-zA-Z0-9_]+')
    
    n = int(input())
    word_counter = Counter()
    for _ in range(n):
        word_counter.update(word_regex.findall(input()))
        
    t = int(input())
    for _ in range(t):
        print(word_counter[input()])
    
  • + 0 comments
    "\\b"+str(wo)+"\\b"
    

    why some times \b workes but some times \b worked???? any on eknow it

    a