This content originally appeared on DEV Community and was authored by MD ARIFUL HAQUE
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:
- What's the optimal way to delete characters if three or more consecutive characters are equal?
- 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"
?>
Explanation:
-
Initialize Variables:
-
$result
: This will store the final "fancy" string.
-
-
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
.
-
Return the Result:
- The
$result
string now contains no three consecutive identical characters.
- The
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:
This content originally appeared on DEV Community and was authored by MD ARIFUL HAQUE
MD ARIFUL HAQUE | Sciencx (2024-11-01T17:33:44+00:00) 1957. Delete Characters to Make Fancy String. Retrieved from https://www.scien.cx/2024/11/01/1957-delete-characters-to-make-fancy-string/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.