UK and US: Part 2

Sort by

recency

|

88 Discussions

|

  • + 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());
            
            //Merge all input together
            String input="";
            for(int i=0;i<n;i++)
                input=input+scanner.nextLine()+" ";
            
            int t = Integer.parseInt(scanner.nextLine());
            for(int i=0;i<t;i++)
            {   String uk_word = scanner.nextLine();
                String us_word = uk_word.replaceAll("our","or");
                String regex = String.format("\\b(%s|%s)\\b",uk_word,us_word);
                Pattern pattern = Pattern.compile(regex);
                Matcher matcher = pattern.matcher(input);
                int count=0;
                while(matcher.find())   count++;
                System.out.println(count);
            }
        }
    }
    
  • + 0 comments

    Javascript:

    const lines = input.split(/^\d+\s*$/gm);
    const words = lines[2].trim().split("\n");
    words.forEach((word) => {
       const regex = new RegExp(`\\b(${word}|${word.replace('our', 'or')})\\b`, 'gm');
       const total = lines[1].match(regex);
    
       console.log(total?.length || 0);
    })
    
  • + 0 comments
    import re
    
    content = ''
    for _ in range(int(input())):
        content += input() + ' '
        
    for _ in range(int(input())):
        uk = input()
        us = uk.replace('our', 'or')
        pattern = f'\\b({uk}|{us})\\b'
        print(len(re.findall(pattern, content)))
    
  • + 0 comments
    s = " ".join([input() for _ in range(int(input()))]).split()
    
    for _ in range(int(input())):
        i = input()
        print(s.count(i) + s.count(i.replace("our", "or")))
    
  • + 0 comments

    JS solution

    function processData(input) {
    
        let dataarray = input.split('\n')
        const N = dataarray.shift()
        const sentences = dataarray.splice(0, N)
        const T = dataarray.shift()
        const testcases = dataarray.splice(0, T)
    
        let counts = testcases.reduce((obj, c) => { obj[c] = 0; return obj; }, {});
    
        for (let tc of testcases) {
    
            const regex = new RegExp(`\\b${tc.trim()}\\b`, 'g')
    
            for (let sentence of sentences) {
                const fsentence = sentence.replaceAll('or', 'our')
                let tccount = fsentence.match(regex)?.length
    
                if (tccount)
                    counts[tc] = counts[tc] + tccount
            }
        }
        
        console.log(Object.values(counts).join('\n'))
    }