Skip to content

Commit

Permalink
136th Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Shyam-Chen committed Jun 8, 2024
1 parent 7cce0a0 commit 2c1961f
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 6 deletions.
22 changes: 16 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,15 @@ Ace Coding Interview with 75 Qs
<details>
<summary>Problems</summary>

| Array / String | | |
| ------------------------------- | ------------------------------------------------------------------------------------- | ---- |
| 1768. Merge Strings Alternately | [Solution](./src/page-17/1768.%20Merge%20Strings%20Alternately/merge-alternately.ts) | Easy |
| Array / String | | |
| ---------------------------------------- | ---------------- | ---- |
| 1768. Merge Strings Alternately | [Solution][1768] | Easy |
| 1071. Greatest Common Divisor of Strings | [Solution][1071] | Easy |

<!-- Array / String -->

[1768]: ./src/page-17/1768.%20Merge%20Strings%20Alternately/merge-alternately.ts
[1071]: ./src/page-11/1071.%20Greatest%20Common%20Divisor%20of%20Strings/gcdOfStrings.ts

</details>

Expand All @@ -77,8 +83,12 @@ Must-do List for Interview Prep
<details>
<summary>Problems</summary>

| Array / String | | |
| ---------------------- | --------------------------------------------------------------- | ---- |
| 88. Merge Sorted Array | [Solution](./src/page-1//88.%20Merge%20Sorted%20Array/merge.ts) | Easy |
| Array / String | | |
| ---------------------- | -------------- | ---- |
| 88. Merge Sorted Array | [Solution][88] | Easy |

<!-- Array / String -->

[88]: ./src/page-1//88.%20Merge%20Sorted%20Array/merge.ts

</details>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { gcdOfStrings } from './gcdOfStrings';

describe('1071. Greatest Common Divisor of Strings', () => {
it('gcdOfStrings', () => {
expect(gcdOfStrings('ABCABC', 'ABC')).toBe('ABC');
expect(gcdOfStrings('ABABAB', 'ABAB')).toBe('AB');
expect(gcdOfStrings('LEET', 'CODE')).toBe('');
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
type GcdOfStrings = (str1: string, str2: string) => string;

export const gcdOfStrings: GcdOfStrings = (str1, str2) => {
// Function to find GCD of two numbers
function gcd(a: number, b: number): number {
if (b === 0) return a;
return gcd(b, a % b);
}

// Function to check if str can divide baseStr
function divides(baseStr: string, str: string): boolean {
if (baseStr.length % str.length !== 0) return false;
const repeatedStr = str.repeat(baseStr.length / str.length);
return repeatedStr === baseStr;
}

// Get the GCD of the lengths of the two strings
const lenGCD = gcd(str1.length, str2.length);

// Get the candidate GCD string
const candidate = str1.substring(0, lenGCD);

// Check if the candidate can divide both strings
if (divides(str1, candidate) && divides(str2, candidate)) {
return candidate;
}

return '';
};

0 comments on commit 2c1961f

Please sign in to comment.