Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Removing letters to make adjacent pairs different using JavaScript
We are required to write a JavaScript function that takes in a string containing only 'A', 'B' and 'C'. Our function should find the minimum number of characters needed to be removed from the string so that no two adjacent characters are the same.
Problem Statement
Given a string with characters 'A', 'B', and 'C', we need to remove the minimum number of characters to ensure all adjacent pairs are different. For example, in "AAB", we need to remove one 'A' to get "AB".
Algorithm Approach
The strategy is to iterate through the string and whenever we find two consecutive identical characters, we remove one of them. This greedy approach ensures we get the minimum removals.
Example Implementation
const str = "ABBABCCABAA";
const removeLetters = (str = '') => {
const arr = str.split('');
let count = 0;
for (let i = 0; i < arr.length; i++) {
if (arr[i] === arr[i + 1]) {
count += 1;
arr.splice(i, 1);
i -= 1; // Adjust index after removal
}
}
return count;
};
console.log("Original string:", str);
console.log("Characters removed:", removeLetters(str));
Original string: ABBABCCABAA Characters removed: 3
How It Works
The function converts the string to an array and iterates through each character. When it finds adjacent identical characters, it removes one and decrements the index to recheck the current position with the next character.
Step-by-Step Example
const demonstrateSteps = (str) => {
const arr = str.split('');
let count = 0;
console.log("Initial:", arr.join(''));
for (let i = 0; i < arr.length; i++) {
if (arr[i] === arr[i + 1]) {
count += 1;
console.log(`Found duplicate at position ${i}: ${arr[i]}`);
arr.splice(i, 1);
console.log("After removal:", arr.join(''));
i -= 1;
}
}
console.log("Total removals:", count);
return count;
};
demonstrateSteps("AABBCC");
Initial: AABBCC Found duplicate at position 0: A After removal: ABBCC Found duplicate at position 1: B After removal: ABCC Found duplicate at position 2: C After removal: ABC Total removals: 3
Alternative Approach
Here's a simpler solution that counts removals without modifying the original array:
const countRemovals = (str) => {
let removals = 0;
for (let i = 0; i < str.length - 1; i++) {
if (str[i] === str[i + 1]) {
removals++;
// Skip the next character as we "remove" current one
while (i < str.length - 1 && str[i] === str[i + 1]) {
i++;
}
}
}
return removals;
};
console.log("ABBABCCABAA:", countRemovals("ABBABCCABAA"));
console.log("AAABBBCCC:", countRemovals("AAABBBCCC"));
console.log("ABCABC:", countRemovals("ABCABC"));
ABBABCCABAA: 3 AAABBBCCC: 3 ABCABC: 0
Conclusion
Both approaches solve the problem by identifying consecutive duplicate characters and counting the minimum removals needed. The greedy strategy ensures we get the optimal solution in O(n) time complexity.
