Skip to content

Commit a29b2b2

Browse files
committed
Solve problems: Is Subsequence & Isomorphic Strings
1 parent d79e8df commit a29b2b2

File tree

7 files changed

+109
-0
lines changed

7 files changed

+109
-0
lines changed

README.MD

+2
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,5 @@ Solutions to problems from the [LeetCode Patterns](https://seanprashad.com/leetc
2323
| [Majority Element](https://leetcode.com/problems/majority-element) | [Solution](https://github.com/GolubevDS/LeetCodePatterns/blob/main/solutions/majorityElement/index.js) | Easy |
2424
| [Running Sum of 1d Array](https://leetcode.com/problems/running-sum-of-1d-array/) | [Solution](https://github.com/GolubevDS/LeetCodePatterns/blob/main/solutions/runningSum/index.js) | Easy |
2525
| [Find Pivot Index](https://leetcode.com/problems/find-pivot-index/) | [Solution](https://github.com/GolubevDS/LeetCodePatterns/blob/main/solutions/pivotIndex/index.js) | Easy |
26+
| [Is Subsequence](https://leetcode.com/problems/is-subsequence/) | [Solution](https://github.com/GolubevDS/LeetCodePatterns/blob/main/solutions/isSubsequence/isSubsequence.js) | Easy |
27+
| [Isomorphic Strings](https://leetcode.com/problems/isomorphic-strings/) | [Solution](https://github.com/GolubevDS/LeetCodePatterns/blob/main/solutions/isIsomorphic/isIsomorphic.js) | Easy |

solutions/isIsomorphic/README.MD

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
## [205. Isomorphic Strings](https://leetcode.com/problems/isomorphic-strings/)
2+
3+
Given two strings `s` and `t`, determine if they are isomorphic.
4+
5+
Two strings `s` and `t` are isomorphic if the characters in `s` can be replaced to get `t`.
6+
7+
All occurrences of a character must be replaced with another character while preserving the order of characters. No two characters may map to the same character, but a character may map to itself.
+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/**
2+
* @param {string} s
3+
* @param {string} t
4+
* @return {boolean}
5+
*/
6+
function isIsomorphic(s, t) {
7+
for (let i = 0; i < s.length; i++) {
8+
if (s.indexOf(s[i], i + 1) !== t.indexOf(t[i], i + 1))
9+
return false;
10+
}
11+
12+
return true;
13+
}
14+
15+
module.exports = isIsomorphic;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
const isIsomorphic = require('./isIsomorphic');
2+
3+
describe('isIsomorphic', () => {
4+
it('should return true when s and t are empty strings', () => {
5+
const result = isIsomorphic('', '');
6+
expect(result).toBe(true);
7+
});
8+
9+
it('should return true when s and t are isomorphic', () => {
10+
const result = isIsomorphic('egg', 'add');
11+
expect(result).toBe(true);
12+
});
13+
14+
it('should return false when s and t are not isomorphic', () => {
15+
const result = isIsomorphic('foo', 'bar');
16+
expect(result).toBe(false);
17+
});
18+
19+
it('should handle edge cases when s and t have different lengths', () => {
20+
const result = isIsomorphic('paper', 'title');
21+
expect(result).toBe(true);
22+
});
23+
24+
it('should handle edge cases when s and t have the same letters but different order', () => {
25+
const result = isIsomorphic('abcdef', 'ghijkl');
26+
expect(result).toBe(true);
27+
});
28+
});

solutions/isSubsequence/README.MD

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
## [392. Is Subsequence](https://leetcode.com/problems/is-subsequence/)
2+
3+
Given two strings `s` and `t`, return `true` if `s` is a subsequence of `t`, or `false` otherwise.
4+
5+
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).
+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* @param {string} s
3+
* @param {string} t
4+
* @return {boolean}
5+
*/
6+
function isSubsequence(s, t) {
7+
if (s.length > t.length) return false;
8+
9+
let sIndex = 0;
10+
11+
for(let i = 0; i < t.length; i++) {
12+
if (t[i] === s[sIndex]) sIndex++;
13+
if (sIndex === s.length) return true;
14+
}
15+
16+
return sIndex === s.length;
17+
}
18+
19+
module.exports = isSubsequence;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
const isSubsequence = require('./isSubsequence');
2+
3+
describe('Is Subsequence', () => {
4+
it('should return true when s is an empty string', () => {
5+
const result = isSubsequence('', 'hello world');
6+
expect(result).toBe(true);
7+
});
8+
9+
it('should return true when s is a substring of t', () => {
10+
const result = isSubsequence('abc', 'aabbcc');
11+
expect(result).toBe(true);
12+
});
13+
14+
it('should return false when s is not a substring of t', () => {
15+
const result = isSubsequence('xyz', 'hello world');
16+
expect(result).toBe(false);
17+
});
18+
19+
it('should return true when s is equal to t', () => {
20+
const result = isSubsequence('hello', 'hello');
21+
expect(result).toBe(true);
22+
});
23+
24+
it('should return true when s is a single character', () => {
25+
const result = isSubsequence('o', 'hello world');
26+
expect(result).toBe(true);
27+
});
28+
29+
it('should handle edge cases when s is longer than t', () => {
30+
const result = isSubsequence('hello world', 'hello');
31+
expect(result).toBe(false);
32+
});
33+
});

0 commit comments

Comments
 (0)