

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Can part of a string be rearranged to form another string in JavaScript
Problem
We are required to write a JavaScript function that takes in two strings, str1 and str2. Our function should return true if a portion of str1 characters can be rearranged to match str2, otherwise returns false.
Example
Following is the code −
const str1 = 'rkqodlw'; const str2 = 'world'; const canForm = (str1 = '', str2 = '') => { if(str1.length < str2.length){ return false; }; const res = str2.split(''); str1.split("").forEach(val => { if(res.includes(val)){ res.splice(res.indexOf(val), 1); }; }); return res.length === 0; }; console.log(canForm(str1, str2));
Output
Following is the console output −
true
- Related Questions & Answers
- Check if a string can be rearranged to form special palindrome in Python
- Check if characters of a given string can be rearranged to form a palindrome in Python
- Replace part of a string with another string in C++
- Can one string be repeated to form other in JavaScript
- How to write a JavaScript function that returns true if a portion of string 1 can be rearranged to string 2?
- Check if a string can be repeated to make another string in Python
- C++ program to find how many characters should be rearranged to order string in sorted form
- Check if the elements of the array can be rearranged to form a sequence of numbers or not in JavaScript
- Count occurrences of a string that can be constructed from another given string in C++
- Check if a string can be formed from another string using given constraints in Python
- Check if a string can be obtained by rotating another string 2 places in Python
- Can the string be segmented in JavaScript
- Check if a string can be converted to another string by replacing vowels and consonants in Python
- Count appearances of a string in another - JavaScript
- Check If a String Can Break Another String in C++
Advertisements