File tree Expand file tree Collapse file tree 2 files changed +36
-0
lines changed Expand file tree Collapse file tree 2 files changed +36
-0
lines changed Original file line number Diff line number Diff line change 2708
2708
3464|[ Maximize the Distance Between Points on a Square] ( ./solutions/3464-maximize-the-distance-between-points-on-a-square.js ) |Hard|
2709
2709
3466|[ Maximum Coin Collection] ( ./solutions/3466-maximum-coin-collection.js ) |Medium|
2710
2710
3476|[ Maximize Profit from Task Assignment] ( ./solutions/3476-maximize-profit-from-task-assignment.js ) |Medium|
2711
+ 3481|[ Apply Substitutions] ( ./solutions/3481-apply-substitutions.js ) |Medium|
2711
2712
2712
2713
## License
2713
2714
Original file line number Diff line number Diff line change
1
+ /**
2
+ * 3481. Apply Substitutions
3
+ * https://leetcode.com/problems/apply-substitutions/
4
+ * Difficulty: Medium
5
+ *
6
+ * You are given a replacements mapping and a text string that may contain placeholders
7
+ * formatted as %var%, where each var corresponds to a key in the replacements mapping.
8
+ * Each replacement value may itself contain one or more such placeholders. Each placeholder
9
+ * is replaced by the value associated with its corresponding replacement key.
10
+ *
11
+ * Return the fully substituted text string which does not contain any placeholders.
12
+ */
13
+
14
+ /**
15
+ * @param {string[][] } replacements
16
+ * @param {string } text
17
+ * @return {string }
18
+ */
19
+ var applySubstitutions = function ( replacements , text ) {
20
+ const replacementMap = new Map ( replacements ) ;
21
+ const cache = new Map ( ) ;
22
+
23
+ return text . replace ( / % ( [ A - Z ] ) % / g, ( match , key ) => helper ( key ) ) ;
24
+
25
+ function helper ( key ) {
26
+ if ( cache . has ( key ) ) {
27
+ return cache . get ( key ) ;
28
+ }
29
+
30
+ const value = replacementMap . get ( key ) ;
31
+ const result = value . replace ( / % ( [ A - Z ] ) % / g, ( match , innerKey ) => helper ( innerKey ) ) ;
32
+ cache . set ( key , result ) ;
33
+ return result ;
34
+ }
35
+ } ;
You can’t perform that action at this time.
0 commit comments