Skip to main content

Hacker Rank "Designer PDF Viewer" Solution

Problem : -----


When you select a contiguous block of text in a PDF viewer, the selection is highlighted with a blue rectangle. In this PDF viewer, each word is highlighted independently. For example:
PDF-highighting.png
In this challenge, you will be given a list of letter heights in the alphabet and a string. Using the letter heights given, determine the area of the rectangle highlight in  assuming all letters are  wide.
For example, the highlighted . Assume the heights of the letters are  and . The tallest letter is  high and there are  letters. The hightlighted area will be  so the answer is .
Function Description
Complete the designerPdfViewer function in the editor below. It should return an integer representing the size of the highlighted area.
designerPdfViewer has the following parameter(s):
  • h: an array of integers representing the heights of each letter
  • word: a string
Input Format
The first line contains  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
  • , where  is an English lowercase letter.
  •  contains no more than  letters.
Output Format
Print a single integer denoting the area in  of highlighted rectangle when the given word is selected. Do not print units of measure.
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  and . The tallest letter, b, is  high. The selection area for this word is .
Note: Recall that the width of each character is .
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  is  at .
Solution : --------
<?php
/* Write your PHP code here */
//Height of each english alphabet.
$h = array(1,3,1,2,4,6,4,3,5,3,7,8,6,5,3,0,0,8,7,5,4,3,7,4,3,7);
//String is given
$word = "abc";
$arr = array();
$a = 0;
$len = 0;
$value = 0;
$ans = 0;
for($i=0;$i<strlen($word);$i++){
$len = ord(strtolower($word[$i]))-97; // a= 1, z= 26, 
$value = $h[$len];
$arr[$i] = $value;
}
rsort($arr);
$a = $arr[0];
$ans = $a * strlen($word);
echo $ans; // Area of the word
?>
ajay yadav expo
Ajayyadavexpo

Thank you

Comments

Popular posts from this blog

PHP Hacker Rank Question "Birthday Cake Candles" solution !

You are in-charge of the cake for your niece's birthday and have decided the cake will have one candle for each year of her total age. When she blows out the candles, she’ll only be able to blow out the tallest ones. Your task is to find out how many candles she can successfully blow out. For example, if your niece is turning   years old, and the cake will have   candles of height  ,  ,  ,  , she will be able to blow out   candles successfully, since the tallest candle is of height   and there are   such candles. Function Description Complete the function  birthdayCakeCandles  in the editor below. It must return an integer representing the number of candles she can blow out. birthdayCakeCandles has the following parameter(s): k : the integer threshold of students on time for class to continue a : an array of integers representing student arrival times Input Format The first line...

Hacker Rank "Division Sum pairs" solution

Question :--- You are given an array of   integers,  , and a positive integer,  . Find and print the number of   pairs where   and   +   is divisible by  . For example,   and  . Our three pairs meeting the criteria are   and  . Function Description Complete the  divisibleSumPairs  function in the editor below. It should return the integer count of pairs meeting the criteria. divisibleSumPairs has the following parameter(s): n : the integer length of array  ar : an array of integers k : the integer to divide the pair sum by Input Format The first line contains   space-separated integers,   and  . The second line contains   space-separated integers describing the values of  . Constraints Output Format Print the number of   pairs where   and   +  ...

PHP Hacker rank Question solution Mini-Max sum in PHP

Given five positive integers, find the minimum and maximum values that can be calculated by summing exactly four of the five integers. Then print the respective minimum and maximum values as a single line of two space-separated long integers. For example,  . Our minimum sum is   and our maximum sum is  . We would print 26 24 Function Description Complete the  miniMaxSum  function in the editor below. It should print two space-separated integers on one line: the minimum sum and the maximum sum of   of   elements. miniMaxSum has the following parameter(s): arr : an array of   integers Input Format A single line of five space-separated integers. Constraints Output Format Print two space-separated long integers denoting the respective minimum and maximum values that can be calculated by summing exactly  four  of the five integers. (The output can be greater than a 32 bi...