From c22fcb11ed3446393ae487dccd7b10c6eca6cc17 Mon Sep 17 00:00:00 2001 From: Shyam-Chen Date: Sun, 16 Jun 2024 17:16:24 +0800 Subject: [PATCH] 152nd Commit --- .../longestPalindrome.test.ts | 8 ++++ .../longestPalindrome.ts | 48 +++++++++++++++++++ .../is-sum-equal.ts | 11 ----- ...s-sum-equal.test.ts => isSumEqual.test.ts} | 2 +- .../isSumEqual.ts | 27 +++++++++++ ...e.test.ts => maxProductDifference.test.ts} | 2 +- ...-difference.ts => maxProductDifference.ts} | 4 +- ...prefixes.test.ts => countPrefixes.test.ts} | 2 +- .../{count-prefixes.ts => countPrefixes.ts} | 5 +- 9 files changed, 91 insertions(+), 18 deletions(-) create mode 100644 src/page-1/5. Longest Palindromic Substring/longestPalindrome.test.ts create mode 100644 src/page-1/5. Longest Palindromic Substring/longestPalindrome.ts delete mode 100644 src/page-18/1880. Check if Word Equals Summation of Two Words/is-sum-equal.ts rename src/page-18/1880. Check if Word Equals Summation of Two Words/{is-sum-equal.test.ts => isSumEqual.test.ts} (85%) create mode 100644 src/page-18/1880. Check if Word Equals Summation of Two Words/isSumEqual.ts rename src/page-18/1913. Maximum Product Difference Between Two Pairs/{max-product-difference.test.ts => maxProductDifference.test.ts} (79%) rename src/page-18/1913. Maximum Product Difference Between Two Pairs/{max-product-difference.ts => maxProductDifference.ts} (94%) rename src/page-21/2255. Count Prefixes of a Given String/{count-prefixes.test.ts => countPrefixes.test.ts} (82%) rename src/page-21/2255. Count Prefixes of a Given String/{count-prefixes.ts => countPrefixes.ts} (63%) diff --git a/src/page-1/5. Longest Palindromic Substring/longestPalindrome.test.ts b/src/page-1/5. Longest Palindromic Substring/longestPalindrome.test.ts new file mode 100644 index 0000000..a8160ea --- /dev/null +++ b/src/page-1/5. Longest Palindromic Substring/longestPalindrome.test.ts @@ -0,0 +1,8 @@ +import { longestPalindrome } from './longestPalindrome'; + +describe('5. Longest Palindromic Substring', () => { + test('longestPalindrome', () => { + expect(longestPalindrome('babad')).toBe('aba'); + expect(longestPalindrome('cbbd')).toBe('bb'); + }); +}); diff --git a/src/page-1/5. Longest Palindromic Substring/longestPalindrome.ts b/src/page-1/5. Longest Palindromic Substring/longestPalindrome.ts new file mode 100644 index 0000000..1dec7c6 --- /dev/null +++ b/src/page-1/5. Longest Palindromic Substring/longestPalindrome.ts @@ -0,0 +1,48 @@ +type LongestPalindrome = (s: string) => string; + +/** + * Accepted + */ +export const longestPalindrome: LongestPalindrome = (s) => { + const n = s.length; + + // If the string is empty or has only one character, it's itself a palindrome + if (n < 2) return s; + + let start = 0; // Starting index of the longest palindromic substring + let maxLength = 1; // Length of the longest palindromic substring + + // Initialize a 2D DP array + const dp: boolean[][] = Array.from({ length: n }, () => Array(n).fill(false)); + + // All substrings of length 1 are palindromes + for (let i = 0; i < n; i++) { + dp[i][i] = true; + } + + // Check for substrings of length 2 + for (let i = 0; i < n - 1; i++) { + if (s[i] === s[i + 1]) { + dp[i][i + 1] = true; + start = i; + maxLength = 2; + } + } + + // Check for substrings of length greater than 2 + for (let len = 3; len <= n; len++) { + for (let i = 0; i <= n - len; i++) { + // Ending index of substring + const j = i + len - 1; + + if (s[i] === s[j] && dp[i + 1][j - 1]) { + dp[i][j] = true; + start = i; + maxLength = len; + } + } + } + + // Extract the longest palindromic substring from start index and maxLength + return s.substring(start, start + maxLength); +}; diff --git a/src/page-18/1880. Check if Word Equals Summation of Two Words/is-sum-equal.ts b/src/page-18/1880. Check if Word Equals Summation of Two Words/is-sum-equal.ts deleted file mode 100644 index 75c2337..0000000 --- a/src/page-18/1880. Check if Word Equals Summation of Two Words/is-sum-equal.ts +++ /dev/null @@ -1,11 +0,0 @@ -type IsSumEqual = (firstWord: string, secondWord: string, targetWord: string) => boolean; - -export const isSumEqual: IsSumEqual = (firstWord, secondWord, targetWord) => { - const sum = (str: string): number => - Array.from(str).reduce((acc, cur) => Number(acc) + (cur.charCodeAt(0) - 'a'.charCodeAt(0)), 0); - - const source = sum(firstWord) + sum(secondWord); - const target = sum(targetWord); - - return source === target; -}; diff --git a/src/page-18/1880. Check if Word Equals Summation of Two Words/is-sum-equal.test.ts b/src/page-18/1880. Check if Word Equals Summation of Two Words/isSumEqual.test.ts similarity index 85% rename from src/page-18/1880. Check if Word Equals Summation of Two Words/is-sum-equal.test.ts rename to src/page-18/1880. Check if Word Equals Summation of Two Words/isSumEqual.test.ts index e5ae176..0b691b8 100644 --- a/src/page-18/1880. Check if Word Equals Summation of Two Words/is-sum-equal.test.ts +++ b/src/page-18/1880. Check if Word Equals Summation of Two Words/isSumEqual.test.ts @@ -1,4 +1,4 @@ -import { isSumEqual } from './is-sum-equal'; +import { isSumEqual } from './isSumEqual'; describe('1880. Check if Word Equals Summation of Two Words', () => { test('isSumEqual', () => { diff --git a/src/page-18/1880. Check if Word Equals Summation of Two Words/isSumEqual.ts b/src/page-18/1880. Check if Word Equals Summation of Two Words/isSumEqual.ts new file mode 100644 index 0000000..1c4bed5 --- /dev/null +++ b/src/page-18/1880. Check if Word Equals Summation of Two Words/isSumEqual.ts @@ -0,0 +1,27 @@ +type IsSumEqual = (firstWord: string, secondWord: string, targetWord: string) => boolean; + +/** + * Accepted + */ +export const isSumEqual: IsSumEqual = (firstWord, secondWord, targetWord) => { + // Helper function to convert a word to its numerical value + function getNumericalValue(word: string): number { + let numericalString = ''; + + for (const char of word) { + // Convert char to its letter value by subtracting 'a' char code + numericalString += (char.charCodeAt(0) - 'a'.charCodeAt(0)).toString(); + } + + // Convert the concatenated string to a number + return Number.parseInt(numericalString, 10); + } + + // Calculate numerical values for each word + const numFirstWord = getNumericalValue(firstWord); + const numSecondWord = getNumericalValue(secondWord); + const numTargetWord = getNumericalValue(targetWord); + + // Check if the sum of numFirstWord and numSecondWord equals numTargetWord + return numFirstWord + numSecondWord === numTargetWord; +}; diff --git a/src/page-18/1913. Maximum Product Difference Between Two Pairs/max-product-difference.test.ts b/src/page-18/1913. Maximum Product Difference Between Two Pairs/maxProductDifference.test.ts similarity index 79% rename from src/page-18/1913. Maximum Product Difference Between Two Pairs/max-product-difference.test.ts rename to src/page-18/1913. Maximum Product Difference Between Two Pairs/maxProductDifference.test.ts index 645507c..db0831f 100644 --- a/src/page-18/1913. Maximum Product Difference Between Two Pairs/max-product-difference.test.ts +++ b/src/page-18/1913. Maximum Product Difference Between Two Pairs/maxProductDifference.test.ts @@ -1,4 +1,4 @@ -import { maxProductDifference } from './max-product-difference'; +import { maxProductDifference } from './maxProductDifference'; describe('1913. Maximum Product Difference Between Two Pairs', () => { test('maxProductDifference', () => { diff --git a/src/page-18/1913. Maximum Product Difference Between Two Pairs/max-product-difference.ts b/src/page-18/1913. Maximum Product Difference Between Two Pairs/maxProductDifference.ts similarity index 94% rename from src/page-18/1913. Maximum Product Difference Between Two Pairs/max-product-difference.ts rename to src/page-18/1913. Maximum Product Difference Between Two Pairs/maxProductDifference.ts index 3ed4e7d..1bdfdd9 100644 --- a/src/page-18/1913. Maximum Product Difference Between Two Pairs/max-product-difference.ts +++ b/src/page-18/1913. Maximum Product Difference Between Two Pairs/maxProductDifference.ts @@ -1,6 +1,8 @@ type MaxProductDifference = (nums: number[]) => number; -// Accepted +/** + * Accepted + */ export const maxProductDifference: MaxProductDifference = (nums) => { const sorted = nums.sort((a, b) => b - a); const [max1, max2] = [sorted[0], sorted[1]]; diff --git a/src/page-21/2255. Count Prefixes of a Given String/count-prefixes.test.ts b/src/page-21/2255. Count Prefixes of a Given String/countPrefixes.test.ts similarity index 82% rename from src/page-21/2255. Count Prefixes of a Given String/count-prefixes.test.ts rename to src/page-21/2255. Count Prefixes of a Given String/countPrefixes.test.ts index 46a8147..ab27ea8 100644 --- a/src/page-21/2255. Count Prefixes of a Given String/count-prefixes.test.ts +++ b/src/page-21/2255. Count Prefixes of a Given String/countPrefixes.test.ts @@ -1,4 +1,4 @@ -import { countPrefixes } from './count-prefixes'; +import { countPrefixes } from './countPrefixes'; describe('2255. Count Prefixes of a Given String', () => { test('countPrefixes', () => { diff --git a/src/page-21/2255. Count Prefixes of a Given String/count-prefixes.ts b/src/page-21/2255. Count Prefixes of a Given String/countPrefixes.ts similarity index 63% rename from src/page-21/2255. Count Prefixes of a Given String/count-prefixes.ts rename to src/page-21/2255. Count Prefixes of a Given String/countPrefixes.ts index b759c23..90bc867 100644 --- a/src/page-21/2255. Count Prefixes of a Given String/count-prefixes.ts +++ b/src/page-21/2255. Count Prefixes of a Given String/countPrefixes.ts @@ -6,9 +6,8 @@ type CountPrefixes = (words: string[], s: string) => number; export const countPrefixes: CountPrefixes = (words, s) => { let count = 0; - for (let i = 0; i < words.length; i++) { - const cur = words[i]; - if (s.startsWith(cur)) count += 1; + for (const word of words) { + if (s.startsWith(word)) count += 1; } return count;