

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Removing letters to make adjacent pairs different using JavaScript
Problem
We are required to write a JavaScript function that takes in a string that contains only ‘A’, ‘B’ and ‘C’. Our function should find the minimum number of characters needed to be removed from the string so that the characters in each pair of adjacent characters are different.
Example
Following is the code −
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 } } return count } console.log(removeLetters(str));
Output
3
- Related Questions & Answers
- Counting adjacent pairs of words in JavaScript
- Program to find shortest string after removing different adjacent bits in Python
- Removing adjacent duplicates from a string in JavaScript
- Removing smallest subarray to make array sum divisible in JavaScript
- Counting rings in letters using JavaScript
- Program to restore the array from adjacent pairs in Python
- Program to make pairwise adjacent sums small in Python
- Removing punctuations from a string using JavaScript
- Removing all spaces from a string using JavaScript
- JavaScript: Adjacent Elements Product Algorithm
- Removing last vowel - JavaScript
- Removing Elements from a Double Linked List using Javascript
- Converting alphabets to Greek letters in JavaScript
- Generating desired pairs within a range using JavaScript
- How to make a text italic using JavaScript?
Advertisements