From b1688b3b3ad5a3fc569badebcf6e803d1596ff96 Mon Sep 17 00:00:00 2001 From: Piyushh10 Date: Sat, 22 Feb 2025 11:20:37 +0530 Subject: [PATCH] Fixed file naming conflict and added solution for 1415 --- ...ring-of-All-Happy-Strings-of-Length-n.java | 41 +++++++++++++++++++ Smallest Subset With Greater Sum.java | 21 ---------- 2 files changed, 41 insertions(+), 21 deletions(-) create mode 100644 1415-The-k-th-Lexicographical-String-of-All-Happy-Strings-of-Length-n/1415-The-k-th-Lexicographical-String-of-All-Happy-Strings-of-Length-n.java delete mode 100644 Smallest Subset With Greater Sum.java diff --git a/1415-The-k-th-Lexicographical-String-of-All-Happy-Strings-of-Length-n/1415-The-k-th-Lexicographical-String-of-All-Happy-Strings-of-Length-n.java b/1415-The-k-th-Lexicographical-String-of-All-Happy-Strings-of-Length-n/1415-The-k-th-Lexicographical-String-of-All-Happy-Strings-of-Length-n.java new file mode 100644 index 00000000..8f6a716c --- /dev/null +++ b/1415-The-k-th-Lexicographical-String-of-All-Happy-Strings-of-Length-n/1415-The-k-th-Lexicographical-String-of-All-Happy-Strings-of-Length-n.java @@ -0,0 +1,41 @@ +class Solution { + // List to store all happy strings + List list = new ArrayList<>(); + + // Method to get the k-th happy string of length n + public String getHappyString(int n, int k) { + char[] chr = {'a', 'b', 'c'}; + + // Generate all happy strings of length n + solve(chr, n, new StringBuilder()); + + // Sort the list of happy strings lexicographically + Collections.sort(list); + + // Return the k-th happy string if it exists, otherwise return an empty string + return list.size() < k ? "" : list.get(k - 1); + } + + // Recursive method to generate happy strings + void solve(char[] chr, int n, StringBuilder sb) { + // If the current string length equals n, add it to the list + if (n == sb.length()) { + list.add(sb.toString()); + return; + } + + // Iterate through each character in chr array + for (int i = 0; i < chr.length; i++) { + // Skip if the current character is the same as the last character in the string + if (sb.length() > 0 && chr[i] == sb.charAt(sb.length() - 1)) continue; + + // Append the current character to the string + sb.append(chr[i]); + // Recursively generate the next character + solve(chr, n, sb); + // Remove the last character to backtrack + sb.setLength(sb.length() - 1); + } + } +} + diff --git a/Smallest Subset With Greater Sum.java b/Smallest Subset With Greater Sum.java deleted file mode 100644 index 72e688b0..00000000 --- a/Smallest Subset With Greater Sum.java +++ /dev/null @@ -1,21 +0,0 @@ -class Solution { - int minSubset(int[] arr,int n) { - Arrays.sort(arr); - long sum=0; - for(int i=0;i