1
1
/**
2
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 `.
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
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`
8
+ * Time Complexity: (O(|source|*|target|)) where `|source|` => length of string `source`
17
9
*
18
- * Time Complexity: (O(|a|*|b|)) where `|a|` => length of string `a`
19
- *
20
- * @param {String } a
21
- * @param {String } b
22
- * @returns {Boolean }
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.
23
13
* @see https://www.hackerrank.com/challenges/abbr/problem - Related problem on HackerRank.
24
14
*/
25
- export const abbreviation = ( a , b ) => {
26
- const n = a . length
27
- const m = b . length
15
+ export const isAbbreviation = ( source , target ) => {
16
+ const sourceLength = source . length
17
+ const targetLength = target . length
28
18
29
- let dp = Array . from ( { length : n + 1 } , ( ) => Array ( m + 1 ) . fill ( false ) )
30
- dp [ 0 ] [ 0 ] = true
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
31
25
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
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
37
35
}
38
- if ( a [ i ] === a [ i ] . toLowerCase ( ) ) {
39
- dp [ i + 1 ] [ j ] = true
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
40
41
}
41
42
}
42
43
}
43
44
}
44
45
45
- return dp [ n ] [ m ]
46
- }
46
+ return canAbbreviate [ sourceLength ] [ targetLength ]
47
+ }
0 commit comments