Udemy JS Algorithms & Data Structures Masterclass- Section 6 Exercise 5

Multiple Pointers - isSubsequence

Write a function called isSubsequence which takes in two strings and checks whether the characters in the first string form a subsequence of the characters in the second string. In other words, the function should check whether the characters in the first string appear somewhere in the second string, without their order changing.

function isSubsequence(string1, string2) { //find letter in second string //each letter found after should come after the previous letter //each letter after should be searched after position of previous letter let pointer = 0; let pointer2 = 0; let match = 0; while (pointer < string1.length && pointer2 < string2.length) { if (string1.charAt(pointer) === string2.charAt(pointer2)) { pointer++; pointer2++; match++; } else if (string1.charAt(pointer) !== string2.charAt(pointer2)) { pointer2++; } } if (match === string1.length) return true; else return false; }

Comments

  1. Caesars Palace Casino Review & Ratings by Experts - Dr
    Caesars Palace Casino is an online 토토사이트 gambling site that serves players from the 포항 출장샵 UK. The 구미 출장마사지 casino is 삼척 출장마사지 owned and operated 순천 출장마사지 by The Entertainment

    ReplyDelete

Post a Comment

Popular posts from this blog

Code Wars: Data Reverse (6 kyu)

Code Wars: longest_palindrome (6 kyu)

Code Wars: Find the odd int