diff options
Diffstat (limited to 'challenge-224/lubos-kolouch/java/ch-1.java')
| -rw-r--r-- | challenge-224/lubos-kolouch/java/ch-1.java | 26 |
1 files changed, 26 insertions, 0 deletions
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<Character, Integer> 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; + } +} |
