Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

3223. Minimum Length of String After Operations #500

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions C++/3223_Minimum-length-of-string-after-operations.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
class Solution
{
public:
int minimumLength(string s)
{
// Step 1: Initialize a frequency map to count occurrences of each
// character
unordered_map<char, int> charFreq;

// Step 2: Count the frequency of each character in the string
for (char currChar : s)
{
charFreq[currChar]++;
}

// Step 3: Calculate the total length after deletions count
int totalLen = 0;
for (const auto &entry : charFreq)
{
// If frequency is even, we can remove all occurrences
// If frequency is odd, we can remove all but one occurrence
totalLen += (entry.second % 2 == 0) ? 2 : 1;
}

// Step 4: Return the minimum length after deletions count
return totalLen;
}
};
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,7 @@ Check out ---> [Sample PR](https://github.com/codedecks-in/LeetCode-Solutions/pu
| 554 | [Brick Wall](https://leetcode.com/problems/brick-wall/) | [C++](./C++/brick-walls.cpp) | _O(n)_ | _O(n)_ | Medium | |
| 146 | [LRU Cache](https://leetcode.com/problems/lru-cache/) | [Javascript](../JavaScript/146.LRU-Cache.js) | _O(log(n))_ | _O(n)_ | Medium | |
| 389 | [Find The Difference](https://leetcode.com/problems/find-the-difference/) | [C++](../C++/Find-The-Difference.cpp) | _O(n)_ | _O(1)_ | Easy | |
| 3223 | [Minimum Length Of String After Operations](https://leetcode.com/problems/minimum-length-of-string-after-operations/) | [C++](../C++/Find-The-Difference.cpp) | _O(n)_ | _O(1)_ | Medium | Hash Table, String, Counting |

<br/>
<div align="right">
Expand Down Expand Up @@ -515,6 +516,8 @@ DISCLAIMER: This above mentioned resources have affiliate links, which means if
| [Shrimadh V Rao](https://github.com/Shrimadh) <br> <img src="https://avatars.githubusercontent.com/u/64469917?v=4" width="100" height="100"> | India | C++ | [GitHub](https://github.com/Shrimadh)
| [Shreyas Shrawage](https://github.com/shreyventure) <br> <img src = "https://avatars.githubusercontent.com/u/55741087?v=4" width="100" height="100"> | India | Python | [CodeChef](https://www.codechef.com/users/shreyventure)<br/>[LeetCode](https://leetcode.com/shreyventure/)<br/>[HackerRank](https://www.hackerrank.com/shreyas_shrawage)
| [Surbhi Mayank](https://github.com/surbhi2408) <br> <img src="https://avatars.githubusercontent.com/u/58289829?s=400&u=68fd396819b927ec4d8820d87d6d1e311c3abd01&v=4" width="100" height="100"> | India | C++ | [GitHub](https://github.com/surbhi2408)
| [Krishna Kesarwani](https://github.com/KK-is-Coding) <br> <img src="https://avatars.githubusercontent.com/u/99733616?v=4" width="100" height="100"> | India | C++ | [GitHub](https://github.com/KK-is-Coding)

<div align="right">
<b><a href="#algorithms">⬆️ Back to Top</a></b>
</div>
Expand Down