Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 39d0113

Browse files
Abbreviation (TheAlgorithms#1547)
* Abbreviation * Updates from code review
1 parent e9e3ea4 commit 39d0113

File tree

2 files changed

+77
-0
lines changed

2 files changed

+77
-0
lines changed

‎Dynamic-Programming/Abbreviation.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/**
2+
* @description
3+
* Given two strings, `source` and `target`, determine if it's possible to make `source` equal
4+
* to `target` You can perform the following operations on the string `source`:
5+
* 1. Capitalize zero or more of `source`'s lowercase letters.
6+
* 2. Delete all the remaining lowercase letters in `source`.
7+
*
8+
* Time Complexity: (O(|source|*|target|)) where `|source|` => length of string `source`
9+
*
10+
* @param {String} source - The string to be transformed.
11+
* @param {String} target - The string we want to transform `source` into.
12+
* @returns {Boolean} - Whether the transformation is possible.
13+
* @see https://www.hackerrank.com/challenges/abbr/problem - Related problem on HackerRank.
14+
*/
15+
export const isAbbreviation = (source, target) => {
16+
const sourceLength = source.length
17+
const targetLength = target.length
18+
19+
// Initialize a table to keep track of possible abbreviations
20+
let canAbbreviate = Array.from({ length: sourceLength + 1 }, () =>
21+
Array(targetLength + 1).fill(false)
22+
)
23+
// Empty strings are trivially abbreviatable
24+
canAbbreviate[0][0] = true
25+
26+
for (let sourceIndex = 0; sourceIndex < sourceLength; sourceIndex++) {
27+
for (let targetIndex = 0; targetIndex <= targetLength; targetIndex++) {
28+
if (canAbbreviate[sourceIndex][targetIndex]) {
29+
// If characters at the current position are equal, move to the next position in both strings.
30+
if (
31+
targetIndex < targetLength &&
32+
source[sourceIndex].toUpperCase() === target[targetIndex]
33+
) {
34+
canAbbreviate[sourceIndex + 1][targetIndex + 1] = true
35+
}
36+
// If the current character in `source` is lowercase, explore two possibilities:
37+
// a) Capitalize it (which is akin to "using" it in `source` to match `target`), or
38+
// b) Skip it (effectively deleting it from `source`).
39+
if (source[sourceIndex] === source[sourceIndex].toLowerCase()) {
40+
canAbbreviate[sourceIndex + 1][targetIndex] = true
41+
}
42+
}
43+
}
44+
}
45+
46+
return canAbbreviate[sourceLength][targetLength]
47+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { isAbbreviation } from '../Abbreviation.js'
2+
3+
const expectPositive = (word, abbr) =>
4+
expect(isAbbreviation(word, abbr)).toBe(true)
5+
const expectNegative = (word, abbr) =>
6+
expect(isAbbreviation(word, abbr)).toBe(false)
7+
8+
describe('Abbreviation - Positive Tests', () => {
9+
test('it should correctly abbreviate or transform the source string to match the target string', () => {
10+
expectPositive('', '')
11+
expectPositive('a', '')
12+
expectPositive('a', 'A')
13+
expectPositive('abcDE', 'ABCDE')
14+
expectPositive('ABcDE', 'ABCDE')
15+
expectPositive('abcde', 'ABCDE')
16+
expectPositive('abcde', 'ABC')
17+
expectPositive('abcXYdefghijKLmnopqrs', 'XYKL')
18+
expectPositive('abc123', 'ABC')
19+
expectPositive('abc123', 'ABC123')
20+
expectPositive('abc!@#def', 'ABC')
21+
})
22+
})
23+
24+
describe('Abbreviation - Negative Tests', () => {
25+
test('it should fail to abbreviate or transform the source string when it is not possible to match the target string', () => {
26+
expectNegative('', 'A')
27+
expectNegative('a', 'ABC')
28+
expectNegative('aBcXYdefghijKLmnOpqrs', 'XYKLOP')
29+
})
30+
})

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /