Lädt...


🔧 1405. Longest Happy String


Nachrichtenbereich: 🔧 Programmierung
🔗 Quelle: dev.to

1405. Longest Happy String

Difficulty: Medium

Topics: String, Greedy, Heap (Priority Queue)

A string s is called happy if it satisfies the following conditions:

  • s only contains the letters 'a', 'b', and 'c'.
  • s does not contain any of "aaa", "bbb", or "ccc" as a substring.
  • s contains at most a occurrences of the letter 'a'.
  • s contains at most b occurrences of the letter 'b'.
  • s contains at mostcoccurrences of the letter'c'`.

Given three integers a, b, and c, 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 "".

A substring is a contiguous sequence of characters within a string.

Example 1:

  • Input: a = 1, b = 1, c = 7
  • Output: "ccaccbcc"
  • Explanation: "ccbccacc" would also be a correct answer.

Example 2:

  • Input: a = 7, b = 1, c = 0
  • Output: "aabaa"
  • Explanation: It is the only correct answer in this case.

Constraints:

  • 0 <= a, b, c <= 100
  • a + b + c > 0

Hint:

  1. Use a greedy approach.
  2. Use the letter with the maximum current limit that can be added without breaking the condition.

Solution:

We can employ a greedy algorithm. The approach is based on the following steps:

  1. Use a Priority Queue (Max Heap):

    • We can use a max heap to keep track of the characters and their remaining counts. This allows us to always choose the character with the highest remaining count while ensuring that we don't exceed two consecutive occurrences of the same character.
  2. Construct the String:

    • Continuously extract the character with the highest count from the heap and append it to the result string.
    • If the last two characters in the result string are the same, we cannot append the same character again. Instead, we will append the next character with the highest count.
    • If we can append a character, we reduce its count and continue the process.
  3. Return the Result:

    • If at any point we can't append a character without breaking the happy string conditions, we stop the process.

Let's implement this solution in PHP: 1405. Longest Happy String

`php
<?php
/**

  • @param Integer $a
  • @param Integer $b
  • @param Integer $c
  • @return String / function longestHappyString($a, $b, $c) { ... ... ... /*
    • go to ./solution.php */ }

// Example usage
$a1 = 1; $b1 = 1; $c1 = 7;
echo "Input: a = $a1, b = $b1, c = $c1\n";
echo "Output: " . longestHappyString($a1, $b1, $c1) . "\n"; // Output: "ccaccbcc" or similar

