
- 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
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 Questions & Answers
- Find largest word in dictionary by deleting some characters of given string
- 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
- 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?
- Program to check whether we can form 24 by placing operators in python
- Deleting the duplicate strings based on the ending characters - JavaScript
- Program to check whether palindrome can be formed after deleting at most k characters or not in python
- JavaScript Remove non-duplicate characters from string
- JavaScript - Remove first n characters from string
- Check if characters of a given string can be rearranged to form a palindrome in Python
Advertisements