Skip to content

Commit a410a72

Browse files
committed
Abbreviation
1 parent 0315c8a commit a410a72

File tree

2 files changed

+66
-0
lines changed

2 files changed

+66
-0
lines changed

Dynamic-Programming/Abbreviation.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/**
2+
* @description
3+
* Given two strings, `a` and `b`, determine if it's possible to make `a` equal
4+
* to `b` You can perform the following operations on the string `a`:
5+
* 1. Capitalize zero or more of `a`'s lowercase letters.
6+
* 2. Delete all the remaining lowercase letters in `a`.
7+
*
8+
* ### Algorithm
9+
* The idea is in the problem statement itself: iterate through characters of
10+
* string `a` and `b` (for character indexes `i` and `j` respectively):
11+
* 1. If `a[i]` and `b[j]` are equal, then move to next position
12+
* 2. If `a[i]` is lowercase of `b[j]`, then explore two possibilities:
13+
* a) Capitalize `a[i]` or
14+
* b) Skip `a[i]`
15+
* 3. If the `a[i]` is not uppercase, just discard that character, else return
16+
* `false`
17+
*
18+
* Time Complexity: (O(|a|*|b|)) where `|a|` => length of string `a`
19+
*
20+
* @param {String} a
21+
* @param {String} b
22+
* @returns {Boolean}
23+
* @see https://www.hackerrank.com/challenges/abbr/problem - Related problem on HackerRank.
24+
*/
25+
export const abbreviation = (a, b) => {
26+
const n = a.length
27+
const m = b.length
28+
29+
let dp = Array.from({length: n + 1}, () => Array(m + 1).fill(false))
30+
dp[0][0] = true
31+
32+
for (let i = 0; i < n; i++) {
33+
for (let j = 0; j <= m; j++) {
34+
if (dp[i][j]) {
35+
if (j < m && a[i].toUpperCase() === b[j]) {
36+
dp[i + 1][j + 1] = true
37+
}
38+
if (a[i] === a[i].toLowerCase()) {
39+
dp[i + 1][j] = true
40+
}
41+
}
42+
}
43+
}
44+
45+
return dp[n][m]
46+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { abbreviation } from '../Abbreviation.js'
2+
3+
describe('Abbreviation', () => {
4+
test('it can abbreviate string a to b', () => {
5+
expect(abbreviation('', '')).toBe(true)
6+
expect(abbreviation('a', '')).toBe(true)
7+
expect(abbreviation('', 'A')).toBe(false)
8+
expect(abbreviation('a', 'A')).toBe(true)
9+
expect(abbreviation('abcDE', 'ABCDE')).toBe(true)
10+
expect(abbreviation('ABcDE', 'ABCDE')).toBe(true)
11+
expect(abbreviation('abcde', 'ABCDE')).toBe(true)
12+
expect(abbreviation('abcde', 'ABC')).toBe(true)
13+
expect(abbreviation('a', 'ABC')).toBe(false)
14+
expect(abbreviation('abcXYdefghijKLmnopqrs', 'XYKL')).toBe(true)
15+
expect(abbreviation('aBcXYdefghijKLmnOpqrs', 'XYKLOP')).toBe(false)
16+
expect(abbreviation('abc123', 'ABC')).toBe(true)
17+
expect(abbreviation('abc123', 'ABC123')).toBe(true)
18+
expect(abbreviation('abc!@#def', 'ABC')).toBe(true)
19+
})
20+
})

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy