
- 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
Rearrange string so that same character become n distance apart JavaScript
We are required to write a JavaScript function that takes in a string with repetitive characters and returns a new string in which all the same characters are exactly n characters away from each other. And the number should be smaller than the length of the array.
For example −
If the input string is: "accessories" And the number n is 3 Then, The return value should be: "secrsecisao"
Note − There may be some other permutation to achieve the required output, the order is not important, we should stick to the logic and as long as we fulfil it our output is correct.
Let's write the code for this function −
Example
const str = 'accessories'; const equalDistance = (str, num) => { const map = str.split("").reduce((acc, val) => { const count = acc.get(val); if(typeof count === 'number'){ acc.set(val, count+1); }else{ acc.set(val, 1); }; return acc; }, new Map()); const arr = Array.from(map).sort((a, b) => b[1] - a[1]); let newString = ''; for(let i = 0, count = 0; i < str.length;){ if(!arr[count][1]){ arr.splice(count, 1); continue; }; newString += arr[count][0]; arr[count][1]--; i++; count = i % num; }; return newString; }; console.log(equalDistance(str, 4)); console.log(equalDistance('abb', 2)); console.log(equalDistance('aacbbc', 3));
Output
The output in the console will be −
sceasceosri bab acbacb
- Related Articles
- Python program to Rearrange a string so that all same characters become d distance away
- Rearrange String k Distance Apart in C++
- Rearrange the given string such that all Prime Multiple indexes have Same Character
- Placing same string characters apart in JavaScript
- Rearrange characters in a string such that no two adjacent are same in C++
- Rearrange first N numbers to make them at K distance in C++
- Function that only replaces character from string after specified appearances in JavaScript
- Count of character pairs at same distance as in English alphabets in C++
- Return the index of first character that appears twice in a string in JavaScript
- Rearrange an array so that arr[i] becomes arr[arr[i]] with O(1) extra space using C++
- Why does sound become faint with distance?
- Corresponding shortest distance in string in JavaScript
- How to build a string with no repeating character n separate list of characters? in JavaScript
- With JavaScript Regular Expression find a non-whitespace character.\n\n
- Second most frequent character in a string - JavaScript

Advertisements