I solved a HackerRank challenge called “Designer PDF Viewer”.
Here is the logic:
- Get input, and create an array of Int type for height value for each alphabet character. The array is named heights.
- Get input, and put it in a variable of type String. It’s for the word to be checked.
- Map the word string to an array of scalar values for its characters.
- We want to get a height value for each character, so we want to use characters’ unicode scalar values as index with the heights array.
- The problem is the heights array’s indices starts at 0. Index 0 is for “a”. But “a” character’s unicode scalar value is 97. They don’t match.
- The solution for this problem is adjust unicode scalar values by subtracting 97. Map it again!
["a", "b", "c"]->
[97, 98, 99]->
[0, 1, 2]
- Now we can use the elements value as index for heights. Map it again!
[0, 1, 2]->
[1, 3, 1]
- Now we have an array of heights for the word.
- Get the largest value from the array using
max()method. Multiply it by the word’s characters count. That’s the answer of the challenge.