DEV Community

Cover image for 1957. Delete Characters to Make Fancy String
MD ARIFUL HAQUE
MD ARIFUL HAQUE

Posted on

1957. Delete Characters to Make Fancy String

1957. Delete Characters to Make Fancy String

Difficulty: Easy

Topics: String

A fancy string is a string where no three consecutive characters are equal.

Given a string s, delete the minimum possible number of characters from s to make it fancy.

Return the final string after the deletion. It can be shown that the answer will always be unique.

Example 1:

  • Input: s = "leeetcode"
  • Output: "leetcode"
  • Explanation: Remove an 'e' from the first group of 'e's to create "leetcode". No three consecutive characters are equal, so return "leetcode".

Example 2:

  • Input: s = "aaabaaaa"
  • Output: "aabaa"
  • Explanation: Remove an 'a' from the first group of 'a's to create "aabaaaa". Remove two 'a's from the second group of 'a's to create "aabaa". No three consecutive characters are equal, so return "aabaa".

Example 3:

  • Input: s = "aab"
  • Output: "aab"
  • Explanation: No three consecutive characters are equal, so return "aab".

Constraints:

  • 1 <= s.length <= 105
  • s consists only of lowercase English letters.

Hint:

  1. What's the optimal way to delete characters if three or more consecutive characters are equal?
  2. If three or more consecutive characters are equal, keep two of them and delete the rest.

Solution:

We need to ensure that no three consecutive characters are the same in the final string. We'll iterate through the input string and build a new "fancy" string by keeping track of the previous two characters. If a third consecutive character matches the last two, we skip it. Otherwise, we add it to the output.

Let's implement this solution in PHP: 1957. Delete Characters to Make Fancy String

<?php
/**
 * @param String $s
 * @return String
 */
function makeFancyString($s) {
    ...
    ...
    ...
    /**
     * go to ./solution.php
     */
}

// Example usage:
echo makeFancyString("leeetcode"); // Output: "leetcode"
echo "\n";
echo makeFancyString("aaabaaaa");  // Output: "aabaa"
echo "\n";
echo makeFancyString("aab");       // Output: "aab"
?>
Enter fullscreen mode Exit fullscreen mode

Explanation:

  1. Initialize Variables:

    • $result: This will store the final "fancy" string.
  2. Iterate through the String:

    • For each character, check if it forms a trio with the last two characters in the result.
    • If it does, skip adding it to $result.
    • If not, add it to $result.
  3. Return the Result:

    • The $result string now contains no three consecutive identical characters.

Complexity Analysis

  • Time Complexity: O(n), where n is the length of the input string, as we process each character once.
  • Space Complexity: O(n), for storing the output string.

This solution meets the constraints efficiently and ensures that the final string has no three consecutive identical characters.

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:

Playwright CLI Flags Tutorial

5 Playwright CLI Flags That Will Transform Your Testing Workflow

  • 0:56 --last-failed
  • 2:34 --only-changed
  • 4:27 --repeat-each
  • 5:15 --forbid-only
  • 5:51 --ui --headed --workers 1

Learn how these powerful command-line options can save you time, strengthen your test suite, and streamline your Playwright testing experience. Click on any timestamp above to jump directly to that section in the tutorial!

Watch Full Video 📹️

Top comments (0)

Playwright CLI Flags Tutorial

5 Playwright CLI Flags That Will Transform Your Testing Workflow

  • 0:56 --last-failed
  • 2:34 --only-changed
  • 4:27 --repeat-each
  • 5:15 --forbid-only
  • 5:51 --ui --headed --workers 1

Learn how these powerful command-line options can save you time, strengthen your test suite, and streamline your Playwright testing experience. Click on any timestamp above to jump directly to that section in the tutorial!

Watch Full Video 📹️

👋 Kindness is contagious

Engage with a wealth of insights in this thoughtful article, valued within the supportive DEV Community. Coders of every background are welcome to join in and add to our collective wisdom.

A sincere "thank you" often brightens someone’s day. Share your gratitude in the comments below!

On DEV, the act of sharing knowledge eases our journey and fortifies our community ties. Found value in this? A quick thank you to the author can make a significant impact.

Okay