String Range Queries to count number of distinct character with updates


A string range query is the range of characters present in the string where a character starts from the index[0] and the last index[] may be specified according to the length of a given string. In this article, we are going to learn how string range queries count the number of distinct characters with their updates.

Let’s take an example of counting the number of distinct characters of a string with its update.

string = “tutorialpoint” // original string

The given string is the length of 12. So the count is 13(counting always starts from 1).

If we add the character ‘s’ after ‘l’ then the length will be 13 and the count is 14.

Now the update string is tutorialspoint.

To count the distinct character of an updated string, it becomes 10 as the distinct character comes only once. For example- “tt” there is a total of two characters after distinction it becomes 1.

Syntax

unordered_map < datatype, string > Map_Name 

parameters

unordered_map − This is the standard template library of c++ that stores elements including keys with value pairs.

datatype − The datatype is represented as a key and this is the type of data mentioned by the user.

string − The string is represented as a value.

Map_Name − Any name was given to the map.

.first 
.second

Parameters

.first- This represents the first part of the string pair.

.second- This represents the second part of the string pair.

Algorithm

  • We will start the program with the header files namely ‘iostream’ and ‘unordered_map’.

  • Now we are starting with the main function to initialize an empty ‘unordered_map <char, int>’ to store the count of each character of a given string.

  • We are using a first-for loop to traverse the string character by character (charCount++).

  • We are using a second-for loop to print the distinct character and its count. Under this loop, we print the following −

    p.first- To print the character of a string.

    p.second- This will count the number of each entry in a character.

  • After completing the second for loop, we update the third index to p to get a newly updated string and its count. Next, simply modify the string of characters of the previous index using [] on the string. Then update the count [l--] on the map by decrementing the count of the old character and with the help of [p++] it will increment the new character to the string.

  • Finally, we print the distinct character with their count and updated string.

Example

In this program, we are going to find the string range queries to count the number of distinct characters with updates.

#include <iostream>
#include <unordered_map>
using namespace std;
int main() {
   string s = "school";

   // counting of distinct characters in a string
   unordered_map<char, int> charCount;
   for (char c : s) {
      charCount[c]++;
   }
   cout << "The Distinct characters of the string: ";
   for (auto p : charCount) {
      cout << p.first << "(" << p.second << ")\t ";
   }
   cout << endl;
   //updation of string count and character distinction
   s[3] = 'p'; 
   
   // user can use any character 
   charCount['l']--;
   charCount['p']++;
   cout<< "*********:After Updation:*********";
   
   // print the distinct characters along with their count
   cout <<"\n"<< "The Distinct characters of the updated string: ";
   for (auto p : charCount) {
      cout << p.first << " (" << p.second << ")\t ";
   }
   cout << endl;
   return 0;
}

Output

The Distinct characters of the string: l(1)	 o(2)	 h(1)	 c(1)	 s(1)	 
*********:After Updation:*********
The Distinct characters of the updated string: l (0)	 o (2)	 h (1)	 p (1)	 c (1)	 s (1)	

Conclusion

We have explored the concept of string range queries to count the number of distinct characters with updates. We saw in the output that it counts the character of the string in reverse order by doing distinction. For example- Many times we have shirts of two same colors but we wear 1 of them and this shows a real-life example of distinction.

Updated on: 10-May-2023

224 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements