Hackerrank – Designer PDF Viewer
Hackerrank – Problem Statement A description of the problem can be found on Hackerrank. Solution Find a max height of given characters. Square is max height multiplied with length of the string. I created solution in: Scala Java JavaScript Ruby 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 |
import java.util.*; public class Solution { public static void main(String[] args) { Scanner stdin = new Scanner(System.in); Object[] heights = Arrays.stream(stdin.nextLine().split(" ")) .map(Integer::parseInt).toArray(); String word = stdin.nextLine(); int maxHeight = 0; for(int i = 0; i < word.length(); i++) { int charIndex = word.charAt(i) - 'a'; int height = (int) heights[charIndex]; if(height > maxHeight) { maxHeight = height; } } long square = word.length() * maxHeight; System.out.println(square); stdin.close(); } } |
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 |
'use strict'; const processData = input => { const lines = input.split('\n'); const heights = lines[0].split(' ').map(i => parseInt(i)); const word = lines[1]; let maxHeight = 0; for(let i = 0; i < word.length; i++) { const charIndex = word[i].codePointAt() - 97; const height = heights[charIndex]; if(height > maxHeight) { maxHeight = height; } } const square = word.length * maxHeight; console.log(square); }; process.stdin.resume(); process.stdin.setEncoding("ascii"); let _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 |
import scala.io.Source object DesignerPdfViewer extends App { val lines = Source.stdin.getLines().toList val heights = lines.head.split(" ").map(_.toInt) val word = lines(1) val characterIndexes = word.map(_ - 'a') val maxHeight = characterIndexes.map(heights(_)).max val square = word.length * maxHeight println(square) } } |
Ruby […]