-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathSolution4.java
45 lines (38 loc) · 1.32 KB
/
Solution4.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/**
* Problem statement:
* 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. 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 mm^2 assuming all letters are 1mm wide.
*
* Input:
* 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
*
* Output:
* 9
*
* Explanation:
* Letter heights are a = 1, b = 3 and c = 1. The tallest letter, b, is 3mm high. The selection area
* for this word is 3 * 1mm * 3mm = 9 mm^2.
**/
public class Solution4{
private static int designerPdfViewer(int[] h, String word) {
int num_chars = word.length();
int area;
int max_height = 0;
for(int i = 0 ; i < num_chars; i++){
//ASCII value of character 'a' is 97
int index = word.charAt(i) - 97;
if(h[index] > max_height ){
max_height = h[index];
}
}
area = num_chars * 1 * max_height;
return area;
}
public static void main(String[] args) {
int[] h = {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};
designerPdfViewer(h, "abc");
}
}