|
| 1 | +package com.fishercoder.solutions; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.List; |
| 5 | + |
| 6 | +/** |
| 7 | + * 1415. The k-th Lexicographical String of All Happy Strings of Length n |
| 8 | + * |
| 9 | + * A happy string is a string that: |
| 10 | + * consists only of letters of the set ['a', 'b', 'c']. |
| 11 | + * s[i] != s[i + 1] for all values of i from 1 to s.length - 1 (string is 1-indexed). |
| 12 | + * |
| 13 | + * For example, strings "abc", "ac", "b" and "abcbabcbcb" are all happy strings and strings "aa", "baa" and "ababbc" are not happy strings. |
| 14 | + * Given two integers n and k, consider a list of all happy strings of length n sorted in lexicographical order. |
| 15 | + * Return the kth string of this list or return an empty string if there are less than k happy strings of length n. |
| 16 | + * |
| 17 | + * Example 1: |
| 18 | + * Input: n = 1, k = 3 |
| 19 | + * Output: "c" |
| 20 | + * Explanation: The list ["a", "b", "c"] contains all happy strings of length 1. The third string is "c". |
| 21 | + * |
| 22 | + * Example 2: |
| 23 | + * Input: n = 1, k = 4 |
| 24 | + * Output: "" |
| 25 | + * Explanation: There are only 3 happy strings of length 1. |
| 26 | + * |
| 27 | + * Example 3: |
| 28 | + * Input: n = 3, k = 9 |
| 29 | + * Output: "cab" |
| 30 | + * Explanation: There are 12 different happy string of length 3 ["aba", "abc", "aca", "acb", "bab", "bac", "bca", "bcb", "cab", "cac", "cba", "cbc"]. You will find the 9th string = "cab" |
| 31 | + * |
| 32 | + * Example 4: |
| 33 | + * Input: n = 2, k = 7 |
| 34 | + * Output: "" |
| 35 | + * |
| 36 | + * Example 5: |
| 37 | + * Input: n = 10, k = 100 |
| 38 | + * Output: "abacbabacb" |
| 39 | + * |
| 40 | + * Constraints: |
| 41 | + * 1 <= n <= 10 |
| 42 | + * 1 <= k <= 100 |
| 43 | + * */ |
| 44 | +public class _1415 { |
| 45 | + public static class Solution1 { |
| 46 | + public String getHappyString(int n, int k) { |
| 47 | + char[] chars = new char[]{'a', 'b', 'c'}; |
| 48 | + List<String> happyStrings = new ArrayList<>(); |
| 49 | + happyStrings.add(""); |
| 50 | + happyStrings = findAllHappyStrings(chars, n, happyStrings); |
| 51 | + return happyStrings.size() < k ? "" : happyStrings.get(k - 1); |
| 52 | + } |
| 53 | + |
| 54 | + private List<String> findAllHappyStrings(char[] chars, int n, List<String> happyStrings) { |
| 55 | + if (happyStrings.get(0).length() == n) { |
| 56 | + return happyStrings; |
| 57 | + } |
| 58 | + List<String> newHappyStrings = new ArrayList<>(); |
| 59 | + for (String str : happyStrings) { |
| 60 | + for (char c : chars) { |
| 61 | + if (str.equals("") || str.charAt(str.length() - 1) != c) { |
| 62 | + StringBuilder newSb = new StringBuilder(str); |
| 63 | + newSb.append(c); |
| 64 | + newHappyStrings.add(newSb.toString()); |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | + happyStrings = newHappyStrings; |
| 69 | + return findAllHappyStrings(chars, n, happyStrings); |
| 70 | + } |
| 71 | + } |
| 72 | +} |
0 commit comments