$a2 = 7; $b2 = 1; $c2 = 0;
echo "Input: a = $a2, b = $b2, c = $c2\n";
echo "Output: " . longestHappyString($a2, $b2, $c2) . "\n"; // Output: "aabaa"
?>
`

Explanation:

  1. Max Heap Creation:

    • We initialize an array $maxHeap with the counts of characters 'a', 'b', and 'c'.
  2. Sorting the Heap:

    • We use usort to sort the heap based on the remaining counts in descending order.
  3. Building the Result String:

    • While there are characters in the heap:
      • Sort the heap to ensure we always access the character with the highest count.
      • If the last two characters in the result string are the same as the character with the highest count, we need to check if we can add a different character.
      • If we can add the character, we decrement its count and continue.
  4. Return:

    • Finally, we return the constructed happy string.

Complexity Analysis

  • Time Complexity: The solution runs in O(n log m) where n is the total number of characters we might output (i.e., a + b + c), and m is the number of different characters (which is at most 3).
  • Space Complexity: The space complexity is O(1) for the counts since we only keep a constant number of characters in the heap.

This solution efficiently constructs the longest happy string while adhering to the constraints specified in the problem.

Contact Links

If you found this series helpful, please consider giving the repository a star on GitHub or sharing the post on your favorite social networks 😍. Your support would mean a lot to me!

If you want more helpful content like this, feel free to follow me:

...

🔧 1405. Longest Happy String


📈 65.75 Punkte
🔧 Programmierung

🔧 Longest Happy String


📈 37.78 Punkte
🔧 Programmierung

🕵️ CVE-2022-1405


📈 27.96 Punkte
🕵️ Sicherheitslücken

🕵️ CVE-2021-1405


📈 27.96 Punkte
🕵️ Sicherheitslücken

🕵️ CVE-2020-1405


📈 27.96 Punkte
🕵️ Sicherheitslücken

🕵️ Vuln: Multiple Cisco Products CVE-2016-1405 Remote Denial of Service Vulnerability


📈 27.96 Punkte
🕵️ Sicherheitslücken

🕵️ Vuln: Multiple Cisco Products CVE-2016-1405 Remote Denial of Service Vulnerability


📈 27.96 Punkte
🕵️ Sicherheitslücken

🔧 Java String Management: String Pool vs String Heap Explained


📈 27.87 Punkte
🔧 Programmierung

🔧 Find the longest Substring of a given String S


📈 26.71 Punkte
🔧 Programmierung

🔧 Generate longest String with character sum at most K by deleting letters


📈 26.71 Punkte
🔧 Programmierung

🔧 Happy Birthday Me, Happy Birthday NeoHaskell


📈 22.14 Punkte
🔧 Programmierung

📰 OpenAI CEO: We're happy if Microsoft makes a sale, and they're happy if we make a sale


📈 22.14 Punkte
📰 IT Nachrichten

🔧 Happy Customers, Happy Holidays: der Fahrplan für erfolgreiche Kampagnen


📈 22.14 Punkte
🔧 Programmierung

🔧 What is a String in JS? The JavaScript String Variable Explained


📈 18.58 Punkte
🔧 Programmierung

🔧 Remove From String in Python – How to Remove Characters from a String


📈 18.58 Punkte
🔧 Programmierung

🔧 Check whether a given string is an anagram of another given string.


📈 18.58 Punkte
🔧 Programmierung

🔧 Int to String in Java – How to Convert an Integer into a String


📈 18.58 Punkte
🔧 Programmierung

🔧 Kotlin String Templates vs. Java String Concatenation: A Tale of Two Strings (Where Kotlin Sings!)


📈 18.58 Punkte
🔧 Programmierung

🔧 string vs String


📈 18.58 Punkte
🔧 Programmierung

🔧 Return a Palindromic String after removing minimum length Prefix from given String


📈 18.58 Punkte
🔧 Programmierung

🔧 'string' vs 'String': The Battle Between Primitive and Object Types in TypeScript


📈 18.58 Punkte
🔧 Programmierung

🔧 Number of pairs of String whose concatenation leads to a Sorted string


📈 18.58 Punkte
🔧 Programmierung

🔧 String Operations(Length, escape character, verbatim string)


📈 18.58 Punkte
🔧 Programmierung

📰 Bash Shell: Replace a String With Another String In All Files Using sed and Perl -pie Options


📈 18.58 Punkte
🐧 Unix Server

🔧 JavaScript: Strings, String Methods, and String Properties


📈 18.58 Punkte
🔧 Programmierung

🕵️ Netgear R6700 up to 1.0.4.84_10.0.58 String Table File Upload format string


📈 18.58 Punkte
🕵️ Sicherheitslücken

🔧 Implementing Excel Reading into List<Map<String, String>> in Java


📈 18.58 Punkte
🔧 Programmierung

🕵️ libxslt 1.1.33 numbers.c xsltNumberFormatInsertNumbers String Format String


📈 18.58 Punkte
🕵️ Sicherheitslücken

🔧 How to Check if a String Contains Another String in C# 🔍


📈 18.58 Punkte
🔧 Programmierung

🕵️ secure-compare up to 3.0.0 on Node.js String Comparison Argument Format String


📈 18.58 Punkte
🕵️ Sicherheitslücken

🔧 JS Remove Char from String – How to Trim a Character from a String in JavaScript


📈 18.58 Punkte
🔧 Programmierung

matomo