18
Jan
2020

Hackerrank Designer PDF Viewer (C++)

C++ :

int designerPdfViewer(vector<int> h, string word) {
    int area = 0, max = 0, stringLength = word.length(), tempSize = 0;
    string alphabetList = "abcdefghijklmnopqrstuvwxyz";

    //find tallest word
    for(int i = 0; i < stringLength; i++){
        for(int j = 0; j < h.size(); j++){
            if(word[i] == alphabetList[j]){
                tempSize = h[j];
            }
        }
        if(max < tempSize){
            max = tempSize;
        }
    }

    area = max * stringLength * 1;

    return area;
}

Explanation:

We need a string of all alphabets to determine the height of each alphabet in word for this one, and store it in variable alphabetList.

In the for loop, for each alphabet in word, compare it to each alphabet in alphabetList. Since vector h is the same size as alphabetList.length, we can actually use either one as the limit for the inner for loop. Once we found the same alphabet of word at index i as alphabet of index j, we find the word’s length in vector h. We then compare every alphabet with max (initially set to 0) after we found the height, to determine which alphabet is the tallest.

Finally, get the area of the word by using max (height of word) * stringLength (length of word) * 1 (width of word).

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *