Find A Sub-Word

Sort by

recency

|

159 Discussions

|

  • + 0 comments

    Pthon code:

    import re
    
    # Enter your code here. Read input from STDIN. Print output to STDOUT
    n = int(input())
    if not (1 <= n <= 100):
        raise ValueError("Number of lines must be between 1 and 100")
    lines = []
    for i in range(n):
        lines.append(input())
    q = int(input())
    if not (1 <= q <= 100):
        raise ValueError("Queries must be between 1 and 10")
    queries = []
    for i in range(q):
        queries.append(input())
    
    # print(n)
    # print(lines)
    # print(q)
    # print(queries)
    
    patterns = []
    for query in queries:
        pattern = '[A-Za-z0-9_]'+query+'[A-Za-z0-9_]'
        patterns.append(pattern)
    
    sum = 0
    for patten in patterns:
        for line in lines:
            x = re.findall(patten,line)
            sum = sum + len(x)
        print(sum)
        sum = 0
    
  • + 0 comments

    Python3:

    import re
    import sys
    
    data = sys.stdin.read()
    
    pattern=r'\n[0-9]*\n([A-z\n]*)'
    
    matches = re.findall(pattern, data)
    
    for word in matches[0].split("\n"):
        myregex= r"([A-z]" + re.escape(word) + r"[A-z])"
        print(len(re.findall(myregex, data, re.IGNORECASE)))
    
  • + 0 comments

    Bash :

    #!/bin/bash
    
    read count
    allString=""
    for (( i=1; i<=$count; i++ ))
    do
        read string 
        allString+="$string " 
    done   
    
    read subWordInt
    for  (( i=1; i<=$subWordInt; i++ ))
    do
        read subSequent
    echo "$allString" | grep -o -E "\B($subSequent)\B" | wc -l
    done
        
    
  • + 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= Integer.parseInt(scanner.nextLine());
            String array[]=new String[n];
            for (int i=0;i<n;i++)   array[i]=scanner.nextLine();
            int q = Integer.parseInt(scanner.nextLine());
            for (int i=0;i<q;i++)
            {   String s = scanner.nextLine();
                String regex = String.format("\\w%s\\w", s);
                Pattern pattern = Pattern.compile(regex);  
                int count=0;
                for(int j=0;j<n;j++)
                {   Matcher matcher = pattern.matcher(array[j]);
                    while(matcher.find())   count++;
                }
                System.out.println(count);
            }
        }
    }
    
  • + 0 comments

    Javascript:

    const lines = input.split(/^\d+\s(?=.*$)/gm);
    const words = lines[2].split("\n");
    words.forEach((word) => {
      const regex = new RegExp(`(?<!\\b)${word}(?!\\b)`, 'g');
      const sum = lines[1].match(regex)?.length || 0;
      console.log(sum);
    });