Check whether we can form string2 by deleting some characters from string1 without reordering the characters of any string - JavaScript


In this given statement our task is to determine whether we can form string2 by removing some characters from the string1 without changing the order of the characters of any string with the help of Javascript functionalities.

Understanding the problem

The problem at hand is to check that the second string can be formed using the first string without changing the order of both the strings. For example suppose we have two strings as:

s1 = "apple"
s2 = "ale"
Output - true

So the output for the above strings should be true because the characters of second string s2 are in the same order as first string s1.

Another example -

s3 = "banana"
s4 = "pan"
Output - false

The output for the above example is false because s4 can not be formed using the s3 without reordering the characters. Even p is not present in the string s3.

Logic for the given problem

For solving the above problem we will use a simple algorithm. So first we will iterate over each character of string2 and then we will check that the characters exist in string1. And if the characters are found in the string1 so we will remove them from the string1 one by one. And suppose if the character is not found and if we have reached at the end of the string1 without finding all the characters of string2. Then the second string2 can not be formed by removing characters from string1.

Algorithm

Step 1: As we have to check that the characters of the second string can be formed using the first string without changing its order. So for doing this task we will first create a function called canFormString and in this function we will pass two parameters string1 and string2. These two strings will be used to check for the given problem.

Step 2: Now we have created a function for checking the characters. So we will initialize two pointers i and j. These pointers will be used to iterate over the string1 and string2 respectively.

Step 3: After defining the pointers, we will iterate until the end of the string2 and all the characters of string2 are found in string1.

Step 4: In this step we will check the conditions. If the character of the first string is equal to the character of the second string. So we will increment the value of both i and j.

Step 5: If the above condition is not true then we will only increment the value of i pointer.

Step 6: Now we will check the condition if the value of j is equal to the length of the second string that means all the characters of string2 have been found in string1 without changing the order. So we will return true.

Step 7: And if the value of i is equal to the length of the string1 so that means we have arrived at the end of the string1 without finding all characters of string2. So that we will return the output as false.

Example

//Function to check that the string can be formed using first string
function canFormString(string1, string2) {
   //Initialize the pointers
   let i = 0;
   let j = 0;

   while (j < string2.length && i < string1.length) {
      if (string1[i] === string2[j]) {
         i++;
         j++;
      } else {
         i++;
      }
   }

   return j === string2.length;
}

const s1 = "hello i am Neel";
const s2 = "hello";

console.log(canFormString(s1, s2));

Output

true

Complexity

The time complexity for checking that the string2 is formed using the characters of string1 without changing the order of both the strings is O(m + n), here m is the length of the string1 and n is the length of the string2. The reason for this complexity is that we have iterated over the strings once for comparing the characters of the strings. The space complexity for the code is O(1), which is a constant value, because we only use a constant amount of space for boolean value.

Conclusion

So we have successfully completed the given problem by implementing the code in Javascript. So this code can be used to check whether the second string is formed using the first string or not without changing the order of strings. And the code has a linear time complexity which makes the code efficient for practical use.

Updated on: 11-Aug-2023

144 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements