
- 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
Check whether we can form string2 by deleting some characters from string1 without reordering the characters of any string - JavaScript
We are required to write a JavaScript function that takes in two strings let’s say str1 and str2 as the first and the second argument.
The function should determine whether we can form str2 by deleting some characters from str1, without reordering the characters of any string.
For example −
If the two strings are −
const str1 = 'sjkfampeflef'; const str2 = 'sample';
Then the output should be true because we can form str2 by deleting some characters from str1.
Example
Following is the code −
const str1 = 'sjkfampeflef'; const str2 = 'sample'; const checkConvertibility = (str1 = '', str2 = '') => { if(!str1 || !str2){ return false; }; const strArr1 = str1.split(''); const strArr2 = str2.split(''); const shorter = strArr1.length < strArr2.length ? strArr1 : strArr2; const longer = strArr1.length < strArr2.length ? strArr2 : strArr1; for(let i = 0; i < shorter.length; i++){ const el = shorter[i]; const index = longer.indexOf(el); if(index !== -1){ longer.splice(index, 1); continue; }; return false; }; return true; }; console.log(checkConvertibility(str1, str2));
Output
And the output in the console will be −
true
- Related Articles
- Check whether second string can be formed from characters of first string in Python
- Find largest word in dictionary by deleting some characters of given string in C++
- Program to check whether we can make k palindromes from given string characters or not in Python?
- Check whether the String contains only digit characters in Java
- Check if characters of one string can be swapped to form other in Python
- Program to check whether palindrome can be formed after deleting at most k characters or not in python
- JavaScript Check whether string1 ends with strings2 or not
- Sorting string characters by frequency in JavaScript
- How can we separate the special characters in JavaScript?
- JavaScript - Remove first n characters from string
- JavaScript Remove non-duplicate characters from string
- Deleting the duplicate strings based on the ending characters - JavaScript
- Check if characters of a string can be made non-decreasing by replacing ‘_’s
- Check if characters of a given string can be rearranged to form a palindrome in Python
- Remove all characters of first string from second JavaScript

Advertisements