
- 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
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 Articles
- 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
- How to write a JavaScript function that returns true if a portion of string 1 can be rearranged to string 2?
- Replace part of a string with another string in C++
- Can one string be repeated to form other in JavaScript
- C++ program to find how many characters should be rearranged to order string in sorted form
- Check if a string can be repeated to make another string in Python
- 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
- Check if a string can be converted to another string by replacing vowels and consonants in Python
- Checking if one string can be achieved from another with single tweak in JavaScript
- Can the string be segmented in JavaScript
- Check If a String Can Break Another String in C++

Advertisements