HackerRank Designer PDF Viewer Solution
In this post, We are going to solve HackerRank Designer PDF Viewer
Problem. 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 ‘2’ is at index 25. There will also be a string. Using the letter heights given
determine the area of the rectangle highlighted assuming all letters are Imm 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 tore The heights are t = 2, 0 = 1, r= 1, and n = 1 The tallest letter is 2 high and there are 4 letters. The highlighted area will be 8mm 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.
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
We are highlighting the word abc
:
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 = 9mm.
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 1mmx 7mm = 28mm.
Designer PDF Viewer C Solution
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <stdbool.h>
int main(){int i;
int *h = malloc(sizeof(int) * 26);
for( i = 0; i < 26; i++){
scanf("%d",&h[i]);
}
char* word = (char *)malloc(512000 * sizeof(char));
scanf("%s",word);
int max=h[word[0]-97];
for(i=0;word[i]!='\0';i++)
{
if(max<h[word[i]-97])
max=h[word[i]-97];
}
printf("%d",strlen(word)*max);
return 0;
}
Designer PDF Viewer C++ Solution
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
int a[30];
for(int i = 0; i < 26; i++) {
cin >> a[i];
}
string s;
cin >> s;
int mx = 0;
for(int i = 0; i < s.size(); i++) {
mx = max(mx, a[s[i] - 'a']);
}
cout << mx * s.size() << endl;
return 0;
}
Designer PDF Viewer C Sharp Solution
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
class Solution {
static void Main(String[] args) {
string[] h_temp = Console.ReadLine().Split(' ');
int[] h = Array.ConvertAll(h_temp,Int32.Parse);
string word = Console.ReadLine();
int max =0;
foreach(var t in word){
int index = (int)t - (int)'a';
if (max<h[index])
max=h[index];
}
Console.WriteLine(word.Length*max);
}
}
Designer PDF Viewer Java Solution
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 maxHeight = 0;
for(char ch = 'a'; ch < 'z'; ch++) {
if(word.contains(ch + "") && h[ch - 'a'] > maxHeight)
maxHeight = h[ch - 'a'];
}
System.out.println(maxHeight * word.length());
}
}
Designer PDF Viewer JavaScript Solution
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();
word = word.split('');
var maxH = 0;
for(var i = 0; i < word.length; i++){
if(maxH < h[word[i].charCodeAt() - 97]){
maxH = h[word[i].charCodeAt() - 97];
}
}
console.log(word.length * maxH);
}
Designer PDF Viewer Python Solution
#!/bin/python3
import sys
h = [int(h_temp) for h_temp in input().strip().split(' ')]
word = input().strip()
print(max([h[ord(c) - 97] for c in word])*len(word))
Other Solution