Skip to content

Commit 3e3be10

Browse files
committed
Add solution #3481
1 parent d889cf3 commit 3e3be10

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2708,6 +2708,7 @@
27082708
3464|[Maximize the Distance Between Points on a Square](./solutions/3464-maximize-the-distance-between-points-on-a-square.js)|Hard|
27092709
3466|[Maximum Coin Collection](./solutions/3466-maximum-coin-collection.js)|Medium|
27102710
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|
27112712

27122713
## License
27132714

solutions/3481-apply-substitutions.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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+
};

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy