-1

So I am trying to solve the problem in Java below. Could someone give me an idea of how to approach this? I can only think of using a bunch of confusing for-loops to split up the arr, go through the alphabet, and go through each string, and even then I am confused about strings versus chars. Any advice would be great.

--

Suppose the letter 'A' is worth 1, 'B' is worth 2, and so forth, with 'Z' worth 26. The value of a word is the sum of all the letter values in it. Given an array arr of words composed of capital letters, return the value of the watch with the largest value. You may assume that arr has length at least 1.

{"AAA","BBB","CCC"} => 9

{"AAAA","B","C"} => 4

{"Z"} => 26

{"",""} => 0

--

Here is what I have tried so far but I'm lost:

public static int largestValue(String[] arr){
    String alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    int largest = 0;
    int wordTotal=0;

    for (int i = 0; i < arr.length; i++){
        String[] parts = arr[i].split("");

        if (wordTotal < largest){ //I don't think this is in the right place
            largest = 0;    }

        for (int j = 0; j < alphabet.length(); j++){

            for(int k = 0; k <parts.length; k++){
                if ( alphabet.charAt(j) == parts[k].charAt(0) ){
                    wordTotal = 0;
                    wordTotal += alphabet.indexOf(alphabet.charAt(j))+1;

                }
            }
        }
    }
    return largest;
}
jro
  • 33
  • 1
  • 3
  • 3
    What code have you tried so far? – Michael Markidis Jun 02 '16 at 02:52
  • When analyzing requirements, you have to discern how reliable they are. The "promise" of the mapping being from the capital [basic Latin](https://en.wikipedia.org/wiki/Basic_Latin_(Unicode_block)) letters in the order of the English alphabet to the integers 1 to 26 seems rather weak. So, I think it is a good thing that you're creating your own "alphabet" rather than relying on their sequence in the underlying character set encoding. (A string is a counted sequence of Unicode UTF-16 code units). – Tom Blodget Jun 02 '16 at 17:26

2 Answers2

2

I would start by breaking the problem into parts, the first step is summing one String. To calculate the sum you can iterate the characters, test if the character is between 'A' and 'Z' (although your requirements say your input is guaranteed to be valid), subtract 'A' (a char literal) from the character and add it to your sum. Something like,

static int sumString(final String str) {
    int sum = 0;
    for (char ch : str.toCharArray()) {
        if (ch >= 'A' && ch <= 'Z') { // <-- validate input
            sum += 1 + ch - 'A';      // <-- 'A' - 'A' == 0, 'B' - 'A' == 1, etc.
        }
    }
    return sum;
}

Then you can iterate an array of String(s) to get the maximum sum; something like

static int maxString(String[] arr) {
    int max = sumString(arr[0]);
    for (int i = 1; i < arr.length; i++) {
        max = Math.max(max, sumString(arr[i]));
    }
    return max;
}

or with Java 8+

static int maxString(String[] arr) {
    return Stream.of(arr).mapToInt(x -> sumString(x)).max().getAsInt();
}

And, finally, validate the entire operation like

public static void main(String[] args) {
    String[][] strings = { { "AAA", "BBB", "CCC" }, { "AAAA", "B", "C" },
            { "Z" }, { "", "" } };
    for (String[] arr : strings) {
        System.out.printf("%s => %d%n", Arrays.toString(arr), maxString(arr));
    }
}

And I get

[AAA, BBB, CCC] => 9
[AAAA, B, C] => 4
[Z] => 26
[, ] => 0
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
1

I think it helps to take note of the two key parts here:

1: You need to be able to find the value of a single word, which is the sum of each letter

2: You need to find the value of all words, and find the largest

Since you need to go through each element (letter/character) in a string, and also each element (word) in the array, the problem really is set up for using 2 loops. I think part of the whole problem is making the for loops clear and concise, which is definitely doable. I don't want to give it away, but having a function that, given a word, returns the value of the word, will help. You could find the value of a word, see if its the largest so far, and repeat. Also, to find the value of a word, please do not use 26 if's (look up ASCII table instead!). Hope this gives you a better understanding without giving it away!

Liam Tyler
  • 33
  • 7