Java > AP-1 > wordsWithoutList (CodingBat Solution)

Problem:

Given an array of strings, return a new List (e.g. an ArrayList) where all the strings of the given length are omitted. See wordsWithout() below which is more difficult because it uses arrays.

wordsWithoutList({"a", "bb", "b", "ccc"}, 1) → {"bb", "ccc"}
wordsWithoutList({"a", "bb", "b", "ccc"}, 3) → {"a", "bb", "b"}
wordsWithoutList({"a", "bb", "b", "ccc"}, 4) → {"a", "bb", "b", "ccc"}

Solution:

public List wordsWithoutList(String[] words, int len) {
ArrayList al = new ArrayList();
for (int i = 0; i < words.length; i++) {
if (words[i].length() != len)
al.add(words[i]);
}
return al;
}

No comments:

Post a Comment