Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork5.7k
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
Uh oh!
There was an error while loading.Please reload this page.
Merged
Abbreviation#1547
Changes fromall commits
Commits
Show all changes
3 commits Select commitHold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/** | ||
* @description | ||
* Given two strings, `source` and `target`, determine if it's possible to make `source` equal | ||
* to `target` You can perform the following operations on the string `source`: | ||
* 1. Capitalize zero or more of `source`'s lowercase letters. | ||
* 2. Delete all the remaining lowercase letters in `source`. | ||
* | ||
* Time Complexity: (O(|source|*|target|)) where `|source|` => length of string `source` | ||
* | ||
* @param {String} source - The string to be transformed. | ||
* @param {String} target - The string we want to transform `source` into. | ||
* @returns {Boolean} - Whether the transformation is possible. | ||
* @see https://www.hackerrank.com/challenges/abbr/problem - Related problem on HackerRank. | ||
*/ | ||
export const isAbbreviation = (source, target) => { | ||
const sourceLength = source.length | ||
const targetLength = target.length | ||
// Initialize a table to keep track of possible abbreviations | ||
let canAbbreviate = Array.from({ length: sourceLength + 1 }, () => | ||
Array(targetLength + 1).fill(false) | ||
) | ||
// Empty strings are trivially abbreviatable | ||
canAbbreviate[0][0] = true | ||
for (let sourceIndex = 0; sourceIndex < sourceLength; sourceIndex++) { | ||
for (let targetIndex = 0; targetIndex <= targetLength; targetIndex++) { | ||
if (canAbbreviate[sourceIndex][targetIndex]) { | ||
// If characters at the current position are equal, move to the next position in both strings. | ||
if ( | ||
targetIndex < targetLength && | ||
source[sourceIndex].toUpperCase() === target[targetIndex] | ||
) { | ||
canAbbreviate[sourceIndex + 1][targetIndex + 1] = true | ||
} | ||
// If the current character in `source` is lowercase, explore two possibilities: | ||
// a) Capitalize it (which is akin to "using" it in `source` to match `target`), or | ||
// b) Skip it (effectively deleting it from `source`). | ||
if (source[sourceIndex] === source[sourceIndex].toLowerCase()) { | ||
canAbbreviate[sourceIndex + 1][targetIndex] = true | ||
} | ||
} | ||
} | ||
} | ||
return canAbbreviate[sourceLength][targetLength] | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { isAbbreviation } from '../Abbreviation.js' | ||
const expectPositive = (word, abbr) => | ||
expect(isAbbreviation(word, abbr)).toBe(true) | ||
const expectNegative = (word, abbr) => | ||
expect(isAbbreviation(word, abbr)).toBe(false) | ||
describe('Abbreviation - Positive Tests', () => { | ||
test('it should correctly abbreviate or transform the source string to match the target string', () => { | ||
expectPositive('', '') | ||
expectPositive('a', '') | ||
expectPositive('a', 'A') | ||
expectPositive('abcDE', 'ABCDE') | ||
expectPositive('ABcDE', 'ABCDE') | ||
expectPositive('abcde', 'ABCDE') | ||
expectPositive('abcde', 'ABC') | ||
expectPositive('abcXYdefghijKLmnopqrs', 'XYKL') | ||
expectPositive('abc123', 'ABC') | ||
expectPositive('abc123', 'ABC123') | ||
expectPositive('abc!@#def', 'ABC') | ||
}) | ||
}) | ||
describe('Abbreviation - Negative Tests', () => { | ||
test('it should fail to abbreviate or transform the source string when it is not possible to match the target string', () => { | ||
expectNegative('', 'A') | ||
expectNegative('a', 'ABC') | ||
expectNegative('aBcXYdefghijKLmnOpqrs', 'XYKLOP') | ||
}) | ||
}) |
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.