
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Queries for characters in a repeated string in C++
In this problem, we are given a string str and Q queries consisting of two values a and b. Our task is to create a program to solve Queries for characters in a repeated string in C++.
Problem Description
To solve each query, we need to check whether the characters at index a and b are the same and return the value accordingly.
Let’s take an example to understand the problem,
Input: str = “tutorialspoint”
Q = 2
Query = {{0, 2}, {4, 7}}
Output:Repeated
Not Repeated
Explanation
For Query 1, the character at index 0 is t, and the character at index 2 is t. Both are the same characters.
For Query 1, the character at index 4 is r, and the character at index 7 is l. Both are not the same characters.
Solution Approach
To solve the problem, we will simply check for the equivalence of the elements at index a and b. If any index is greater than the length of string, be will find the value of (index%len), to get the character’s index. And the user the new index for comparison.
Example
#include <iostream> #include <string> using namespace std; bool isrepeated(string str, int len, int a, int b){ if(a > len) a %= len; if(b > len) b %= len; if(str[a] == str[b]) return true; else return false; } int main(){ string str = "tutorialspoint"; int len = str.length(); int Q = 3; int query[Q][2] = {{0, 2}, {3, 32}, {5, 18}}; for(int i = 0; i < Q; i++){ if(isrepeated(str, len, query[i][0], query[i][1])) cout<<"Character is repeated in both the index values"<<endl; else cout<<"Character is not repeated in both the index values"<<endl; } return 0; }
Output
Character is repeated in both the index values Character is not repeated in both the index values Character is not repeated in both the index values
- Related Articles
- Queries for frequencies of characters in substrings in C++
- Python Program to Capitalize repeated characters in a string
- How to count the number of repeated characters in a Golang String?
- Find repeated character present first in a string in C++
- Find the first repeated word in a string in C++
- Count occurrences of a character in a repeated string in C++
- Swapping Characters of a String in C#
- Find the first repeated character in a string using C++.
- How to find the number of occurrences of unique and repeated characters in a string vector in R?
- A shorthand array notation in C for repeated values?
- How to look for partial string matches in queries in PostgreSQL?
- C++ Queries for DFS of a Subtree in a Tree
- How to scan a string for specific characters in Python?
- How to return a string repeated N number of times in C#?
- Queries for number of distinct elements in a subarray in C++
