File tree Expand file tree Collapse file tree 2 files changed +66
-0
lines changed Expand file tree Collapse file tree 2 files changed +66
-0
lines changed Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ } )
You can’t perform that action at this time.
0 commit comments