leetcode

Solution to some Leetcode problems written in C++
git clone git://git.dimitrijedobrota.com/leetcode.git
Log | Files | Refs | README | LICENSE

commit 4e616e9cf5c3404c50f7532b3583cfe421bcffd7
parent eb9d4957d3db84259b9023b98a51cb5ce4f6a3f9
Author: Dimitrije Dobrota <mail@dimitrijedobrota.com>
Date:   Sat,  2 Mar 2024 13:07:59 +0000

1 Random Problem

Diffstat:
AProblems/0833.cpp | 29+++++++++++++++++++++++++++++
MREADME.md | 1+
2 files changed, 30 insertions(+), 0 deletions(-)

diff --git a/Problems/0833.cpp b/Problems/0833.cpp @@ -0,0 +1,29 @@ +class Solution { + public: + string findReplaceString(const string &s, const vector<int> &indices, const vector<string> &sources, + const vector<string> &targets) const { + const int n = size(indices); + + static int order[10001]; + iota(begin(order), begin(order) + n, 0); + sort(begin(order), begin(order) + n, [&indices](int i, int j) { return indices[i] < indices[j]; }); + + string res; + int crnt = 0; + for (int i = 0; i < n; i++) { + const int idx = order[i], m = size(sources[idx]); + int pos = 0; + + if (indices[idx] < crnt) continue; + while (indices[idx] > crnt) + res += s[crnt++]; + while (pos < m && s[crnt + pos] == sources[idx][pos]) + pos++; + if (pos == m) res += targets[idx], crnt += m; + } + while (crnt < size(s)) + res += s[crnt++]; + + return res; + } +}; diff --git a/README.md b/README.md @@ -488,6 +488,7 @@ for solving problems. | 0820 | Medium | [Short Encoding of Words](Problems/0820.cpp) | | 0823 | Medium | [Binary Trees With Factors](Problems/0823.cpp) | | 0830 | Medium | [Kth Smallest Element in a BST](Problems/0230.cpp) | +| 0833 | Medium | [Find And Replace in String](Problems/0833.cpp) | | 0835 | Medium | [Image Overlap](Problems/0835.cpp) | | 0837 | Medium | [New 21 Game](Problems/0837.cpp) | | 0838 | Medium | [Push Dominoes](Problems/0838.cpp) |