HackerRank The British and American Style of Spelling Solution

Hello Programmers, In this post, you will know how to solve the HackerRank The British and American Style of Spelling Solution. This problem is a part of the Regex HackerRank Series.

HackerRank The British and American Style of Spelling Solution
HackerRank The British and American Style of Spelling 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 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.

Ezoicreport this adHackerRank The British and American Style of Spelling Solutions 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 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(); 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 Solutions 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))
Ezoicreport this ad

HackerRank The British and American Style of Spelling Solutions 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 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 = ' ' . 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 BrokenProgrammers. This tutorial is only for Educational and Learning purposes.

Next: HackerRank Building a Smart IDE: Programming Language Detection Solution

Sharing Is Caring

Leave a Comment

Ezoicreport this ad