Clean the string
Practice
4.3 (9 votes)
String searching
Greedy algorithms
Algorithms
String
String algorithms
Problem
29% Success 909 Attempts 30 Points 1s Time Limit 256MB Memory 1024 KB Max Code

El has a string \(S\) consisting of '\(0\)', '\(1\)' and '\(2\)' as only characters. El is a sorted person, so he does not like randomness, so when he looks at the string and finds that characters in string are in random order, he becomes frustrated. He wants to clean up the string, i.e, he wants all same characters in the string to appear together. In one move El can swap any two characters in the string, i.e, he can select index \(i\), \(j\) then swap \(S_i\) and \(S_j\). Find the minimum number of moves required to clean the string.

Input Format

  • First line contains an integer \(T\), which denotes the number of test cases.
  • Each line of the test case contains a  string \(S\) consisting of '\(0\)', '\(1\)' and '\(2\)' as only characters.

Output Format

For each test case print only one integer - the minimum no. of moves required.

Constraint

\(1 \leq T \leq 10^4 \\ 1 \leq |S| \leq 2 \cdot 10^5 \\ \text{Sum of length of strings for all test case does not exceed} \ 2\cdot10^5\)

 

Please login to use the editor

You need to be logged in to access the code editor

Loading...

Please wait while we load the editor

Loading...
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:30
8 votes
Tags:
String AlgorithmsAlgorithmsString Searching
Points:30
127 votes
Tags:
AlgorithmsApprovedKMP AlgorithmMediumOpenString Manipulation
Points:30
3 votes
Tags:
AlgorithmsString SearchingString AlgorithmsC++