leetcode

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

commit ee50f61f2a4975426b500be88ae4aff74455bd34
parent 67ecf731b5e0e4e8a23b30e9d47c07b222e42568
Author: Dimitrije Dobrota <mail@dimitrijedobrota.com>
Date:   Thu,  2 Mar 2023 20:48:08 +0100

Daily Problem

Diffstat:
AProblems/0443.cpp | 23+++++++++++++++++++++++
MREADME.md | 1+
2 files changed, 24 insertions(+), 0 deletions(-)

diff --git a/Problems/0443.cpp b/Problems/0443.cpp @@ -0,0 +1,23 @@ +class Solution { +public: + int compress(vector<char> &chars) { + char prev = chars[0]; + int count = 1, crnt = 0; + for (int i = 1; i < chars.size(); i++) { + if (chars[i] == prev) + count++; + else { + chars[crnt++] = prev; + if (count != 1) + for (char c : to_string(count)) chars[crnt++] = c; + count = 1; + prev = chars[i]; + } + } + chars[crnt++] = prev; + if (count != 1) + for (char c : to_string(count)) chars[crnt++] = c; + + return crnt; + } +}; diff --git a/README.md b/README.md @@ -238,6 +238,7 @@ for solving problems. | 0435 | Medium | [Non-overlapping Intervals](Problems/0435.cpp) | | 0437 | Medium | [Path Sum III](Problems/0437.cpp) | | 0438 | Medium | [Find All Anagrams in a String](Problems/0438.cpp) | +| 0443 | Medium | [String Compression](Problems/0443.cpp) | | 0445 | Medium | [Add Two Numbers II](Problems/0445.cpp) | | 0448 | Easy | [Find All Numbers Disappeared in an Array](Problems/0448.cpp) | | 0450 | Medium | [Delete Node in a BST](Problems/0450.cpp) |