Is Subsequence | Leetcode 392

preview_player
Показать описание
#coding #leetcode #python #javascript #programming In This video I show you how to solve Leetcode 392 - Is Subsequence
Рекомендации по теме
Комментарии
Автор

You should remove `or s==t`because string comparison loops over the strings and checks equality of each character. In other words this comparison adds another N operations to your already O(N) algorithm.

In the worst case, where s and t are completely different strings of equal length N, the if statement will go through N comparisons to determine if the strings are equal. Then the 4 loop will go through another N iterations to check if t is a substring of s. This results in a total of 2N operations.

If you remove the initial comparison, those first N operations are removed, and you only have N operations. You also still check if s==t with the for loop you wrote anyways, as it will still return true if the strings are equal.

This means you can speed this up 2x in the worst case by removing that comparison

jemzhatfelid
Автор

id say subset, not subsequence. Imo subsequence requires it to be in the same sequence

duckner
Автор

What if there are repeat letters?
S=ace
t=aabcde

RoyChen-dpqf