From 9b40cb4d3105e8da3847aa5a522c21759bb1db97 Mon Sep 17 00:00:00 2001 From: diego Date: Wed, 23 Apr 2025 14:46:11 -0300 Subject: [PATCH] Implementing String coding problems --- .../coding/problems/01_isUnique.ts | 13 ++++++++++- .../coding/problems/02_checkPermutations.ts | 12 +++++++++- .../coding/problems/03_urlify.ts | 6 +++++ .../problems/04_palindromePermutation.ts | 16 +++++++++++++ .../coding/problems/05_oneAway.ts | 22 +++++++++++++++++- .../coding/problems/06_stringCompression.ts | 13 ++++++++++- .../coding/problems/07_rotateMatrix.ts | 15 ++++++++++++ .../coding/problems/08_zeroMatrix.ts | 23 +++++++++++++++++++ .../coding/problems/09_stringRotation.ts | 4 ++++ 9 files changed, 120 insertions(+), 4 deletions(-) diff --git a/technical-fundamentals/coding/problems/01_isUnique.ts b/technical-fundamentals/coding/problems/01_isUnique.ts index 07f432b1..7a9d07fb 100644 --- a/technical-fundamentals/coding/problems/01_isUnique.ts +++ b/technical-fundamentals/coding/problems/01_isUnique.ts @@ -3,4 +3,15 @@ // Implement an algorithm to determine if a string has all unique characters. // What if you cannot use additional data structures? -export default function isUnique(str: string): boolean {} +export default function isUnique(str: string): boolean { + const arr= str.split(''); + for (let i=0; i<=arr.length - 1; i++) { + for (let si = i + 1; si <= arr.length - 1; si++) { + if (arr[i] === arr[si]) { + return false; + } + } + } + + return true; +} diff --git a/technical-fundamentals/coding/problems/02_checkPermutations.ts b/technical-fundamentals/coding/problems/02_checkPermutations.ts index 56deb0f0..53a60702 100644 --- a/technical-fundamentals/coding/problems/02_checkPermutations.ts +++ b/technical-fundamentals/coding/problems/02_checkPermutations.ts @@ -2,4 +2,14 @@ // Given two strings, write a method to decide if one is a permutation of the other. -export default function checkPermutations(s1: string, s2: string): boolean {} +export default function checkPermutations(s1: string, s2: string): boolean { + if (s1.length !== s2.length) return false; + const chars: Record = {} + for (let i=0; i<=s1.length - 1; i++) { + + s1[i] in chars ? chars[s1[i]]++ : chars[s1[i]] = 1; + s2[i] in chars ? chars[s2[i]]-- : chars[s2[i]] = -1; + + } + return !Object.values(chars).some(item => item !== 0); +} diff --git a/technical-fundamentals/coding/problems/03_urlify.ts b/technical-fundamentals/coding/problems/03_urlify.ts index ca989b83..5190b0a3 100644 --- a/technical-fundamentals/coding/problems/03_urlify.ts +++ b/technical-fundamentals/coding/problems/03_urlify.ts @@ -6,4 +6,10 @@ export default function URLify (s1 : string): string { + return s1.split('').map(item => { + if (item === ' ') { + return '%20'; + } + return item; + }).join(''); } \ No newline at end of file diff --git a/technical-fundamentals/coding/problems/04_palindromePermutation.ts b/technical-fundamentals/coding/problems/04_palindromePermutation.ts index d80c9217..2f4d3a41 100644 --- a/technical-fundamentals/coding/problems/04_palindromePermutation.ts +++ b/technical-fundamentals/coding/problems/04_palindromePermutation.ts @@ -10,5 +10,21 @@ // ``` export default function palindromePermutation (str: string): boolean { + const sanitized = str.toLowerCase().replaceAll(' ', '').split(''); + + const [s1, s2] = sanitized.length % 2 === 0 ? + [sanitized.slice(0, sanitized.length / 2), sanitized.slice(sanitized.length / 2, sanitized.length)] : + [sanitized.slice(0, (sanitized.length / 2) + 1), sanitized.slice(sanitized.length / 2, sanitized.length)] + + const chars: Record = {} + for (let i=0; i<=s1.length - 1; i++) { + + s1[i] in chars ? chars[s1[i]]++ : chars[s1[i]] = 1; + s2[i] in chars ? chars[s2[i]]-- : chars[s2[i]] = -1; + + } + + return !Object.values(chars).some(item => item !== 0); + } \ No newline at end of file diff --git a/technical-fundamentals/coding/problems/05_oneAway.ts b/technical-fundamentals/coding/problems/05_oneAway.ts index 79c9a12d..117fa9ab 100644 --- a/technical-fundamentals/coding/problems/05_oneAway.ts +++ b/technical-fundamentals/coding/problems/05_oneAway.ts @@ -5,5 +5,25 @@ // Given two strings, write a function to check if they are one edit (or zero edits) away. export default function isOneAway(str1: string, str2: string): boolean { - + if (Math.abs(str1.length - str2.length) > 1 ) { + return false + } + + let acc = 0; + for (let i1=0, i2=0; i1 str2.length) { + i2--; + } + acc++ + } + if (acc>1) { + return false; + } + } + + + return true; } diff --git a/technical-fundamentals/coding/problems/06_stringCompression.ts b/technical-fundamentals/coding/problems/06_stringCompression.ts index 525ce40d..9fa28273 100644 --- a/technical-fundamentals/coding/problems/06_stringCompression.ts +++ b/technical-fundamentals/coding/problems/06_stringCompression.ts @@ -7,5 +7,16 @@ // You can assume the string has only uppercase and lowercase letters (a - z). export default function stringCompression (str: string) : string { - + + let count = 1; + let result = ''; + for (let i = 0; i <= str.length - 1; i++) { + if (str[i] === str[i+1]) { + count++; + } else { + result += str[i] + count; + count = 1; + } + } + return result.length >= str.length ? str : result; } \ No newline at end of file diff --git a/technical-fundamentals/coding/problems/07_rotateMatrix.ts b/technical-fundamentals/coding/problems/07_rotateMatrix.ts index 875d0841..e9c37141 100644 --- a/technical-fundamentals/coding/problems/07_rotateMatrix.ts +++ b/technical-fundamentals/coding/problems/07_rotateMatrix.ts @@ -7,4 +7,19 @@ type Matrix = number[][] export default function rotateMatrix (matrix: Matrix) { + const rotatedMatrix= []; + //move all cols from left to rows from top + for (let col = 0; col < matrix[0].length; col++) { + const cols: number[] = []; + for (let row = matrix.length - 1; row >= 0; row--) { + cols.push(matrix[row][col]); + } + rotatedMatrix.push(cols); + } + // now replace original matrix since the parameter is passed as reference + for (let row = 0; row < matrix.length; row++) { + for (let col = 0; col < matrix[row].length; col++) { + matrix[row][col] = rotatedMatrix[row][col]; + } + } } \ No newline at end of file diff --git a/technical-fundamentals/coding/problems/08_zeroMatrix.ts b/technical-fundamentals/coding/problems/08_zeroMatrix.ts index 5bf6941d..883df9cb 100644 --- a/technical-fundamentals/coding/problems/08_zeroMatrix.ts +++ b/technical-fundamentals/coding/problems/08_zeroMatrix.ts @@ -6,4 +6,27 @@ type Matrix = number[][] export default function zeroMatrix (matrix: Matrix) { + const rows: number[] = []; + const cols: number[] = []; + for (let row = 0; row < matrix.length; row++) { + if (matrix[row].includes(0)) { + rows.push(row) + } + for (let col = 0; col < matrix[row].length; col++) { + if (matrix[row][col] === 0) { + if (!cols.includes(col)) { + cols.push(col) + } + } + } + } + for (let row of rows) { + matrix[row] = matrix[row].map(() => 0) + } + for (let col of cols) { + for (let row = 0; row < matrix.length; row++) { + matrix[row][col] = 0 + } + } + return matrix; } \ No newline at end of file diff --git a/technical-fundamentals/coding/problems/09_stringRotation.ts b/technical-fundamentals/coding/problems/09_stringRotation.ts index f5bfa3ad..cf2355c9 100644 --- a/technical-fundamentals/coding/problems/09_stringRotation.ts +++ b/technical-fundamentals/coding/problems/09_stringRotation.ts @@ -10,4 +10,8 @@ function isSubstring(s1: string, s2: string): boolean { export default function stringRotation(s1: string, s2: string): boolean { + if (s1.length !== s2.length) return false; + + return isSubstring(s1 + s1, s2); + } \ No newline at end of file