
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Finding minimum deletions in string in JavaScript
Suppose we have a binary string like this −
const str = '001001';
We are required to write a JavaScript function that takes in one such string as the first and the only argument.
The function should then compute and return the number of minimum deletions required in the input so that no two adjacent numbers are the same.
For example, for the above string, the output should be −
const output = 2;
because if we delete '0' at index 0 and 3, the new string will be '0101' which is the longest desired string.
Example
The code for this will be −
const str = '001001'; const minimumDeletions = (str = '') => { let count = 0; const { length } = str; for(let i = 0; i < length; i++){ if (str[i] === str[i + 1]){ count++; }; } return count; }; console.log(minimumDeletions(str));
Output
And the output in the console will be −
2
- Related Articles
- Finding minimum flips in a binary string using JavaScript
- Minimum number of deletions to make a string palindrome in C++.
- Program to find minimum deletions to make string balanced in Python
- Finding minimum time difference in an array in JavaScript
- Finding mistakes in a string - JavaScript
- Minimum number of deletions and insertions to transform one string into another using C++.
- Program to find minimum deletions to make strings strings in Python
- Finding duplicate "words" in a string - JavaScript
- Finding missing letter in a string - JavaScript
- Finding sort order of string in JavaScript
- Finding shortest word in a string in JavaScript
- Finding unique string in an array in JavaScript
- Finding hamming distance in a string in JavaScript
- Finding minimum steps to make array elements equal in JavaScript
- Finding second smallest word in a string - JavaScript

Advertisements