Hello Programmers, In this post, you will know how to solve the HackerRank Designer PDF Viewer Solution. This problem is a part of the HackerRank Algorithms Series.
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 Designer PDF Viewer Solution
Task
When a contiguous block of text is selected in a PDF viewer, the selection is highlighted with a blue rectangle. In this PDF viewer, each word is highlighted independently. For example:
There is a list of 26 character heights aligned by index to their letters. For example, ‘a’ is at index 0 and ‘z’ is at index 25. There will also be a string. Using the letter heights given, determine the area of the rectangle highlight in mm2 assuming all letters are 1mm wide.
Example
h = [1, 3, 1, 3, 1, 4, 1, 3, 2, 5, 5, 5, 5, 1, 1, 5, 5, 1, 5, 2, 5, 5, 5, 5, 5, 5] word =’torn‘
The heights are t = 2, o = 1, r = 1 and n = 1. The tallest letter is 2 high and there are 4 letters. The hightlighted area will be 2*4 = 8mm2 so the answer is 8.
Function Description
Complete the designerPdfViewer function in the editor below.
designerPdfViewer has the following parameter(s):
- int h[26]: the heights of each letter
- string word: a string
Returns
- int: the size of the highlighted area
Input Format
The first line contains 26 space-separated integers describing the respective heights of each consecutive lowercase English letter, ascii[a-z].
The second line contains a single word consisting of lowercase English alphabetic letters.
Constraints
- 1 <= h[?] <= 7 where ? is an English lowercase letter.
- word contains no more than 10 letters.
Sample Input 0
1 3 1 3 1 4 1 3 2 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 abc
Sample Output 0
9
Explanation 0
Letter heights are a = 1, b = 3 and c = 1. The tallest letter, b
, is 3mm high. The selection area for this word is
3 * 1mm * 3mm = 9mm2.
Note: Recall that the width of each character is 1mm.
Sample Input 1
1 3 1 3 1 4 1 3 2 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 7 zaba
Sample Output 1
28
Explanation 1
The tallest letter in zaba is z at 7mm. The selection area for this word is 4 x 1mm x 7mm = 28mm2.
HackerRank Designer PDF Viewer Solution
Designer PDF Viewer Solution in C
#include <math.h> #include <stdio.h> #include <string.h> #include <stdlib.h> #include <assert.h> #include <limits.h> #include <stdbool.h> int main(){ int n=26; int *h = malloc(sizeof(int) * n); for(int h_i = 0; h_i < n; h_i++){ scanf("%d",&h[h_i]); } char* word = (char *)malloc(512000 * sizeof(char)); scanf("%s",word); int l=strlen(word); int i; int max=0; for(i=0;i<l;i++) { if(h[(int)word[i]-'a']>max) max=h[(int)word[i]-'a']; } printf("%d",max*l); return 0; }
Designer PDF Viewer Solution in Cpp
#include <bits/stdc++.h> using namespace std; int a[42]; char s[1231212]; int main() { for (int i = 0; i < 26; i++) { scanf("%d", a + i); } scanf("%s", s); int h = 0; int w = 0; for (int i = 0; s[i]; i++) { w++; h = max(h, a[s[i] - 'a']); } printf("%d\n", h * w); return 0; }
Designer PDF Viewer 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 in = new Scanner(System.in); int n = 26; int h[] = new int[n]; for(int h_i=0; h_i < n; h_i++){ h[h_i] = in.nextInt(); } String word = in.next(); int mx = 0; for (int i = 0; i < word.length(); i++) { int f = h[(int) (word.charAt(i) - 'a')]; if (f > mx) { mx = f; } } System.out.println((word.length() * mx)); } }
Designer PDF Viewer Solution in Python
import sys h = map(int,raw_input().strip().split(' ')) word = raw_input().strip() hi = 0 for c in word: hi = max(hi, h[ord(c)-ord('a')]) print hi * len(word)
Designer PDF Viewer Solution using JavaScript
process.stdin.resume(); process.stdin.setEncoding('ascii'); var input_stdin = ""; var input_stdin_array = ""; var input_currentline = 0; process.stdin.on('data', function (data) { input_stdin += data; }); process.stdin.on('end', function () { input_stdin_array = input_stdin.split("\n"); main(); }); function readLine() { return input_stdin_array[input_currentline++]; } /////////////// ignore above this line //////////////////// function main() { h = readLine().split(' '); h = h.map(Number); var word = readLine().split(""); var maxH = 0; word.forEach((w) => { var char = w.charCodeAt(0) - 97; maxH = Math.max(h[char],maxH); }); console.log(maxH * word.length); }
Designer PDF Viewer Solution in Scala
object Solution { def main(args: Array[String]) { val sc = new java.util.Scanner (System.in); val n = 26; var h = new Array[Int](n); for(h_i <- 0 to n-1) { h(h_i) = sc.nextInt(); } var word = sc.next(); println(word.size*word.toCharArray.map(c=>h(c.toInt-97)).toSeq.max); } }
Designer PDF Viewer Solution in Pascal
Disclaimer: This problem (Designer PDF Viewer) is generated by HackerRank but the Solution is Provided by BrokenProgrammers. This tutorial is only for Educational and Learning purposes.