From 0bd1c82f6d0bee8cc121398ee9a7f87a4d025262 Mon Sep 17 00:00:00 2001 From: Lubos Kolouch Date: Wed, 5 Jul 2023 12:42:23 +0200 Subject: feat(challenge-224/lubos-kolouch/perl,python,java/): Challenge 224 LK Perl Python Java --- challenge-224/lubos-kolouch/java/ch-1.java | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 challenge-224/lubos-kolouch/java/ch-1.java (limited to 'challenge-224/lubos-kolouch/java/ch-1.java') diff --git a/challenge-224/lubos-kolouch/java/ch-1.java b/challenge-224/lubos-kolouch/java/ch-1.java new file mode 100644 index 0000000000..aeff01b10f --- /dev/null +++ b/challenge-224/lubos-kolouch/java/ch-1.java @@ -0,0 +1,26 @@ +import java.util.HashMap; +import java.util.Map; + +public class Main { + public static void main(String[] args) { + System.out.println(canFormTarget("abc", "xyz")); // Output: false + System.out.println( + canFormTarget("scriptinglanguage", "perl")); // Output: true + System.out.println(canFormTarget("aabbcc", "abc")); // Output: true + } + + public static boolean canFormTarget(String source, String target) { + Map sourceChars = new HashMap<>(); + for (char c : source.toCharArray()) { + sourceChars.put(c, sourceChars.getOrDefault(c, 0) + 1); + } + + for (char c : target.toCharArray()) { + if (!sourceChars.containsKey(c) || sourceChars.get(c) == 0) { + return false; + } + sourceChars.put(c, sourceChars.get(c) - 1); + } + return true; + } +} -- cgit