Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 09e32fa

Browse files
feat(LC): add 392, notes, optimizations
1 parent a6ceb45 commit 09e32fa

File tree

1 file changed

+164
-0
lines changed

1 file changed

+164
-0
lines changed
Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
/**
2+
* @see {@link https://leetcode.com/problems/is-subsequence/?envType=study-plan&id=level-1 392. Is Subsequence}
3+
*/
4+
5+
/**
6+
* EXPLAIN
7+
* A subsequence is defined as where letters is s occur in t in the order that they appear in s
8+
* so s = "abc", t = "ahbgdc" === true, because a appears before b appears before c appears
9+
*
10+
* all lowercase
11+
* CASE?: s = "abc", t = "abac" === true! b/c you can delete second "a"
12+
*/
13+
14+
/**
15+
* APPROACH
16+
*
17+
* create placeholder string to collect matches
18+
* one pointer at start of s
19+
* one pointer at start of t
20+
*
21+
* LOOP through t
22+
* grab curr char
23+
* IF curr char matches pointer char of s
24+
* YES add curr char to placeholder string
25+
* advance s pointer
26+
* advance t pointer
27+
* NO ONLY advance t pointer
28+
* END LOOP
29+
*
30+
* IF placeholder length === length of s
31+
* YES return true
32+
* NO return false
33+
*/
34+
35+
/**
36+
* ORIGINAL CODE: ACCEPTED
37+
* SPEED: 71ms - 59%
38+
* MEMORY: 42MB - 59%
39+
*/
40+
/**
41+
* @param {string} s
42+
* @param {string} t
43+
* @return {boolean}
44+
*/
45+
var isSubsequence = function (s, t) {
46+
// Track order of matching subsequence
47+
let match = "";
48+
49+
// Starting index of s
50+
let sPoint = 0;
51+
52+
// Loop through t
53+
for (let tPoint = 0; tPoint < t.length; tPoint++) {
54+
// Grab both chars; s at its index, t at curr loop index
55+
const tChar = t[tPoint];
56+
const sChar = s[sPoint];
57+
58+
// If both chars match, add char to match string
59+
if (tChar === sChar) {
60+
match += tChar;
61+
sPoint++;
62+
}
63+
// not equal; do nothing, just advance to next char in t
64+
}
65+
66+
// Compare lengths
67+
// if equal: s is a subsequence of t
68+
// if not equal: s is NOT a subsequence of t
69+
if (match.length === s.length) {
70+
return true;
71+
}
72+
73+
return false;
74+
};
75+
76+
/**
77+
* 2ND CODE - no match string; only integers
78+
* SPEED: 78ms - 43%
79+
* MEMORY: 41MB - 99%
80+
*/
81+
/**
82+
* @param {string} s
83+
* @param {string} t
84+
* @return {boolean}
85+
*/
86+
var isSubsequence = function (s, t) {
87+
// Starting index of s
88+
let sPoint = 0;
89+
90+
// Loop through t
91+
for (let tPoint = 0; tPoint < t.length; tPoint++) {
92+
// Grab both chars; s at its index, t at curr loop index
93+
const tChar = t[tPoint];
94+
const sChar = s[sPoint];
95+
96+
// If both chars match, increment s pointer
97+
if (tChar === sChar) {
98+
sPoint++;
99+
}
100+
// not equal; do nothing, just advance to next char in t
101+
}
102+
103+
// Compare pointer count to s length
104+
// if equal: s is a subsequence of t
105+
// if not equal: s is NOT a subsequence of t
106+
if (sPoint === s.length) {
107+
return true;
108+
}
109+
110+
return false;
111+
};
112+
113+
/**
114+
* 3RD CODE: add base cases
115+
* SPEED: 54ms - 98%
116+
* MEMORY: 41.9MB - 59%
117+
*/
118+
/**
119+
* @param {string} s
120+
* @param {string} t
121+
* @return {boolean}
122+
*/
123+
var isSubsequence = function (s, t) {
124+
// BASE CASES
125+
// s cannot be longer than t
126+
if (s.length > t.length) return false;
127+
128+
// s and t are same length, but not same strings
129+
if (s.length === t.length && s !== t) return false;
130+
131+
// Starting index of s
132+
let sPoint = 0;
133+
134+
// Loop through t
135+
for (let tPoint = 0; tPoint < t.length; tPoint++) {
136+
// Grab both chars; s at its index, t at curr loop index
137+
const tChar = t[tPoint];
138+
const sChar = s[sPoint];
139+
140+
// If both chars match, increment s pointer
141+
if (tChar === sChar) {
142+
sPoint++;
143+
}
144+
// not equal; do nothing, just advance to next char in t
145+
}
146+
147+
// Compare pointer count to s length
148+
// if equal: s is a subsequence of t
149+
// if not equal: s is NOT a subsequence of t
150+
if (sPoint === s.length) {
151+
return true;
152+
}
153+
154+
return false;
155+
};
156+
157+
/**
158+
* READ 02:10
159+
* EXPLAIN 04:52 07:02
160+
* APPROACH 13:44 20:47
161+
* CODE 04:45 25:33
162+
* TEST 00:17 25:51
163+
* OPTIMIZE 14:40 40:32
164+
*/

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /