1957. Delete Characters to Make Fancy String
leetcode
Problem
Given a string, we need to delete characters to create a "fancy" string. A "fancy" string is defined as a string that does not have three consecutive identical characters. The task is to return the fanciest string we can possibly create by deleting some characters from the original string.
Solution Description
To implement a function that transforms a string into a fancy string, we need to traverse the string while maintaining a count of consecutive identical characters. The general idea is to iterate through the string character by character, keeping a count of consecutive occurrences of each character. If the count reaches three, we skip adding that character to the result. This ensures no three consecutive characters are present in the result.
We can follow these steps:
- Initialize a result string and set the count of consecutive characters to zero.
- Iterate through the string.
- Compare the current character with the previous one.
- If it's the same, and the count is less than two, increment the count and add the character to the result.
- If it's different, reset the count and add the new character.
- Continue until the end of the string and return the result.
Time Complexity: O(n), where n is the number of characters in the string, as we iterate through the string once.
Space Complexity: O(n), mainly for storing the result.
Example
Consider the string "aaabaaaa":
- Start with the empty result and count set to zero.
- Add 'a' to the result, count = 1: "a"
- Add 'a' to the result, count = 2: "aa"
- Skip 'a' since count would become 3.
- Add 'b' to the result, reset count: "aab"
- Add 'a' to the result, count = 1: "aaba"
- Add 'a' to the result, count = 2: "aabaa"
- Skip 'a' since count would become 3.
- Return "aabaa"
References
For a deeper understanding of string manipulation techniques, consider reviewing String (Computer Science).
Solution
2024-11-01 Delete Characters to Make Fancy String - Submission Detail - LeetCode leetcode.com
Below is a JavaScript implementation of the solution complete with test cases:
// Function to make the string fancy by deleting characters
/**
* Returns a "fancy" string by ensuring no three consecutive identical characters.
* @param {string} s - The input string.
* @returns {string} - The fancy string with no three consecutive identical characters.
*/
function makeFancyString(s) {
const log = typeof NestedInteger !== 'undefined' ? () => {} : console.log;
const table = typeof NestedInteger !== 'undefined' ? () => {} : console.table;
let result = '';
let lastCharCount = 0;
let lastChar = '';
for (const ch of s) {
if (ch === lastChar) {
lastCharCount += 1;
} else {
lastChar = ch;
lastCharCount = 1;
}
if (lastCharCount < 3) {
result += ch;
}
}
return result;
}
// Test cases
const testCases = [
{ input: "aaabaaaa", expected: "aabaa" },
{ input: "aabbcc", expected: "aabbcc" },
{ input: "a", expected: "a" },
{ input: "aa", expected: "aa" },
{ input: "aaa", expected: "aa" },
{ input: "aabaaa", expected: "aabaa" }
];
testCases.forEach((test, index) => {
const result = makeFancyString(test.input);
console.log(`Test Case ${index + 1}: ${result === test.expected ? 'Passed' : 'Failed'} (Expected: ${test.expected}, Got: ${result})`);
});