Answer to Question #268511 in Java | JSP | JSF for akki

Question #268511

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[] {};



}



}

1
Expert's answer
2021-11-19T10:02:04-0500
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();
  }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS