-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c0ac049
commit c22fcb1
Showing
9 changed files
with
91 additions
and
18 deletions.
There are no files selected for viewing
8 changes: 8 additions & 0 deletions
8
src/page-1/5. Longest Palindromic Substring/longestPalindrome.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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'); | ||
}); | ||
}); |
48 changes: 48 additions & 0 deletions
48
src/page-1/5. Longest Palindromic Substring/longestPalindrome.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
}; |
11 changes: 0 additions & 11 deletions
11
src/page-18/1880. Check if Word Equals Summation of Two Words/is-sum-equal.ts
This file was deleted.
Oops, something went wrong.
2 changes: 1 addition & 1 deletion
2
...mmation of Two Words/is-sum-equal.test.ts → ...Summation of Two Words/isSumEqual.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
src/page-18/1880. Check if Word Equals Summation of Two Words/isSumEqual.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
}; |
2 changes: 1 addition & 1 deletion
2
... Two Pairs/max-product-difference.test.ts → ...en Two Pairs/maxProductDifference.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 3 additions & 1 deletion
4
...tween Two Pairs/max-product-difference.ts → ...Between Two Pairs/maxProductDifference.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
... of a Given String/count-prefixes.test.ts → ...s of a Given String/countPrefixes.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters