Search code examples
javaarraysstringalphabet

Java: Assign values to alphabet and determine value of a string


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;
}

Solution

  • 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