You are given a String S of size N, consisting of lowercase English characters. Now, you need to select a single English lowercase alphabet and delete all occurrences of it from the given string S
Considering you do the mentioned exactly once over the given string, what is the minimum possible length of the resultant string?
Example:
Input: n = 5, s= "aaaaa"
Output: 0
Approach
C++
#include <bits/stdc++.h>using namespace std;int stringFreqCount(int n, string s){int f[26] = {0};for (int i = 0; i < n; i++)f[s[i] - 'a']++;int max1 = INT_MIN;for (int i = 0; i < 26; i++){if (f[i] > max1)max1 = f[i];}return n - max1;}int main(){int n = 5;string s = "aaaaa";cout << stringFreqCount(n, s) << "\n";return 0;}
Java
public class StringT {public static void main(String[] args) {int n = 5;String s = "aaaaa";System.out.println(stringFreqCount(n, s));}private static int stringFreqCount(int n, String s) {int f[] = new int[26];for (int i = 0; i < n; i++)f[s.charAt(i) - 'a']++;int max1 = Integer.MIN_VALUE;for (int i = 0; i < 26; i++) {if (f[i] > max1)max1 = f[i];}return n - max1;}}
No comments:
Post a Comment