Given a string** S** of Size N. Initially, the value of count is 0. The task is to find the value of count after N operations to remove all the N characters of the given string S where each operation is:

  • In each operation, select an alphabetically the smallest character in the string S and remove that character from S and add its index to count.
  • If multiple characters are present then remove the character having the smallest index.

Note: Consider string as 1-based indexing.

Examples:

Input:_ N = 5, S = “abcab” _

Output:_ 8 _

Explanation:

_Remove character ‘a’ then string becomes “bcab” and the count = 1. _

_Remove character ‘a’ then string becomes “bcb” and the count = 1 + 3 = 4. _

_Remove character ‘b’ then string becomes “cb” and the count = 4 + 1 = 5. _

_Remove character ‘b’ then string becomes “c” and the count = 5 + 2 = 7. _

Remove character ‘c’ then string becomes “” and the count = 7 + 1 = 8.

Input:_ N = 5 S = “aabbc” _

Output:_ 5 _

Explanation:

The value after 5 operations to remove all the 5 characters of String aabbc is 5.

Recommended: Please try your approach on {IDE} first, before moving on to the solution.

**Naive Approach: **The idea is to check if the string is empty or not. If it is not empty then following are the steps to solve the problem:

  • Find the first occurrence of the smallest alphabets in the current string, let’s say ind and remove it from the string.
  • Increase the count by **ind **+ 1.
  • Repeat the above step until the string becomes empty.

Below is the implementation of the above approach:

  • C++
  • Java
  • C#

// C++ program for the above approach

#include <iostream>

#include <string>

**using** **namespace** std;

// Function to find the value after

// N operations to remove all the N

// characters of string S

**void** charactersCount(string str, **int** n)

{

**int** count = 0;

// Iterate till N

**while** (n > 0) {

**char** cur = str[0];

**int** ind = 0;

**for** (``**int** j = 1; j < n; j++) {

**if** (str[j] < cur) {

cur = str[j];

ind = j;

}

}

// Remove character at ind and

// decrease n(size of string)

str.erase(str.begin() + ind);

n--;

// Increase count by ind+1

count += ind + 1;

}

cout << count << endl;

}

// Driver Code

**int** main()

{

// Given string str

string str = "aabbc"``;

**int** n = 5;

// Function call

charactersCount(str, n);

**return** 0;

}

Output:

5

Time Complexity:_ O(N2)_

Auxiliary Space:_ O(1)_

**Efficient Approach: **This problem can be solved using the concept of Binary Index Tree or Fenwick Tree. Below are the steps:

  • Initially, Store the values of indices of all the characters/alphabet in increasing order.
  • Start with the smallest alphabet ‘a’ and remove all ‘a’s in the order of there occurrence. After removing, select the next alphabet ‘b’, and repeat this step until all alphabets are removed.
  • Removing the character means that its corresponding value in the array is changed to 0, indicating that it is removed.
  • Before removing, find the position of the character in the string using the sum() method in Fenwick Tree and add the position value to the count.
  • After removing all the characters in the string, the value of count is obtained.

#advanced data structure #divide and conquer #strings #tree #segment-tree #trees

Find value after N operations to remove N characters of string S with given constraints
1.15 GEEK