Designer PDF Viewer HackerRank Solution in C, C++, Java, Python

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. 

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 (mm)^2 assuming all letters are  wide.

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

 

Designer PDF Viewer HackerRank 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 HackerRank Solution in 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;
}

 

Designer PDF Viewer HackerRank 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 HackerRank Solution in Python

#!/bin/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 HackerRank Solution in C#

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();
        
        Console.Out.WriteLine(word.ToCharArray().Max(ch => h[ch - 'a']) * word.Length);
    }
}

 

Attempt –  HackerRank Challenge

Link – https://www.hackerrank.com/challenges/designer-pdf-viewer/

Next HackerRank Challenge Solution 

Link – https://exploringbits.com/utopian-tree-hackerrank-solution/

Leave a Comment