
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
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
Dynamic Programming: Second String Subsequence of First in JavaScript
We are given two strings str1 and str2, we are required to write a function that checks if str1 is a subsequence of str2.
A subsequence of a string is a new string which is formed from the original string by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters.
For example, "ace" is a subsequence of "abcde" while "aec" is not
Example
const str1 = 'ace'; const str2 = 'abcde'; const isSubsequence = (str1, str2) => { let i=0; let j=0; while(i<str1.length){ if(j===str2.length){ return false; } if(str1[i]===str2[j]){ i++; } j++; }; return true; }; console.log(isSubsequence(str1, str2));
Output
And the output in the console will be −
true
Advertisements