Designer PDF Viewer | HackerRank Solution

Hello coders, today we are going to solve Designer PDF Viewer HackerRank Solution which is a Part of HackerRank Algorithm Series.

Designer PDF Viewer

Contents

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 = 2o = 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

We are highlighting the word abc:

Letter heights are a = 1b = 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.

Solution – Designer PDF Viewer

C++

#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;
}

Python

import sys

h = [int(h_temp) for h_temp in input().strip().split(' ')]
word = input().strip()
max_height = 0
for i in range (len(word)):
    num = ord(word[i])-97
    if (h[num] >= max_height):
        max_height = h[num]

area = len(word)*max_height
print(area)

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));
    }
}

Disclaimer: The above Problem (Designer PDF Viewer) is generated by Hacker Rank but the Solution is Provided by CodingBroz. This tutorial is only for Educational and Learning Purpose.

Leave a Comment

Your email address will not be published.