- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Matching strings for similar characters - JavaScript
We are required to write a JavaScript function that accepts two string and a number n.
The function matches the two strings i.e., it checks if the two strings contains the same characters.
The function returns true if both the strings contain the same character irrespective of their order or if they contain at most n different characters, else the function should return false.
Example
Following is the code −
const str = 'some random text'; const str2 = 'some r@ndom text'; const deviationMatching = (first, second, num) => { let count = 0; for(let i = 0; i < first.length; i++){ if(!second.includes(first[i])){ count++; }; if(count > num){ return false; }; }; return true; }; console.log(deviationMatching(str, str2, 1));
Output
This will produce the following output in console −
true
- Related Articles
- Group strings starting with similar number in JavaScript
- JavaScript filter an array of strings, matching case insensitive substring?
- How to return all matching strings against a RegEx in JavaScript?
- How to get substring between two similar characters in JavaScript?
- Matching Nonprintable Characters using Java regex
- How to sort strings with accented characters using JavaScript?
- K-Similar Strings in C++
- Python – Filter Similar Case Strings
- Matching strings with a wildcard in C#
- Deleting the duplicate strings based on the ending characters - JavaScript
- Finding and returning uncommon characters between two strings in JavaScript
- Program to find value of K for K-Similar Strings in C++
- Forming and matching strings of an array based on a random string in JavaScript
- Program to find smallest value of K for K-Similar Strings in Python
- Python – Equidistant consecutive characters Strings

Advertisements