Minimum number of operations to make all letters in the string equal Last Updated : 15 Sep, 2023 Comments Improve Suggest changes Like Article Like Report Given a string "str" consisting of lowercase alphabets, the task is to find the minimum number of operations to be applied on string "str" such that all letters of the string "str" are the same. In one operation you can either remove all even indexed letters or you can remove all odd indexed characters. Examples: Input: str = "cbcdce"Output: 1Explanation: After removing all odd-indexed letters we are left with the string "ccc" in which all the characters are the same. Hence number of operations required is 1. Input: str = "abacaaba"Output: 2Explanation: First removing all odd-indexed elements we are left with the string "aaab". Now again removing odd - indexed elements we have the string "aa". Hence number of operations required is 2. Approach: The idea is to use the concept of recursion. Follow the steps below to solve the problem: If all characters in the string are the same then return 0.Here we have two choices, we can either remove all odd indexed elements or even indexed elements and simply this can be done by recursion.In 1 operation remove all even indexed elements and solve for the remaining string.Similarly, remove all odd-indexed elements and solve for the remaining string.Return the minimum of above 2.Below is the code implementation of the above approach: C++ #include <iostream> #include <string> bool AllCharsAreSame(const std::string& arr) { char ch = arr[0]; for (int i = 1; i < arr.length(); i++) { if (arr[i] != ch) return false; } return true; } int solve(std::string& s) { // If all characters of string are // same then simply answer is zero if (AllCharsAreSame(s)) return 0; // Generate the new strings by removing // either odd indexed or even indexed elements std::string sb1, sb2; for (int i = 0; i < s.length(); i++) { if (i % 2 == 0) { sb1 += s[i]; } else { sb2 += s[i]; } } // Recursively solve for subproblems // generated and compute the result using their answers return 1 + std::min(solve(sb1), solve(sb2)); } int main() { std::string s = "abacaaba"; std::cout << solve(s) << std::endl; return 0; } Java // Java code to implement above approach import java.io.*; import java.util.*; public class GFG { public static void main(String[] args) { // Scanner scn=new Scanner(System.in); // String s = scn.next(); String s = "abacaaba"; StringBuilder sb = new StringBuilder(s); System.out.println(solve(sb)); } static int solve(StringBuilder s) { // If all characters of string are // same then simply answer is zero if (AllCharsAreSame(s)) return 0; // Generate the new strings by removing // either odd indexed or even // indexed elements StringBuilder sb1 = new StringBuilder(); StringBuilder sb2 = new StringBuilder(); for (int i = 0; i < s.length(); i++) { if (i % 2 == 0) { sb1.append(s.charAt(i) + ""); } else { sb2.append(s.charAt(i) + ""); } } // Recursively solve for subproblems // generated and computing the // result using their answers return 1 + Math.min(solve(sb1), solve(sb2)); } // Function to check in a string all // characters are same or not static boolean AllCharsAreSame(StringBuilder arr) { char ch = arr.charAt(0); for (int i = 1; i < arr.length(); i++) { if (arr.charAt(i) != ch) return false; } return true; } } Python # Function to check if all characters in the string are the same def all_chars_are_same(arr): ch = arr[0] for i in range(1, len(arr)): if arr[i] != ch: return False return True # Recursive function to solve the problem def solve(s): # If all characters of the string are the same, the answer is zero if all_chars_are_same(s): return 0 # Generate new strings by removing either odd-indexed or even-indexed elements sb1 = "" sb2 = "" for i in range(len(s)): if i % 2 == 0: sb1 += s[i] else: sb2 += s[i] # Recursively solve for subproblems generated and compute the result using their answers return 1 + min(solve(sb1), solve(sb2)) s = "abacaaba" print(solve(s)) C# using System; public class Program { public static bool AllCharsAreSame(string arr) { char ch = arr[0]; for (int i = 1; i < arr.Length; i++) { if (arr[i] != ch) return false; } return true; } public static int Solve(string s) { // If all characters of string are // same then simply answer is zero if (AllCharsAreSame(s)) return 0; // Generate the new strings by removing // either odd indexed or even indexed elements string sb1 = "", sb2 = ""; for (int i = 0; i < s.Length; i++) { if (i % 2 == 0) { sb1 += s[i]; } else { sb2 += s[i]; } } // Recursively solve for subproblems // generated and compute the result using their answers return 1 + Math.Min(Solve(sb1), Solve(sb2)); } public static void Main() { string s = "abacaaba"; Console.WriteLine(Solve(s)); } } // This code is contributed by Tapesh(tapeshdua420) JavaScript // Function to check if all characters in the string are the same function allCharsAreSame(arr) { const ch = arr[0]; for (let i = 1; i < arr.length; i++) { if (arr[i] !== ch) return false; } return true; } // Recursive function to solve the problem function solve(s) { // If all characters of the string are the same, the answer is zero if (allCharsAreSame(s)) return 0; // Generate new strings by removing either odd-indexed or even-indexed elements let sb1 = ""; let sb2 = ""; for (let i = 0; i < s.length; i++) { if (i % 2 === 0) { sb1 += s[i]; } else { sb2 += s[i]; } } // Recursively solve for subproblems generated and compute the result using their answers return 1 + Math.min(solve(sb1), solve(sb2)); } const s = "abacaaba"; console.log(solve(s)); Output2Time Complexity: O(N*log(N)), T(n) = 2*T(n/2) + 2O(n) now using master's theorem time complexity comes out to be O(Nlog(N)).Auxiliary Space: O(N) Comment More infoAdvertise with us Next Article Minimum number of operations to make all letters in the string equal K karandeep1234 Follow Improve Article Tags : Strings Recursion DSA Algorithms-Recursion strings +1 More Practice Tags : RecursionStringsStrings Similar Reads Minimum number of increment operations required to make two Strings equal Given two strings S1 and S2 of the same length containing characters ranging from '0' to '9', the task is to return the minimum number of operations required to make two strings equal by following the below operations: Increment any two characters from the string by 1.If it is not possible to make t 8 min read Minimum number of given operations required to make two strings equal Given two strings A and B, both strings contain characters a and b and are of equal lengths. There is one _ (empty space) in both the strings. The task is to convert first string into second string by doing the minimum number of the following operations: If _ is at position i then _ can be swapped w 15 min read Minimum number of operations required to make two strings equal Given Two Strings s1 and s2 containing only lowercase letters of same length. The task is to make these strings equal by using the minimum number of operations. In one operation you can equalize any letter to any other alphabet. Examples: Input: S1 = "abb", S2 = "dad" Output: 2 a -> d b -> a E 9 min read Minimum move to end operations to make all strings equal Given n strings that are permutations of each other. We need to make all strings same with an operation that takes front character of any string and moves it to the end. Examples: Input: n = 2, arr[] = {"molzv", "lzvmo"}Output: 2Explanation: In first string, we remove first element("m") from first s 13 min read Find minimum operations to make all characters of a string identical Given a string of size N, the task is to find the minimum operation required in a string such that all elements of a string are the same. In one operation, you can choose at least one character from the string and remove all chosen characters from the string, but you can't choose an adjacent charact 11 min read Find the minimum number of preprocess moves required to make two strings equal Given two strings A and B of equal lengths consisting of lower case English letters. The task is to count the minimum number of pre-processing moves on string A required to make it equal to string B after applying below operations: Choose any index i (0 ? i < n) and swap characters ai and bi.Choo 9 min read Minimum number of additions to make the string balanced Given a string str of lowercase characters, the task is to find the minimum number of characters that need to add to the string in order to make it balanced. A string is said to be balanced if and only if the number of occurrences of each of the characters is equal. Examples: Input: str = "geeksforg 6 min read Javascript Program for Minimum move to end operations to make all strings equal Given n strings that are permutations of each other. We need to make all strings same with an operation that takes front character of any string and moves it to the end.Examples: Input : n = 2 arr[] = {"molzv", "lzvmo"}Output : 2Explanation: In first string, we removefirst element("m") from first st 2 min read Transform One String to Another using Minimum Number of Given Operation Given two strings A and B, the task is to convert A to B if possible. The only operation allowed is to put any character from A and insert it at front. Find if it's possible to convert the string. If yes, then output minimum no. of operations required for transformation.Examples: Input: A = "ABD", B 15+ min read Minimum number of given operations required to convert a string to another string Given two strings S and T of equal length. Both strings contain only the characters '0' and '1'. The task is to find the minimum number of operations to convert string S to T. There are 2 types of operations allowed on string S: Swap any two characters of the string.Replace a '0' with a '1' or vice 14 min read Like