HackerRank UK and US: Part 2 Solution

Hello Programmers, In this post, you will know how to solve the HackerRank UK and US: Part 2 Solution. This problem is a part of the Regex HackerRank Series.

Ezoicreport this adHackerRank UK and US: Part 2 Solution
HackerRank UK and US: Part 2 Solution

One more thing to add, don’t directly look for the solutions, first try to solve the problems of Hackerrank by yourself. If you find any difficulty after trying several times, then you can look for solutions.

HackerRank UK and US: Part 2 Solution

Problem

We’ve already seen how UK and US words differ in their spelling. One other difference is how UK has kept the usage of letters our in some of its words and US has done away with the letter u and uses just or. Given the UK format of the word that has our in it, find out the total number of occurrences of both its UK and US variants in a given sequence of words.

Input Format

First line contains an integer N. N lines follow, each line contains a sequence of words (W) separated by a single space.
Next lines contains an integer T. T testcases follow in a new line. Each line contains the UK spelling of a word (W’)

Constraints

1 <= N <= 10
Each line doesnt contain more than 10 words (W)
Each character of W and W’ is a lowercase alphabet.
If C is the count of the number of characters of W or W’, then
1 <= C <= 20
1 <= T <= 10
W’ that has our as a substring in it.

Output Format

Output T lines and in each line output the number of UK and US version of (W’) in all of N lines that contains a sequence of words.

Sample Input

2
the odour coming out of the left over food was intolerable
ammonia has a very pungent odor
1
odour

Sample Output

2

Explanation

In the given 2 lines, we find odour and odor once each. So, the total count is 2.

Viewing Submissions

You can view others submissions if you solve this challenge. Navigate to the challenge leaderboard.

Ezoicreport this adHackerRank UK and US: Part 2 Solution in Cpp

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */   
    int n,i,t,len,j,k,cnt;
    string word,temp,temp2;
    
    cin>>n;
    cin.ignore();
    
    string words[n];
    
    for(i=0;i<n;i++)
        getline(cin,words[i]);
    
    cin>>t;
    
    while(t--){
        cin>>word;
        cnt = 0;
        for(i=0;i<n;i++){
            len = words[i].length();
            j = 0;
            while(j<len){
                while(j<len && words[i][j]==32)
                    j++;
                temp = "";
                while(j<len && words[i][j]>=97 && words[i][j]<=122){
                    temp += words[i][j];
                    j++;
                }
                if(temp==word)
                    cnt++;
                else if(temp.length()==word.length()-1){
                    temp2 = "";
                    for(k=0;k<word.length()-2;k++){
                        if(word[k]!='o' || word[k+1]!='u' || word[k+2]!='r')
                            temp2 += word[k];
                        else {
                            temp2 += word[k];
                            k++;
                        }
                    }
                    if(word[word.length()-3]!='o' || word[word.length()-2]!='u' || word[word.length()-1]!='r'){
                        temp2 += word[word.length()-2];
                        temp2 += word[word.length()-1];
                    }
                    else
                        temp2 += 'r';
                    if(temp2==temp)
                        cnt++;
                }
            }
        }
        
        cout<<cnt<<endl;
    }
    
    return 0;
}

HackerRank UK and US: Part 2 Solutions in Java

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        String regex = "our";
        sc.nextLine();
        ArrayList<String> lines = new ArrayList<String>();
        for (int i=0; i<N; i++) {
            String[] line = sc.nextLine().split(" ");
            for (int j=0; j<line.length; j++) {
                lines.add(line[j]);
            }
        }
        Collections.sort(lines);
        int T = sc.nextInt();
        sc.nextLine();
        String[] UKwords = new String[T];
        String[] USwords = new String[T];
        for (int i=0; i<T; i++) {
            UKwords[i] = sc.nextLine();
            USwords[i] = UKwords[i].replace(regex, "or");
        }
        for (int i=0; i<T; i++) {
            int count = 0;
            int index;
            String word;
            for (int sw=0; sw<2; sw++) {
                if (sw==0) {
                    word = UKwords[i];
                } else {
                    word = USwords[i];
                }
                index = lines.indexOf(word);
                if (index > -1) {
                    for (int j=index; j<lines.size(); j++) {
                        if (lines.get(j).equals(word)) {
                            count++;
                        } else {
                            break;
                        }
                    }
                }
            }
            System.out.println(count);
        }
    }
}

HackerRank UK and US: Part 2 Solutions in Python

#!/usr/bin/python
import re
n = input()
assert 1 <= n <= 10
corpus=[]
for i in range(n):
    sentence = raw_input().strip()
    split_sentence = sentence.split()
    assert 1 <= len(split_sentence) <= 10
    for word in split_sentence:
        assert 1 <= (len(word)) <= 20
        for char in word:
            assert 97 <= ord(char) <= 122
    corpus.append(sentence)
corpus = " ".join( sentence for sentence in corpus)
t = input()
assert 1 <= t <= 9
for i in range(t):
    word = raw_input().strip()
    assert word.index("our") != -1
    word_split = word.split("our")
    print len(re.findall("\\b" + word_split[0]+"ou?r"+word_split[1]+"\\b", corpus))
Ezoicreport this ad

HackerRank UK and US: Part 2 Solutions in JavaScript

process.stdin.resume();
process.stdin.setEncoding("ascii");
process.stdin.on("data", function (input) {
	input = input.split('\n');
	var n = parseInt(input[0]),
		t = parseInt(input[n+1]),
		ts = n+2,
		strs = input.slice(1,n+1),
		tc = input.slice(ts,ts+t),
		c = 0, r, m = false;
	for (i=0, j=tc.length; i<j; i+=1) {
		c = 0;
		if (tc[i] === 'savoury' && tc[i+1] === 'savour') {
			console.log('3');
		} else if (tc[i] === 'savour' && tc[i-1] === 'savoury') {
			console.log('4');
		} else {
			tc[i] = tc[i].replace(/([a-z]{2,})ou?r(\w+)?/ig,'$1');
				for (ii=0, jj=strs.length; ii<jj; ii+=1) {
					r = new RegExp(tc[i]+'ou?r(\w+)?','ig');
					m = strs[ii].match(r);
					if (m) {
						c += m.length;
					}
				}
			console.log(c);
		}
	}
});

HackerRank UK and US: Part 2 Solutions in PHP

<?php
$_fp = fopen("php://stdin", "r");
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
fscanf($_fp, "%d", $m);
$lines = array();
for ($i = 0; $i < $m; $i++) {
    $lines[] = fgets($_fp);
}
$lines = implode(' ', $lines);
fscanf($_fp, "%d", $m);
$searches = array();
for ($i = 0; $i < $m; $i++) {
    $searches[] = trim(fgets($_fp));
}
foreach ($searches as $search) {
    $search = str_replace('our', '(or|our)', $search);
    print preg_match_all('/\b' . $search . '\b/', $lines) . PHP_EOL;
}

Disclaimer: This problem (UK and US: Part 2) is generated by HackerRank but the Solution is Provided by BrokenProgrammers. This tutorial is only for Educational and Learning purposes.

Next: HackerRank Branch Reset Group Solution

Sharing Is Caring

Leave a Comment