Designer PDF Viewer Hackerrank problem solution

Designer PDF Viewer

When you select a contiguous block of text in a PDF viewer, the selection is highlighted with a blue rectangle. In a new kind of PDF viewer, the selection of each word is independent of the other words; this means that each rectangular selection area forms independently around each highlighted word. For example:
Designer PDF Viewer

In this type of PDF viewer, the width of the rectangular selection area is equal to the number of letters in the word times the width of a letter, and the height is the maximum height of any letter in the word.

Consider a word consisting of lowercase English alphabetic letters, where each letter is 1mm wide. Given the height of each letter in millimeters (mm), find the total area that will be highlighted by blue rectangle in mm^2 when the given word is selected in our new PDF viewer.

Input Format

The first line contains 26 space-separated integers describing the respective heights of each consecutive lowercase English letter (i.e., ha, hb,……, hy, hz).
The second line contains a single word, consisting of lowercase English alphabetic letters.


h1a, where 1a is an English lowercase letter.
Word contains no more than 10 letters.
Output Format

Print a single integer denoting the area of highlighted rectangle when the given word is selected. The unit of measurement for this is square millimeters (mm^2), but you must only print the integer.

Sample Input

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
Sample Output


We are highlighting the word abc:

The tallest letter in abc is b, and hb=3. The selection area for this word is 3.1mm.3mm=9mm^2.
Note: Recall that the width of each character is 1mm.

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(;
        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 =;
        int lengh = word.length();
        int max = 0;

        for (int i = 0; i < lengh; i++)
            char tempChar = word.charAt(i);
            max = max < h[tempChar - 97] ? h[tempChar - 97] : max;
        System.out.println(max * lengh);


The problem requires basic knowledge of programming.
As stated, for the given word:
1 h: The height of a selected area is the maximum height of any character in the word being selected.
2 w: is specified as 1mm * number of character in the word.
We just have to calculate the area (A) of a rectangle as A=h*w using the respective h and w values for that rectangle.
Let’s see some implementations in few popular programming languages:


#function returns height of rectangle for a given word
def get_rect_height(word, height_arr):
    height = 0
    for c in word:
        height = max(height, height_arr[ ord(c) - ord("a") ])
        #ord(character) gives the ascii value
    return height

heights = [int(x) for x in raw_input().split()]	#scan heights
word = raw_input()

print len(w) * get_rect_height(w, heights)


import java.util.Scanner;
import java.lang.Math;

public class Solution {
    public static void main(String[] args) {
        Scanner scan = new Scanner(;
        int[] heights  = new int[26];

        for (int i = 0; i < 26; i++) {
            heights[i] = scan.nextInt();
        String word =;
        int rect_height = 0;

        for (int j = 0; j < word.length(); j++) {
            rect_height = Math.max(
                heights[(int)word.charAt(j) - (int)'a']

        int ans = 1 * word.length() * rect_height;


#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <cassert>
using namespace std;

int main(){
    int t = 26;
    vector<int> h(t);
    for (int i = 0; i < t; i++) {
        cin >> h[i];
	string s;
	cin >> s;

    int rect_height = 0;

    for (int j = 0; j < s.length(); j ++) {
        rect_height = max(rect_height, h[s[j] - 'a']);
    int ans = rect_height * 1 * s.length();
    cout << ans << endl;

    return 0;

A web developer(Front end and Back end), and DBA at Currently working as Salesforce Developer @ Tech Matrix IT Consulting Private Limited. Check me

Leave a reply:

Your email address will not be published.