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
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 1mm wide.
Example:
Input: 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}, word="abc"
Output: 9
Approach
Java
public class DesignerPDFViewer {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 };String word = "abc";System.out.println(designerPdfViewer(h, word));}private static int designerPdfViewer(int[] h, String word) {int m = word.length();int c[] = new int[m];int k = 0;for (int j = 0; j < m; j++) {int x = 97;while (word.charAt(j) != x && x <= 122) {x++;}c[k] = h[x - 97];k++;}int large = c[0];for (int i = 1; i < m; i++) {if (c[i] > large)large = c[i];}return large * m;}}
C++
#include <bits/stdc++.h>using namespace std;int designerPdfViewer(vector<int> h, string word){int m = word.size();int c[m];int k = 0;for (int j = 0; j < m; j++){int x = 97;while (word[j] != x && x <= 122){x++;}c[k] = h[x - 97];k++;}int large = c[0];for (int i = 1; i < m; i++){if (c[i] > large)large = c[i];}return large * m;}int main(){vector<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};string word = "abc";cout << designerPdfViewer(h, word);return 0;}
No comments:
Post a Comment