Hello Programmers, In this post, you will learn how to solve HackerRank UK and US: Part 2 Solution. This problem is a part of the Regex HackerRank Series.
One more thing to add, don’t straight away look for the solutions, first try to solve the problems by yourself. If you find any difficulty after trying several times, then look for the solutions. We are going to solve the Regex HackerRank Solutions using CPP, JAVA, PYTHON, JavaScript & PHP Programming Languages.
You can practice and submit all HackerRank problem solutions in one place. Find a solution for other domains and Sub-domain. I.e. Hacker Rank solution for HackerRank C Programming, HackerRank C++ Programming, HackerRank Java Programming, HackerRank Python Programming, HackerRank Linux Shell, HackerRank SQL Programming, and HackerRank 10 days of Javascript.

As you already know that this site does not contain only the Hacker Rank solutions here, you can also find the solution for other problems. I.e. Web Technology, Data Structures, RDBMS Programs, Java Programs Solutions, Fiverr Skills Test answers, Google Course Answers, Linkedin Assessment, and Coursera Quiz Answers.
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 doesn‘t 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 sub–string 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.
HackerRank 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 Solution 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 Solution 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))
HackerRank UK and US: Part 2 Solution 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 Solution 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 Chase2learn. This tutorial is only for Educational and Learning purposes.
FAQ:
1. How do you solve the first question in HackerRank?
If you want to solve the first question of Hackerrank then you have to decide which programing language you want to practice i.e C programming, Cpp Programing, or Java programming then you have to start with the first program HELLO WORLD.
2. How do I find my HackerRank ID?
You will receive an email from HackerRank to confirm your access to the ID. Once you have confirmed your email, the entry will show up as verified on the settings page. You will also have an option to “Make primary”. Click on that option. Read more
3. Does HackerRank detect cheating?
yes, HackerRank uses a powerful tool to detect plagiarism in the candidates’ submitted code. The Test report of a candidate highlights any plagiarized portions in the submitted code and helps evaluators to verify the integrity of answers provided in the Test.
4. Does HackerRank use camera?
No for coding practice Hackerrank does not use camera but for companies’ interviews code submission time Hackerrank uses the camera.
5. Should I put HackerRank certificate on resume?
These certificates are useless, and you should not put them on your resume. The experience you gained from getting them is not useless. Use it to build a portfolio, and link to it on your resume.
6. Can I retake HackerRank test?
The company which sent you the HackerRank Test invite owns your Test submissions and results. It’s their discretion to permit a reattempt for a particular Test. If you wish to retake the test, we recommend that you contact the concerned recruiter who invited you to the Test and request a re-invite.
7. What is HackerRank?
HackerRank is a tech company that focuses on competitive programming challenges for both consumers and businesses. Developers compete by writing programs according to provided specifications. Wikipedia
Finally, we are now, in the end, I just want to conclude some important message for you
Note:- I compile all programs, if there is any case program is not working and showing an error please let me know in the comment section. If you are using adblocker, please disable adblocker because some functions of the site may not work correctly.
Please share our posts on social media platforms and also suggest to your friends to Join Our Groups. Don’t forget to subscribe.