-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add JavaScript functions and categories
- Loading branch information
1 parent
97979c9
commit 78747ed
Showing
22 changed files
with
73 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
--- | ||
sidebar_position: 1 | ||
tags: | ||
- bonus | ||
--- | ||
|
||
# isSubsequence (Bonus) | ||
|
||
Write a function called <code>isSubsequence</code> which takes in two strings and checks whether the characters in the first string form a subsequence of the characters in the second string. In other words, the function should check whether the characters in the first string appear somewhere in the second string, without their order changing. | ||
|
||
### Example 1 | ||
|
||
``` | ||
Input: nums = 'hello', 'hello world' | ||
Output: true | ||
``` | ||
|
||
### Example 2 | ||
|
||
``` | ||
Input: nums = 'sing', 'sting' | ||
Output: true | ||
``` | ||
|
||
### Example 3 | ||
|
||
``` | ||
Input: nums = 'abc', 'abracadabra' | ||
Output: true | ||
``` | ||
|
||
### Example 4 | ||
|
||
``` | ||
Input: nums = 'abc', 'acb' | ||
Output: false (order matters) | ||
``` | ||
|
||
### Solution | ||
|
||
```jsx | ||
/** | ||
* @param {string} str1 | ||
* @param {string} str2 | ||
* @return {boolean} | ||
*/ | ||
function isSubsequence(str1, str2) { | ||
let i = 0; | ||
let j = 0; | ||
if (!str1) return true; | ||
while (j < str2.length) { | ||
if (str1[i] === str2[j]) i++; | ||
if (i === str1.length) return true; | ||
j++; | ||
} | ||
return false; | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"label": "Bonus", | ||
"position": 3, | ||
"link": { | ||
"type": "generated-index", | ||
"description": "5 minutes to learn the most important Docusaurus concepts." | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
function numberCompare(a, b) { | ||
return a - b; | ||
} | ||
|
||
[6, 4, 5, 10].sort(numberCompare); | ||
console.log([6, 4, 5, 10].sort(numberCompare)); // [4, 5, 6, 10] |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.