From 762dfbd944e52a623d500130e13271bcdc259097 Mon Sep 17 00:00:00 2001 From: Prateek Verma Date: Sat, 19 Oct 2024 13:13:03 +0530 Subject: [PATCH] day 16 solution in java --- solutions/day16/solution_java.java | 39 ++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 solutions/day16/solution_java.java diff --git a/solutions/day16/solution_java.java b/solutions/day16/solution_java.java new file mode 100644 index 0000000..fa3ff34 --- /dev/null +++ b/solutions/day16/solution_java.java @@ -0,0 +1,39 @@ +import java.util.PriorityQueue; + +class Solution { + public String longestDiverseString(int a, int b, int c) { + // Priority queue to store the characters and their counts. + PriorityQueue pq = new PriorityQueue<>((x, y) -> y[0] - x[0]); + if (a > 0) pq.offer(new int[]{a, 'a'}); + if (b > 0) pq.offer(new int[]{b, 'b'}); + if (c > 0) pq.offer(new int[]{c, 'c'}); + + StringBuilder result = new StringBuilder(); + + while (!pq.isEmpty()) { + int[] first = pq.poll(); + + // Check if last two characters are the same. + if (result.length() >= 2 && result.charAt(result.length() - 1) == first[1] && + result.charAt(result.length() - 2) == first[1]) { + + if (pq.isEmpty()) break; // No more valid characters. + + // Pick the second character. + int[] second = pq.poll(); + result.append((char) second[1]); + second[0]--; + + if (second[0] > 0) pq.offer(second); + pq.offer(first); + } else { + result.append((char) first[1]); + first[0]--; + + if (first[0] > 0) pq.offer(first); + } + } + + return result.toString(); + } +} \ No newline at end of file