
- 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
Similar string groups in JavaScript
Two strings str1 and str2 are similar if we can swap two letters (in different positions) of str1, so that it equals str2. Also, two strings str1 and str2 are similar if they are equal.
For example, "tars" and "rats" are similar (swapping at positions 0 and 2), and "rats" and "arts" are similar, but "star" is not similar to "tars", "rats", or "arts".
Together, these form two connected groups by similarity: {"tars", "rats", "arts"} and {"star"}. Notice that "tars" and "arts" are in the same group even though they are not similar.
Formally, each group is such that a word is in the group if and only if it is similar to at least one other word in the group.
Given a list arr of strings where every string in arr is an anagram of every other string in arr. We are required to write a function that finds out how many groups are there.
Example
Following is the code −
const arr = ["tars","rats","arts","star"]; const isSimilar = (str1, str2) => { const obj = {} let counter = 0 for(let i=0; i< str1.length; i++){ if(str1[i] !== str2[i]) { counter++ } obj[str1[i]] = str2[i] } return counter === 2? true : false } const similarStringGroup = (arr = []) => { const group = [[arr[0]]] for(let i=1; i<arr.length; i++){ let match = false for(let j=0; j<group.length; j++){ for(let k=0; k< group[j].length; k++){ const booleanMatch = isSimilar(group[j][k], arr[i]) if(booleanMatch) { group[j].push(arr[i]); match = true break; } } if(match === true) { break } } if(match === false){ group.push([arr[i]]) } } return group.length } console.log(similarStringGroup(arr));
Output
Following is the console output −
2
- Related Articles
- Similar String Groups in C++
- Splitting string into groups – JavaScript
- Split string into groups - JavaScript
- Rearranging cards into groups in JavaScript
- Count groups of negatives numbers in JavaScript
- Splitting an array into groups in JavaScript
- Reduce an array to groups in JavaScript
- Group Similar Items in JSON in JavaScript
- Named capture groups JavaScript Regular Expressions
- Regex - reusing patterns to capture groups in JavaScript?
- Merge objects in array with similar key JavaScript
- Group strings starting with similar number in JavaScript
- Sum all similar elements in one array - JavaScript
- Matching strings for similar characters - JavaScript
- Separating data type from array into groups in JavaScript
