You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
We use a hash table $\textit{d}$ to store the substitution mapping, and then define a function $\textit{dfs}$ to recursively replace the placeholders in the string.
82
+
83
+
The execution logic of the function $\textit{dfs}$ is as follows:
84
+
85
+
1. Find the starting position $i$ of the first placeholder in the string $\textit{s}$. If not found, return $\textit{s}$;
86
+
2. Find the ending position $j$ of the first placeholder in the string $\textit{s}$. If not found, return $\textit{s}$;
87
+
3. Extract the key of the placeholder, and then recursively replace the value of the placeholder $d[key]$;
88
+
4. Return the replaced string.
89
+
90
+
In the main function, we call the $\textit{dfs}$ function, pass in the text string $\textit{text}$, and return the result.
91
+
92
+
The time complexity is $O(m + n \times L)$, and the space complexity is $O(m + n \times L)$. Where $m$ is the length of the substitution mapping, and $n$ and $L$ are the length of the text string and the average length of the placeholders, respectively.
0 commit comments