Problem Statement
A description of the problem can be found on Hackerrank.
Solution
Print “0” if the input matches ^hackerrank$
or ^hackerrank.*hackerrank$
Print “1” if the input matches ^hackerrank.*
Print “2” if the input matches .*hackerrank$
else print “-1”
I created solution in:
All solutions are also available on my GitHub.
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 |
import java.util.*; public class FindHackerrank { public static void main(String[] args) { Scanner stdin = new Scanner(System.in); int tests = Integer.parseInt(stdin.nextLine()); for(int i = 0; i < tests; i++) { String input = stdin.nextLine(); System.out.println(findHackerrank(input)); } stdin.close(); } private static int findHackerrank(String line) { if(line.matches("^hackerrank$") || line.matches("^hackerrank.*hackerrank$")) return 0; else if(line.matches("^hackerrank.*")) return 1; else if(line.matches(".*hackerrank$")) return 2; else return -1; } } |
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 |
'use strict'; let findHackerrank = line => { if(line.match(/^hackerrank$/) || line.match(/^hackerrank.*hackerrank$/)) return "0"; else if(line.match(/^hackerrank.*/)) return "1"; else if(line.match(/.*hackerrank$/)) return "2"; else return "-1"; }; const processData = input => { let lines = input.split('\n').slice(1); console.log(lines.filter(line => line.match(/[a-z]+/)).map(findHackerrank).join('\n')) }; process.stdin.resume(); process.stdin.setEncoding("ascii"); var _input = ""; process.stdin.on("data", input => _input += input); process.stdin.on("end", () => processData(_input)); |
Scala
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
import scala.io.Source object FindHackerRank extends App { val lines = Source.stdin.getLines().drop(1) val outputNums = lines.map(findHackerrank) println(outputNums.mkString("\n")) def findHackerrank(line: String): Int = { if(line.matches("^hackerrank$") || line.matches("^hackerrank.*hackerrank$")) 0 else if(line.matches("^hackerrank.*")) 1 else if(line.matches(".*hackerrank$")) 2 else -1 } } |
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 |
gets.chomp.to_i.times do input = gets.chomp.to_s if input =~ /^hackerrank$/ || input =~ /^hackerrank.*hackerrank$/ puts "0" elsif input =~ /^hackerrank.*/ puts "1" elsif input =~ /.*hackerrank$/ puts "2" else puts "-1" end end |