UK and US: Part 2

  • + 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'))
    }