HackerRank The British and American Style of Spelling Solution

Hello Programmers, In this post, you will learn how to solve HackerRank The British and American Style of Spelling 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 ProgrammingHackerRank C++ ProgrammingHackerRank Java Programming, HackerRank Python ProgrammingHackerRank Linux ShellHackerRank SQL Programming, and HackerRank 10 days of Javascript.

HackerRank The British and American Style of Spelling Solution
HackerRank The British and American Style of Spelling Solution

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 StructuresRDBMS ProgramsJava Programs Solutions,  Fiverr Skills Test answersGoogle Course AnswersLinkedin Assessment, and Coursera Quiz Answers.

HackerRank The British and American Style of Spelling Solution

Problem

American English and British English differ in several aspects which are reflected in their spelling. One difference frequently observed, is that words written in American English, which have a suffix ze often end in se in British English. Given the American-English spelling of a word which ends in ze your task is to find the total count of all its British and American variants in all the given sequences of words. i.e. you need to account for the cases where the word occurs as it is given to you (i.e. the version ending in -ze) and you also need to find the occurances of its British-English counterparts (i.e, the version ending in -se).

Input Format

First line contains N, N lines follow each line contains a sequence of words (W) separated by a single space. Next line contains T. T testcases follow in a new line. Each line contains the American English 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’ ends with ze ( US version of the word)

Output Format

Output T lines and in each line output the total number of American and British versions of (W’) in all of N lines that contains a sequence of words.

Sample Input

2
hackerrank has such a good ui that it takes no time to familiarise its environment
to familiarize oneself with ui of hackerrank is easy
1
familiarize

Sample Output

2

Explanation

In the given 2 lines, we find familiarize and familiarise 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 The British and American Style of Spelling Solution in Cpp

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;


int main() {
    int N;
    cin >> N;
    
    vector<string> x(N);
    getline(cin, x[0]);
    for(int i = 0; i < N; i++)
        getline(cin, x[i]);
        
    int T;
    cin >> T;
    
    vector<string> ize(T);
    vector<string> ise(T);
    getline(cin, ize[0]);
    for(int i = 0; i < T; i++){
        getline(cin, ize[i]);
        int count = 0;
        ise[i] = ize[i];
        ise[i][ise[i].size()-2] = 's';
        
        for(int j = 0; j < N; j++){
            for(int index = 0; index < x[j].size() - ize[i].size(); index++)
                if(x[j].substr(index, ize[i].size()) == ize[i] || x[j].substr(index, ise[i].size()) == ise[i])
                count++;
        }
        cout << count << endl;
    }
      
    return 0;
}

HackerRank The British and American Style of Spelling 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(); sc.nextLine();

    String [][] lines = new String[n][];
    for (int i = 0; i < n; i++) {
      lines[i] = sc.nextLine().split(" ");
    }
    int t = sc.nextInt(); sc.nextLine();
    for (int i = 0; i < t; i++) {
      String ukWord = sc.next();
      String usWord = ukWord.substring(0,ukWord.length() - 2) + "se";
      int ans = 0;
      assert ( ukWord.endsWith("ze") );
      for ( String[] line : lines ){
        for ( String word : line ){
          if ( word.equals(ukWord) || word.equals(usWord) ){
            ++ans;
          }
        }
      }
      System.out.println(ans);
    }
  }
}

HackerRank The British and American Style of Spelling Solution in Python

n = input()
words = []
for i in xrange(n):
    words += raw_input().strip().split()
t = input()
for i in xrange(t):
    uk = raw_input()
    us = uk[:-2]+"se"
    print len(filter(lambda x: x == uk or x == us, words))

HackerRank The British and American Style of Spelling Solution in JavaScript

process.stdin.resume();
process.stdin.setEncoding("ascii");
process.stdin.on("data", function (input) {
	input = input.match(/^(.*)$/igm);
	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;
		tc[i] = tc[i].replace(/([^ ]+)[iy][sz](?:e|ing)/ig,'$1');
			for (ii=0, jj=strs.length; ii<jj; ii+=1) {
				r = new RegExp(tc[i]+'[iy][sz](?:e|ing)','ig');
				m = strs[ii].match(r);
				if (m) {
					c += m.length;
				}
			}
		process.stdout.write(c+'\n');
	}
});

HackerRank The British and American Style of Spelling 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 = ' ' . str_replace(' ', '  ', implode(' ', $lines)) . ' ';
fscanf($_fp, "%d", $m);
$searches = array();
for ($i = 0; $i < $m; $i++) {
    $searches[] = trim(fgets($_fp));
}
foreach ($searches as $search) {
    $search = substr($search, 0, strlen($search) - 2);
    print preg_match_all('/\s' . $search . '(ze|se)\s/', $lines) . PHP_EOL;
}

Disclaimer: This problem (The British and American Style of Spelling) 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. 

Sharing Is Caring

Leave a Comment