Skip to content

Latest commit

 

History

History
40 lines (30 loc) · 1.36 KB

풀이_LC392.md

File metadata and controls

40 lines (30 loc) · 1.36 KB

🫖 392. Is Subsequence

  • Date : 2021.11.14(일)
  • Time : 25분

Problem

  • Given two strings s and t, return true if s is a subsequence of t, or false otherwise. A subsequence of a string is a new string that is formed from the original string by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (i.e., "ace" is a subsequence of "abcde" while "aec" is not).

Constraints

  • 0 <= s.length <= 100
  • 0 <= t.length <= 10^4
  • s and t consist only of lowercase English letters.

Example

  • Input: s = "abc", t = "ahbgdc"
  • Output: true

풀이

    def isSubsequence(self, s: str, t: str) -> bool:
        if s == "":
            return True
        if t == "":
            return False

        i = 0
        for j in range(len(t)):
            if t[j] == s[i]:
                i += 1
                if i == len(s):
                    return True

        return i == len(s)

: s가 t의 순열인지 알아보는 알고리즘이다. 만약 s가 비어있다면 무조건 true이고 만약 t가 비어있다면 비교할 대상이 없으므로 False이다. for문을 돌면서 만약에 t의 문자열이 s에 있다면 +1을 해준다. 여기서 i를 점진적으로 늘려가는 이유는 순열을 찾는것이기 때문에 숫자가 올바르게 존재해야하기 때문이다.