Good String

Given a string S, print the minimum number of characters you have to remove from the string S to make it a good string. A good string is a string in which all the characters are distinct.

Example:

Input:  s = "aabc"
Output: 1

Approach

C++

#include <bits/stdc++.h>
using namespace std;

int goodString(string s)
{
    int n = s.size();
    int f[26] = {0};
    for (int i = 0i < ni++)
        f[s[i] - 'a']++;
    int ans = 0;
    for (int i = 0i < 26i++)
    {
        if (f[i] > 1)
            ans += f[i] - 1;
    }
    return ans;
}
int main()
{
    string s = "aabc";

    cout << goodString(s<< "\n";

    return 0;
}


No comments:

Post a Comment