Problem Statement
A description of the problem can be found on Hackerrank.
Solution
For each word in each sentence check if the word contains "ze"
(US) or "se"
(UK).
I created solution in:
All solutions are also available on my GitHub.
Scala
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
import scala.io.Source object AmericanEnglishSpelling extends App { val lines = Source.stdin.getLines().toList val n = lines(0).toInt val words = lines.slice(1, n + 1).map(_.split(" ")).flatten val tests = lines(n + 1).toInt val testWords = lines.drop(n + 2) val counts = testWords.map(w => words.count(isUsOrUkSpelling(_, w))) println(counts.mkString("\n")) def isUsOrUkSpelling(word: String, matching: String): Boolean = { word.matches(matching) || word.matches(matching.replaceAll("ze", "se")) } } |
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
lines = gets.chomp.to_i all_words = [] lines.times do words = gets.chomp.split.map { |s| s.to_s } words.each { |w| all_words << w} end tests = gets.chomp.to_i tests.times do us_word = gets.chomp.to_s uk_word = us_word.sub("ze", "se") count = 0 all_words.each do |word| count += 1 if word == us_word || word == uk_word end puts count end |
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
function processData(input) { var lines = input.split("\n"); var n = parseInt(lines[0]); var words = []; for(var i = 1; i <= n; i++) { var arr = lines.split(" "); for(var j = 0; j < arr.length; j++) { words.push(arr[j]); } } var tests = parseInt(lines[n + 1]); for(var t = n + 1; t <= n + tests; t++) { var count = 0; var american = lines[t]; var british = lines[t].replace(/ze/g, "se"); for(var w = 0; w < words.length; w++) { if(w.match(american) || w.match(british)) { count++; } } console.log(count); } } process.stdin.resume(); process.stdin.setEncoding("ascii"); _input = ""; process.stdin.on("data", function (input) { _input += input; }); process.stdin.on("end", function () { processData(_input); }); |
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.Scanner; public class BritishAndAmericanSpelling { public static void main(String[] args) { Scanner stdin = new Scanner(System.in); int n = Integer.parseInt(stdin.nextLine()); List<String> words = new ArrayList<>(); for(int i = 0; i < n; i++) { String[] s = stdin.nextLine().split(" "); words.addAll(Arrays.asList(s)); } int tests = Integer.parseInt(stdin.nextLine()); for(int i = 0; i < tests; i++) { int count = 0; String american = stdin.nextLine(); String british = american.replaceAll("ze", "se"); for(int j = 0; j < words.size(); j++) { if(words.get(j).matches(american) || words.get(j).matches(british)) { count++; } } System.out.println(count); } stdin.close(); } } |