aboutsummaryrefslogtreecommitdiff
path: root/other/longest_happy_string.cpp
diff options
context:
space:
mode:
authorJack Sangdahl <[email protected]>2025-03-10 00:36:13 -0600
committerJack Sangdahl <[email protected]>2025-03-10 00:36:13 -0600
commit7c60dee83b436e8e8a667bf9f191beced8de4fc3 (patch)
treeeb7a24ee2067f92245c57bb118e85a1baf15df50 /other/longest_happy_string.cpp
parent5d7cdc215e4ba1ec4cbb24c084cccb5e5e641468 (diff)
downloadleetcode-7c60dee83b436e8e8a667bf9f191beced8de4fc3.tar.gz
leetcode-7c60dee83b436e8e8a667bf9f191beced8de4fc3.zip
chore: move cpp and rust code to directory
Diffstat (limited to 'other/longest_happy_string.cpp')
-rw-r--r--other/longest_happy_string.cpp33
1 files changed, 33 insertions, 0 deletions
diff --git a/other/longest_happy_string.cpp b/other/longest_happy_string.cpp
new file mode 100644
index 0000000..ea3ad78
--- /dev/null
+++ b/other/longest_happy_string.cpp
@@ -0,0 +1,33 @@
+// 1405. Longest Happy String
+#include "leetcode.h"
+
+/*
+ * a string 's' is called happy if it satisfies the following conditions: 's'
+ * only contains the letters 'a', 'b', 'c', does not contain any of 'aaa',
+ * 'bbb', or 'ccc' as a substring, contains at most 'n' occurences of the
+ * letters 'a', 'b', 'c'. given three integers, return the longest possible
+ * happy string. if there are multiple longest happy strings, return any of
+ * them. if there is no such string, return the empty string
+ */
+
+class Solution {
+public:
+ string longestDiverseString(int a, int b, int c, char aa = 'a', char bb = 'b',
+ char cc = 'c') {
+ if (a < b)
+ return longestDiverseString(b, a, c, bb, aa, cc);
+ if (b < c)
+ return longestDiverseString(a, c, b, aa, cc, bb);
+ if (!b)
+ return string(min(2, a), aa);
+ auto use_a = min(2, a), use_b = a - use_a >= b ? 1 : 0;
+ return string(use_a, aa) + string(use_b, bb) +
+ longestDiverseString(a - use_a, b - use_b, c, aa, bb, cc);
+ }
+};
+
+int main() {
+ Solution obj;
+ cout << obj.longestDiverseString(1, 1, 7) << endl; // expect: ccaccbcc
+ cout << obj.longestDiverseString(7, 1, 0) << endl; // expect: aabaa
+}