Skip to content

Abbreviation #1547

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 15, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions Dynamic-Programming/Abbreviation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/**
* @description
* Given two strings, `a` and `b`, determine if it's possible to make `a` equal
* to `b` You can perform the following operations on the string `a`:
* 1. Capitalize zero or more of `a`'s lowercase letters.
* 2. Delete all the remaining lowercase letters in `a`.
*
* ### Algorithm
* The idea is in the problem statement itself: iterate through characters of
* string `a` and `b` (for character indexes `i` and `j` respectively):
* 1. If `a[i]` and `b[j]` are equal, then move to next position
* 2. If `a[i]` is lowercase of `b[j]`, then explore two possibilities:
* a) Capitalize `a[i]` or
* b) Skip `a[i]`
* 3. If the `a[i]` is not uppercase, just discard that character, else return
* `false`
*
* Time Complexity: (O(|a|*|b|)) where `|a|` => length of string `a`
*
* @param {String} a
* @param {String} b
* @returns {Boolean}
* @see https://www.hackerrank.com/challenges/abbr/problem - Related problem on HackerRank.
*/
export const abbreviation = (a, b) => {
const n = a.length
const m = b.length

let dp = Array.from({length: n + 1}, () => Array(m + 1).fill(false))
dp[0][0] = true

for (let i = 0; i < n; i++) {
for (let j = 0; j <= m; j++) {
if (dp[i][j]) {
if (j < m && a[i].toUpperCase() === b[j]) {
dp[i + 1][j + 1] = true
}
if (a[i] === a[i].toLowerCase()) {
dp[i + 1][j] = true
}
}
}
}

return dp[n][m]
}
20 changes: 20 additions & 0 deletions Dynamic-Programming/tests/Abbreviation.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { abbreviation } from '../Abbreviation.js'

describe('Abbreviation', () => {
test('it can abbreviate string a to b', () => {
expect(abbreviation('', '')).toBe(true)
expect(abbreviation('a', '')).toBe(true)
expect(abbreviation('', 'A')).toBe(false)
expect(abbreviation('a', 'A')).toBe(true)
expect(abbreviation('abcDE', 'ABCDE')).toBe(true)
expect(abbreviation('ABcDE', 'ABCDE')).toBe(true)
expect(abbreviation('abcde', 'ABCDE')).toBe(true)
expect(abbreviation('abcde', 'ABC')).toBe(true)
expect(abbreviation('a', 'ABC')).toBe(false)
expect(abbreviation('abcXYdefghijKLmnopqrs', 'XYKL')).toBe(true)
expect(abbreviation('aBcXYdefghijKLmnOpqrs', 'XYKLOP')).toBe(false)
expect(abbreviation('abc123', 'ABC')).toBe(true)
expect(abbreviation('abc123', 'ABC123')).toBe(true)
expect(abbreviation('abc!@#def', 'ABC')).toBe(true)
})
})