Is Subsequence | LeetCode 392 | Google Coding Interview Tutorial

Sdílet
Vložit
  • čas přidán 7. 09. 2024

Komentáře • 24

  • @TerribleWhiteboard
    @TerribleWhiteboard  Před 4 lety +10

    Updated code that only uses one while loop:
    let isSubsequence = function(s, t) {
    if (s.length === 0) {
    return true;
    }
    let pointer1 = 0;
    let pointer2 = 0;
    while (pointer1 < s.length && pointer2 < t.length) {
    if (t.charAt(pointer2) === s.charAt(pointer1)) {
    pointer1++;
    }
    pointer2++;
    }
    return pointer1 === s.length;
    };
    Also, if there are any videos you'd like me to make or if you have any ideas on how to optimize this solution, let me know!

    • @sathishraj1
      @sathishraj1 Před 4 lety +3

      If S is acb and T is abcd , S is not a subsequence of T right? But it will fail here right,?

    • @TerribleWhiteboard
      @TerribleWhiteboard  Před 4 lety +3

      Hm. Why would it fail?

    • @sathishraj1
      @sathishraj1 Před 4 lety +3

      @@TerribleWhiteboard My bad.. It will not fail..By the way , u are an awesome problem solver and tutor.. Thank you !

    • @TerribleWhiteboard
      @TerribleWhiteboard  Před 4 lety +3

      Thanks!

    • @mileicampeonmundial
      @mileicampeonmundial Před 4 lety +1

      @@TerribleWhiteboard but what if abc and abdxi you return true cuz pointer1 === s.length but it is not

  • @followerOfChrist212-x5n
    @followerOfChrist212-x5n Před 4 lety +5

    Great Explanation...thanks buddy

  • @jillfleming9742
    @jillfleming9742 Před 4 lety +6

    Thanks for the detail.

  • @lilycooper7672
    @lilycooper7672 Před 4 lety +6

    Great solution!

  • @ajaywadhwa3398
    @ajaywadhwa3398 Před 4 lety +4

    Thanks for your explanations .Really helped alot.

  • @BryantCabrera
    @BryantCabrera Před 3 lety

    I find your videos so helpful, thank you!

  • @dharmiknaik1772
    @dharmiknaik1772 Před 3 lety +1

    Thanks for the explanation.

  • @NareshKumar-dw9xp
    @NareshKumar-dw9xp Před 4 lety

    Thanks for all this . Easy and straight forward :)

  • @sumitnegi2708
    @sumitnegi2708 Před 4 lety +4

    that nested while loop was unnecessary.

  • @valikonen
    @valikonen Před 3 lety

    Your example it is to complicated...
    function isSub(t, str) {
    if(t === '') {
    return true;
    }
    if(t.length > str.length) {
    return false;
    }
    for(let i = 0; i < t.length; i++) {
    if(str.indexOf(t[i]) === -1) {
    return false;
    }
    }
    return true;
    }

  • @shadmanmartinpiyal4057

    var isSubsequence = function(s, t) {
    let pS = 0, pT = 0;
    while(!!s[pS] && !!t[pT]){
    if(s[pS] === t[pT]){
    pS++;
    }
    pT++;
    }
    return (pS - 1) === (s.length - 1)
    };