Designer PDF Viewer Algorithm

"The Highlighter Algorithm...?"

ยท

5 min read

Howdy fellow programmers! ๐Ÿ‘‹๐Ÿฝ

In this article, I'll be describing how I solved the Designer PDF Viewer Challenge on HackerRank using Python 3.

This challenge is part of the Implementation challenges in the Algorithm section on HackerRank.

If this is your first time reading my data structures and algorithms article, please consider reading more in my Algos in Plain English series.๐Ÿ˜ƒ

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:

image.png

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.

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 = 2, o = 1, r = 1 and n = 1. The tallest letter is 2 high and there are 4 letters. The hightlighted area will be 2 x 4 = 8mm^2 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
  • int player[m]: the player's scores

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:

image.png

Sample Output 0:

image.png

Explanation:

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 3mm x 1mm = 9mm^2.

Note: Recall that the width of each character is 1mm.

I'll get right into the algorithm. Please feel free to read the full question on HackerRank.

Solution ๐Ÿ”Ž

In plain English, given a word to be highlighted, we need to get the highlighted area. The letters in the text have varying heights, so we get the area by multiplying the maximum height by the number of words.

designerPdfViewer.drawio.png

To test our solution, let's consider a different sample input with an expected output of 28 because the tallest letter in zaba is at 7mm.

image.png

Let's write down the steps we'll use to solve this problem.

# Given the word, return the indexes for each letter based on the alphabets
# Get the height from 'h' at each of these indexes
# Return the area which is the maximum word height multiplied by the length of 'word'

Step 1๏ธโƒฃ: Getting the index of word letters in the alphabet.

  1. We need to first get the location of each word letter in the alphabet. This way we can get the height, h[index] of each of these letters in h.
  2. We'll create variables to store the index of word letters, their respective heights from h[index], and the alphabets.
def designerPdfViewer(h, word):
    # Write your code here
    word_indexes = []        # store the index of letters in 'word'
    word_heights = []        # store the heights of letters in 'h'
    alphabets = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']

    # Given the word, return the indexes for each letter based on the alphabets
    for letter in word:
        word_indexes.append(alphabets.index(letter))

Step 2๏ธโƒฃ: Get the height of each letter.

To get the height of each of the letters, we'll simply use h[index]. i.e. the height at each index in h and store it in word_heights

def designerPdfViewer(h, word):
    # Write your code here
    word_indexes = []        # store the index of letters in 'word'
    word_heights = []        # store the heights of letters in 'h'
    alphabets = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']

    # Given the 'word', return the indexes for each letter based on the alphabets
    for letter in word:
        word_indexes.append(alphabets.index(letter))

    # Get the height from 'h' at each of these indexes
    for index in word_indexes:
        word_heights.append(h[index])

Step 3๏ธโƒฃ: Return the area of the highlighted text

This area is simply the product of the maximum height in word_heights and the length of word (remember the image above).

def designerPdfViewer(h, word):
    # Write your code here
    word_indexes = []        # store the index of letters in 'word'
    word_heights = []        # store the heights of letters in 'h'
    alphabets = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']

    # Given the word, return the indexes for each letter based on the alphabets
    for letter in word:
        word_indexes.append(alphabets.index(letter))

    # Get the height from 'h' at each of these indexes
    for index in word_indexes:
        word_heights.append(h[index])

    # Return the area which is the maximum word height multiplied by the length of 'word'
    return max(word_heights) * len(word)

designerPdfViewer([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')
# 28

That's it! Thanks for reading! ๐Ÿ™‚

Hope this article helped with understanding this challenge. Happy Coding!๐Ÿ‘จ๐Ÿฝโ€๐Ÿ’ป

Buy me a coffee.png

ย