Highest Scoring Word algorithm throwing quirky error

I’m doing a challenge on CodeWars and I’m getting a quirky error. This is the problem statement:

Given a string of words, you need to find the highest scoring word. Each letter of a word scores points according to its position in the alphabet: a = 1, b = 2, c = 3 etc. You need to return the highest scoring word as a string. If two words score the same, return the word that appears earliest in the original string. All letters will be lowercase and all inputs will be valid.

This is the algorithm I wrote which does not work in some cases:

public static String high(String s) {
    String[] words = s.split(" ");
    int[] scores = new int[words.length];
    
    for (int j = 0; j < words.length; j++) {
        for (int i = 0; i < words[j].length(); i++) {
            scores[j] += (int) words[j].charAt(i);
        }
    }
    
    int highestWordIndex = 0;
    for (int i = 1; i < words.length; i++) {
        if (scores[i] > scores[highestWordIndex]) highestWordIndex = i;
    }
    
    return words[highestWordIndex];
}

However, when I add a “- 96” inside the nested for-loop it works. Here’s that code:

public static String high(String s) {
    String[] words = s.split(" ");
    int[] scores = new int[words.length];
    
    for (int j = 0; j < words.length; j++) {
        for (int i = 0; i < words[j].length(); i++) {
            scores[j] += (int) words[j].charAt(i) - 96;
        }
    }
    
    int highestWordIndex = 0;
    for (int i = 1; i < words.length; i++) {
        if (scores[i] > scores[highestWordIndex]) highestWordIndex = i;
    }
    
    return words[highestWordIndex];
}

Any idea why that’s making a difference?

Answer

The (int) char version of ‘a’ is 97, ‘b’ is 98, ‘c’ is 99 and so on. The question asks you to cast these to 1,2,3,… for good reason. consider the word cab vs the word ox.

“cab” should be 3 + 1 + 2 = 6. However in your implementation it’s 99+97+98=294

“ox” should be 15 + 24 = 39. However in your implementation it’s 111 + 120 = 231

“ox” SHOULD score higher than “cab”, but it doesn’t because you cast your chars to their ascii representative integers, rather than 1-26 like the question asks. As a result of this, your algorithm gives many more points to longer words, as each extra character gets an extra 96 points above what it should be getting according to the question. Subtracting 96 casts your score domain down from 97-122 to 1-26, which solves this problem for you.

Hope this helped 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *