 
  Data Structure Data Structure
 Networking Networking
 RDBMS RDBMS
 Operating System Operating System
 Java Java
 MS Excel MS Excel
 iOS iOS
 HTML HTML
 CSS CSS
 Android Android
 Python Python
 C Programming C Programming
 C++ C++
 C# C#
 MongoDB MongoDB
 MySQL MySQL
 Javascript Javascript
 PHP PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Queries to find the last non-repeating character in the sub-string of a given string in C++
In this problem, we are given string str, and Q queries, each consisting of two integers. Our task is to create the program to solve Queries to find the last non-repeating character in the sub-string of a given string in C++.
Problem Description
In each query, we have two integers L and R. To solve the queries, we will take a substring starting from index L to index R. And find the last character which is non-repeating in the sub-string.
Let’s take an example to understand the problem,
Input: str = “Tutorialspoint” Q = 2
query = {{4, 8}, {2, 6}}
Output: -1 , -1
Explanation
subStr[4...8] = “rials”. The last non-repeating character is s. But all characters have frequency 1.
subStr[2...6] = “toria”. The last non-repeating character is a. But all characters have frequency 1.
Solution Approach
To solve the problem, we need to find the character with a single frequency of occurrence. For this, an easy and simple approach is to create a matrix to calculate the charFreq[][]. To solve the subarray query, we will check for the frequency of occurrence of all characters and return the last character with frequency 1.
Example
#include<bits/stdc++.h> using namespace std; int charFreq[256][1000] = {0}; void initialiseCharFrequency(string str, int n) {    charFreq[(int)str[0]][0] = 1;    for (int i = 1; i < n; i++) {       char ch = str[i];       for (int j = 0; j < 256; j++) {          char charToUpdate = (char)j;          if (charToUpdate == ch)             charFreq[j][i] = charFreq[j][i - 1] + 1;          else             charFreq[j][i] = charFreq[j][i - 1];       }    } } string returnCharFromString(char x) {    string s(1, x);    return s; } string lastUniqueChar(string str, int n, int start, int end) {    for (int i = end; i >= start; i--) {       char ch = str[i];    if ((charFreq[(int)ch][end] - charFreq[(int)ch][start - 1]) ==1)       return returnCharFromString(ch);    }    return "-1"; } int main() {    string str = "TutorialsPoint";    int len = str.length();    int Q = 3;    int query[Q][2] = { { 2, 9 }, { 2, 3 }, { 0, 12 } };    initialiseCharFrequency(str, len);    for (int i = 0; i < Q; i++)       cout<<"\nFor Query "<<(i+1)<<": The last non-repeating character in the sub-string of a given string is "<<lastUniqueChar(str, len,query[i][0], query[i][1]); } Output
For Query 1: The last non-repeating character in the sub-string of a given string is P For Query 2: The last non-repeating character in the sub-string of a given string is o For Query 3: The last non-repeating character in the sub-string of a given string is n
