
- 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
Formatting a string to separate identical characters in JavaScript
We are required to write a JavaScript function that takes in a character string as the first and the only argument.
The function should try and re-organize the characters present in the string such that no two identical characters are placed adjacent to each other.
If there exists at least one such combination then our function should return that combination string otherwise our function should return an empty string.
For example −
If the input string is −
const str = 'add';
Then our function can output −
const output = 'dad';
Example
Following is the code −
const str = 'add'; const formatString = (str = '') => { const map = {}; for(let i = 0; i < str.length; i++){ map[str[i]] = map[str[i]] || 0; map[str[i]] ++; } let keys = Object.keys(map).sort((a, b) => { if(map[a] < map[b]){ return 1; }; return -1; }); let flag = str.length%2?(Math.floor(str.length/2)+1):str.length/2; if(map[keys[0]] > flag){ return ""; }; const res = []; let index = 0, max = str.length-1; while(keys.length){ let currKey = keys.shift(); let count = map[currKey]; while(count){ res[index] = currKey; index = index+2; if(index>max) index=1; count--; } } return res.join(""); }; console.log(formatString(str));
Output
Following is the console output −
dad
- Related Articles
- How to build a string with no repeating character n separate list of characters? in JavaScript
- Java Program to Separate the Individual Characters from a String
- How can we separate the special characters in JavaScript?
- Regrouping characters of a string in JavaScript
- How to find unique characters of a string in JavaScript?
- Filtering string to contain unique characters in JavaScript
- Generate random string/characters in JavaScript?
- Remove characters from a string contained in another string with JavaScript?
- Sorting string characters by frequency in JavaScript
- Placing same string characters apart in JavaScript
- Finding count of special characters in a string in JavaScript
- Number of non-unique characters in a string in JavaScript
- Switching positions of selected characters in a string in JavaScript
- String Formatting in C# to add padding
- Recursive program to insert a star between pair of identical characters in C++

Advertisements