본문 바로가기
LeetCode/2020 LeetCoding Challenge

LeetCode. Is Subsequence

by 벤진[Benzene] 2020. 6. 10.

Problem:

Given a string s and a string t, check if s is subsequence of t.

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. (ie, "ace" is a subsequence of "abcde" while "aec" is not).

 

Follow up:
If there are lots of incoming S, say S1, S2, ... , Sk where k >= 1B, and you want to check one by one to see if T has its subsequence. In this scenario, how would you change your code?

 

Example 1:

Input: s = "abc", t = "ahbgdc"

Output: true

 

Example 2:

Input: s = "axc", t = "ahbgdc"

Output: false

 

-Summary-

Window-Sliding + Two-pointers problem

1. We set two-pointer for each string.

2. Loop

- Increase pointerS index if character found where current pointer indicates in 't' string.

- Increase pointerT index if character not found for a character in string 's'

 

3. After the loop, if pointerS is the same with the length of string 's'. Then we found all characters in s, return True.

   Otherwise, return False.

 

모든 문제에 대한 저작권은 LeetCode 회사에 있습니다. [Copyright © 2020 LeetCode]

댓글