From 2390cb3375065c3ecf8ac1d3a8321337979144a1 Mon Sep 17 00:00:00 2001 From: Packy Anderson Date: Wed, 13 Sep 2023 00:11:32 -0400 Subject: Add challenge 234 solutions in Java --- challenge-234/packy-anderson/java/Ch1.java | 81 ++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 challenge-234/packy-anderson/java/Ch1.java (limited to 'challenge-234/packy-anderson/java/Ch1.java') diff --git a/challenge-234/packy-anderson/java/Ch1.java b/challenge-234/packy-anderson/java/Ch1.java new file mode 100644 index 0000000000..70bb98fa43 --- /dev/null +++ b/challenge-234/packy-anderson/java/Ch1.java @@ -0,0 +1,81 @@ +import java.util.ArrayList; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Set; + +// Press Shift twice to open the Search Everywhere dialog and type `show whitespaces`, +// then press Enter. You can now see whitespace characters in your code. +public class Ch1 { + public static HashMap charFrequency(String word) { + HashMap freq = new HashMap(); + for (int i=0; i < word.length(); i++) { + char c = word.charAt(i); + freq.put(c, freq.getOrDefault(c,0)+1); + } + return freq; + } + + public static ArrayList commonCharacters(String[] words) { + // get the character frequencies for each word in words + ArrayList> freq = + new ArrayList>(); + for (String word : words) { + freq.add(charFrequency(word)); + } + // grab the character frequency map for the first word + HashMap first = freq.remove(0); + // now check the characters in the first word against + // the characters in all the subsequent words + Set keys = new HashSet<>(first.keySet()); + for (Character c : keys) { + for (HashMap subsequent : freq) { + if (! subsequent.containsKey(c)) { + // this character isn't in subsequent words, + // so let's remove it from the frequency map + // of the first word + first.remove(c); + } + else { + // the character IS in subsequent words, + // so let's set the frequency count to be + // the minimum count found in those words + first.put(c, Math.min(first.get(c), subsequent.get(c))); + } + } + } + // now we generate a list of characters in the order they + // appear in the first word + ArrayList output = new ArrayList(); + // once again, loop over the characters in the first word + for (int i=0; i < words[0].length(); i++) { + Character c = words[0].charAt(i); + if (first.containsKey(c)) { + if (first.get(c) > 1) { + first.put(c, first.get(c) - 1); + } + else { + first.remove(c); + } + output.add("" + c); + } + } + return output; + } + + public static void solution(String[] words) { + System.out.println("Input: @words = (\"" + String.join("\", \"", words) + "\")"); + ArrayList output = commonCharacters(words); + System.out.println("Output: (\"" + String.join("\", \"", output) + "\")"); + } + + public static void main(String[] args) { + System.out.println("Example 1:"); + solution(new String[] {"java", "javascript", "julia"}); + + System.out.println("\nExample 2:"); + solution(new String[] {"bella", "label", "roller"}); + + System.out.println("\nExample 3:"); + solution(new String[] {"cool", "lock", "cook"}); + } +} \ No newline at end of file -- cgit