From 2de2f33bb04fae57e573dcba16dce95241ef5afb Mon Sep 17 00:00:00 2001 From: Francisco Tomas Date: Fri, 1 Dec 2023 13:49:13 +0000 Subject: [PATCH] problem: new problem solution - 1662 . Check If Two String Arrays are Equivalent --- README.md | 1 + ...heckIfTwoStringArraysAreEquivalent.test.ts | 33 ++++ .../CheckIfTwoStringArraysAreEquivalent.ts | 147 ++++++++++++++++++ 3 files changed, 181 insertions(+) create mode 100644 problems/algorithms/checkIfTwoStringArraysAreEquivalent/CheckIfTwoStringArraysAreEquivalent.test.ts create mode 100644 problems/algorithms/checkIfTwoStringArraysAreEquivalent/CheckIfTwoStringArraysAreEquivalent.ts diff --git a/README.md b/README.md index f4a7751..65e051f 100644 --- a/README.md +++ b/README.md @@ -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: diff --git a/problems/algorithms/checkIfTwoStringArraysAreEquivalent/CheckIfTwoStringArraysAreEquivalent.test.ts b/problems/algorithms/checkIfTwoStringArraysAreEquivalent/CheckIfTwoStringArraysAreEquivalent.test.ts new file mode 100644 index 0000000..6b18335 --- /dev/null +++ b/problems/algorithms/checkIfTwoStringArraysAreEquivalent/CheckIfTwoStringArraysAreEquivalent.test.ts @@ -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); + }); +}); diff --git a/problems/algorithms/checkIfTwoStringArraysAreEquivalent/CheckIfTwoStringArraysAreEquivalent.ts b/problems/algorithms/checkIfTwoStringArraysAreEquivalent/CheckIfTwoStringArraysAreEquivalent.ts new file mode 100644 index 0000000..0e644aa --- /dev/null +++ b/problems/algorithms/checkIfTwoStringArraysAreEquivalent/CheckIfTwoStringArraysAreEquivalent.ts @@ -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; +}