edward.ribeiro
BAN USERimport java.util.List;
import java.util.Arrays;
import java.util.ArrayList;
public class LargestWord {
public static void main(String[] args) {
String maxStr = "";
int maxLength = Integer.MIN_VALUE;
List<String> words = Arrays.asList("abacus", "deltoid", "gaff", "giraffe", "microphone", "reef", "qar");
for (String w : words) {
List<Character> check = new ArrayList<Character>(Arrays.asList('a', 'e', 'f', 'f', 'g', 'i', 'r', 'q'));
boolean flag = true;
for (char c : w.toCharArray()) {
if (check.indexOf(c) != -1) {
check.remove(Character.valueOf(c));
}
else {
flag = false;
break;
}
}
if (flag && w.length() > maxLength) {
maxStr = w;
maxLength = w.length();
}
}
System.out.println(maxStr);
}
}
Yeah, guys you are right. Thanks for the tips on how to improve the code. :) I wrote it some nights ago, but didn't register on the site back then. Below are some of my points:
- edward.ribeiro March 23, 2013struct node{
char a[100];
struct node* next;
};
The code above doesn't really solve the malloc() overhead, but I can use this idea and create an static array and use it to replace the whole linked list I have created.
I didn't benchmark malloc()'s performance so I don't know for sure if malloc() is *wildly* inefficient. Of course, it's slower than the contiguous space allocation of an array, but even so it's better than a solution in Java/C# and more robust than an array of static size. ;-)