Skip to content

Commit

Permalink
152nd Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Shyam-Chen committed Jun 16, 2024
1 parent c0ac049 commit c22fcb1
Show file tree
Hide file tree
Showing 9 changed files with 91 additions and 18 deletions.
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 src/page-1/5. Longest Palindromic Substring/longestPalindrome.ts
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);
};

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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', () => {
Expand Down
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;
};
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { maxProductDifference } from './max-product-difference';
import { maxProductDifference } from './maxProductDifference';

describe('1913. Maximum Product Difference Between Two Pairs', () => {
test('maxProductDifference', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -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]];
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { countPrefixes } from './count-prefixes';
import { countPrefixes } from './countPrefixes';

describe('2255. Count Prefixes of a Given String', () => {
test('countPrefixes', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit c22fcb1

Please sign in to comment.