From 2481d94c479cef1e8dc4c8819343aaf38d50e0e6 Mon Sep 17 00:00:00 2001 From: chayan das <110921638+Chayandas07@users.noreply.github.com> Date: Wed, 19 Feb 2025 23:40:32 +0530 Subject: [PATCH] Create 1415. The k-th Lexicographical String of All Happy Strings of Length n --- ...al String of All Happy Strings of Length n | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 1415. The k-th Lexicographical String of All Happy Strings of Length n diff --git a/1415. The k-th Lexicographical String of All Happy Strings of Length n b/1415. The k-th Lexicographical String of All Happy Strings of Length n new file mode 100644 index 0000000..291ca4c --- /dev/null +++ b/1415. The k-th Lexicographical String of All Happy Strings of Length n @@ -0,0 +1,24 @@ +class Solution { +public: + string s = "abc", ans; + void backtrack(string cur, int &n, int &k, int &count) + { + if(count == k) return; + if(cur.size() == n) + { + ans = cur, count += 1; + return; + } + for(auto ch : s) + { + if(cur.size() and ch == cur.back()) continue; + backtrack(cur + ch, n, k, count); + } + } + string getHappyString(int n, int k) + { + int count = 0; + backtrack("", n, k, count); + return count == k ? ans : ""; + } +};