Problem Statement- Write a function that takes in an array of words and returns the smallest array of characters needed to form all of the words. The characters don’t need to be in particular order.
Sample Input words = ["this", "that", "did", "deed", "them!", "a"]
Sample Output ["t", "t", "h", "i", "s", "a", "d", "d", "e", "e", "m", "!"]
● Please don’t write any main function, focus on writing the given function. A sample function signature is given below (for Java)
class Program { public char[] minimumCharactersForWords(String[] words)
{
// Write your code here.
return new char[] {};
}
}
class Program {
public char[] minimumCharactersForWords(String[] words) {
Set<char> chars = new HashSet<char>();
for(String str : words) {
for (int i = 0; i < str.size(); i++)
chars.insert(str.charAt(i));
}
return chars.toArray();
}
}
Comments
Leave a comment