Home » leet code » leet Code 392. Is Subsequence (easy)

leet Code 392. Is Subsequence (easy)

Is Subsequence Leetcode 392 : Hey there, coding enthusiasts! Welcome back to another exciting coding session. Today’s problem is a treat—literally! We’re going to solve the “Is Subsequence” problem.

leet Code 392.  Is Subsequence
leet Code 392. Is Subsequence

Step 1: Problem Overview :

The “Is Subsequence” problem, as presented in LeetCode 392, aims to determine if one given string, denoted as ‘s,‘ is a subsequence of another string, denoted as ‘t.’ A subsequence is essentially a new string formed by removing some (or none) of the characters from the original string, ‘t,’ without changing the relative order of the remaining characters.

Find the Longest Substring Without Repeating Characters: Solution and Algorithm

2: Initialization

  • We initialize two indices, ‘i’ and ‘j,’ to traverse through the characters of ‘s’ and ‘t,’ respectively.

3: Traversing the Strings

  • We iterate through both ‘s’ and ‘t’ simultaneously.
  • At each step, we compare the characters at indices ‘i’ and ‘j.’
  • If they are equal, we increment ‘i,’ effectively moving to the next character in ‘s.’
  • Regardless of the comparison result, we always increment ‘j’ to progress through ‘t.’

Leet Code: Longest Substring Without Repeating Characters Java | JavaScript | CPP | Python Solution

4: Completion Check

  • After the traversal, we check whether ‘i’ has reached the length of ‘s.’ If it has, this means that all characters in ‘s’ have been found in ‘t’ in the same relative order, making ‘s’ a sub sequence of ‘t.’
  • If ‘i’ is not equal to the length of ‘s,’ then ‘s’ cannot be a sub sequence of ‘t’ based on the given conditions.

5: Return Result

  • We return ‘true’ if ‘s’ is a sub sequence of ‘t’ and ‘false’ otherwise.

Leet Code : Longest Palindromic Substring Java | CPP | Java Script | Python Solution

Leet code :Zigzag Conversion solution c++ , Java , Python , JavaScript , Swift ,TypeScript

Certainly! Here’s the isSubsequence function implemented in C++, Java, Python, and JavaScript based above Approach and Explanation.

Code

C++: leet Code 392. Is Sub sequence

class Solution {
public:
    bool isSubsequence(string s, string t) {
        int i = 0, j = 0;  // Initialize indices for both strings

        while (i < s.length() && j < t.length()) {
            if (s[i] == t[j]) {
                i++;  // Move to the next character in string s
            }
            j++;  // Always move to the next character in string t
        }

        // If i reaches the length of s, it means all characters in s have been found in t
        return i == s.length();
    }
};

Java: leet Code 392. Is Sub sequence

public class Solution {
    public boolean isSubsequence(String s, String t) {
        int i = 0, j = 0;  // Initialize indices for both strings

        while (i < s.length() && j < t.length()) {
            if (s.charAt(i) == t.charAt(j)) {
                i++;  // Move to the next character in string s
            }
            j++;  // Always move to the next character in string t
        }

        // If i reaches the length of s, it means all characters in s have been found in t
        return i == s.length();
    }
}

Python: leet Code 392.

class Solution:
    def isSubsequence(self, s: str, t: str) -> bool:
        i, j = 0, 0  # Initialize indices for both strings

        while i < len(s) and j < len(t):
            if s[i] == t[j]:
                i += 1  # Move to the next character in string s
            j += 1  # Always move to the next character in string t

        # If i reaches the length of s, it means all characters in s have been found in t
        return i == len(s)

JavaScript: leet Code 392. Is Subsequence

class Solution {
    isSubsequence(s, t) {
        let i = 0, j = 0;  // Initialize indices for both strings

        while (i < s.length && j < t.length) {
            if (s.charAt(i) === t.charAt(j)) {
                i++;  // Move to the next character in string s
            }
            j++;  // Always move to the next character in string t
        }

        // If i reaches the length of s, it means all characters in s have been found in t
        return i === s.length;
    }
}

These implementations are equivalent and will return true if s is a subsequence of t, and false otherwise.

Leet Code : Three (3) Sum Java | CPP | Python Solution

Visit Our Blog ,Instagram , Youtube channel , Linkedin link to connect :

List of Some Important Leet Code Question :

Leave a Comment

Your email address will not be published. Required fields are marked *