Hey people...

My problem is:

- -
Heaviest Word

Find the the heaviest word in an array.
One word is heavier than the other if it has more vocals than the other.
Vocal characters are ('a', 'e', 'i', 'o', 'u', 'y')

If two words have the same number of vocals, then heavier is the one which comes first alphabeticaly.

Input parameters:
words - array of words

words will have 1 to 10 elements inclusive

Return value:
The heaviest word.

Class Name:

Method :
public int heaviest(String[] words)

Test case 1:
heaviest({"a", "b", "c", "e"}) = "a"

Test case 2:
heaviest({"a", "b", "c", "ea"}) = "ea"

Test case 3:
heaviest({"zaaa", "yaab", "c", "e"}) = "yaab"

Test case 4:
heaviest({"akjqwhe", "asdasd", "qwe", "asde", "asdasd", "qweqwe", "qqqqqqq", "alksjd", "aeoi"}) = "aeoi"

Test case 5:
heaviest({"dddd", "cccc", "xxxx", "bbbb"}) = "bbbb"

Test case 6:
heaviest({"bc", "b"}) = "b"
- -

Here is my code:

public class HeaviestWord {
public String heaviest(String[] words) {

int br=0;
int no=0;
String hv = "";

for(String zbor : words){
for(int i=0; i<zbor.length(); i++){

if(zbor.charAt(i)== 'a'){ br++; }
if(zbor.charAt(i)== 'e'){ br++; }
if(zbor.charAt(i)== 'i'){ br++; }
if(zbor.charAt(i)== 'o'){ br++; }
if(zbor.charAt(i)== 'u'){ br++; }
if(zbor.charAt(i)== 'y'){ br++; }

hv = zbor;

return hv;

Now, I don't know how to restore the assembly word.

returned word should be just like this image:


What's the "assembly" word over here ?

Also the spec says the method should return an int, so this would most probably be the index of that element in the array. You have instead declared the return type as "String".

The logic to solve this should be implemented in two steps:
1. Where you count the vowels (I guess thats what you mean) in each word and assign a weight to each of these elements based on it. These weights could be assigned in a corresponding integer array, where an integer at index i is the weight for the word in the index i of the word array.

2. Next you move through the weights array finding the maximum weight, if there are two (or more) elements having the same weight, you compare the words in the words array at the corresponding locations and then figure out the max weight.

In the end you just return the index of the max weight in the weight array, due to our earlier arrangements of the weights this index will match the index of the "heaviest word".

This article has been dead for over six months. Start a new discussion instead.