Skip to content

Commit

Permalink
problem: new problem solution - 1662 . Check If Two String Arrays are…
Browse files Browse the repository at this point in the history
… Equivalent
  • Loading branch information
squxq committed Dec 1, 2023
1 parent f0b98a0 commit 2de2f33
Show file tree
Hide file tree
Showing 3 changed files with 181 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ My approach to solving LeetCode problems typically involves the following steps:
| 559 | [Maximum Depth of N-ary Tree](https://leetcode.com/problems/maximum-depth-of-n-ary-tree/) | Algorithms | [TypeScript](./problems/algorithms/maximumDepthOfNAryTree/MaximumDepthOfNAryTree.ts) | Easy |
| 563 | [Binary Tree Tilt](https://leetcode.com/problems/binary-tree-tilt/) | Algorithms | [TypeScript](./problems/algorithms/binaryTreeTilt/BinaryTreeTilt.ts) | Easy |
| 572 | [Subtree of Another Tree](https://leetcode.com/problems/subtree-of-another-tree/) | Algorithms | [TypeScript](./problems/algorithms/subtreeOfAnotherTree/SubtreeOfAnotherTree.ts) | Easy |
| 1662 | [Check If Two String Arrays are Equivalent](https://leetcode.com/problems/check-if-two-string-arrays-are-equivalent/) | Algorithms | [TypeScript](./problems/algorithms/checkIfTwoStringArraysAreEquivalent/CheckIfTwoStringArraysAreEquivalent.ts) | Easy |
| ... | ... | ... | ... | ... |

In this table:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Source : https://leetcode.com/problems/check-if-two-string-arrays-are-equivalent/
// Author : Francisco Tomas
// Date : 2023-12-01

import { arrayStringsAreEqual } from "./CheckIfTwoStringArraysAreEquivalent";

describe("check if two string arrays are equivalent", () => {
test("basic example", () => {
const result: boolean = arrayStringsAreEqual(["ab", "c"], ["a", "bc"]);
expect(result).toBe(true);
});

test("failing basic example", () => {
const result: boolean = arrayStringsAreEqual(["a", "cb"], ["ab", "c"]);
expect(result).toBe(false);
});

test("more complex example", () => {
const result: boolean = arrayStringsAreEqual(
["abc", "d", "defg"],
["abcddefg"],
);
expect(result).toBe(true);
});

test("failing more complex example", () => {
const result: boolean = arrayStringsAreEqual(
["abc", "d", "defg"],
["abcddef"],
);
expect(result).toBe(false);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
// Source : https://leetcode.com/problems/check-if-two-string-arrays-are-equivalent/
// Author : Francisco Tomas
// Date : 2023-12-01

/*****************************************************************************************************
*
* Given two string arrays word1 and word2, return true if the two arrays represent the same string,
* and false otherwise.
*
* A string is represented by an array if the array elements concatenated in order forms the string.
*
* Example 1:
*
* Input: word1 = ["ab", "c"], word2 = ["a", "bc"]
* Output: true
* Explanation:
* word1 represents string "ab" + "c" -> "abc"
* word2 represents string "a" + "bc" -> "abc"
* The strings are the same, so return true.
*
* Example 2:
*
* Input: word1 = ["a", "cb"], word2 = ["ab", "c"]
* Output: false
*
* Example 3:
*
* Input: word1 = ["abc", "d", "defg"], word2 = ["abcddefg"]
* Output: true
*
* Constraints:
*
* 1 <= word1.length, word2.length <= 10^3
* 1 <= word1[i].length, word2[i].length <= 10^3
* 1 <= sum(word1[i].length), sum(word2[i].length) <= 10^3
* word1[i] and word2[i] consist of lowercase letters.
******************************************************************************************************/

/* eslint-disable @typescript-eslint/no-non-null-assertion */

/**
* string[] string[] -> boolean
* given two string arrays, word1 and word2, return true if the two arrays represent the same string, and false otherwise
* Stub:
function arrayStringsAreEqual(word1: string[], word2: string[]): boolean {return false}
* Tests:
* I: word1 = ["ab", "c"], word2 = ["a", "bc"] -> O: true
* I: word1 = ["a", "cb"], word2 = ["ab", "c"] -> O: false
* I: word1 = ["abc", "d", "defg"], word2 = ["abcddefg"] -> O: true
* I: word1 = ["abc","d","defg"], word2 = ["abcddef"] -> O: false
* Constraints:
* - 1 <= word1.length, word2.length <= 10^3
* - 1 <= word1[i].length, word2[i].length <= 10^3
* - 1 <= sum(word1[i].length), sum(word2[i].length) <= 10^3
* - word1[i] and word2[i] consist of lowercase letters.
*/

/**
* Time Complexity: O(n + m), where n is the length of word1 and m is the length of word2
* Space Complexity: O(n + m)
* Runtime: 66ms (9.93%)
* Memory: 43.75MB (76.60%)
*/
export function arrayStringsAreEqualV1(
word1: string[],
word2: string[],
): boolean {
return word1.join("") === word2.join("");
}

/**
* Time Complexity: O(n + m), where n is the length of word1 and m is the length of word2
* Space Complexity: O(1)
* Runtime: 51ms (80.95%)
* Memory: 44.82MB (9.93%)
*/
export function arrayStringsAreEqualV2(
word1: string[],
word2: string[],
): boolean {
const i: { string: number; char: number } = { string: 0, char: 0 };
const j: { string: number; char: number } = { string: 0, char: 0 };
while (i.string < word1.length && j.string < word2.length) {
if (word1[i.string]![i.char] === word2[j.string]![j.char]) {
i.char++;
j.char++;

if (i.char === word1[i.string]!.length) {
i.string++;
i.char = 0;
}

if (j.char === word2[j.string]!.length) {
j.string++;
j.char = 0;
}
} else {
return false;
}
}
return i.string === word1.length && j.string === word2.length;
}

/**
* Time Complexity: O(n + m), where n is the length of word1 and m is the length of word2
* Space Complexity: O(1)
* Runtime: 43ms (97.87%)
* Memory: 44.54MB (29.79%)
*/
export function arrayStringsAreEqual(
word1: string[],
word2: string[],
): boolean {
let stringPointer1: number = 0;
let stringPointer2: number = 0;
let charPointer1: number = 0;
let charPointer2: number = 0;

while (stringPointer1 < word1.length && stringPointer2 < word2.length) {
if (
word1[stringPointer1]![charPointer1] ===
word2[stringPointer2]![charPointer2]
) {
charPointer1++;
charPointer2++;

if (charPointer1 === word1[stringPointer1]!.length) {
stringPointer1++;
charPointer1 = 0;
}

if (charPointer2 === word2[stringPointer2]!.length) {
stringPointer2++;
charPointer2 = 0;
}
} else {
return false;
}
}
return stringPointer1 === word1.length && stringPointer2 === word2.length;
}

0 comments on commit 2de2f33

Please sign in to comment.