Open In App

Queries for same characters in a repeated string

Last Updated : 07 Mar, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Given a string s, we create an infinitely repeating string t by appending s to itself multiple times.
We are given a 2D array queries[][], where each row contains two indices i and j. Or we can say, each row queries[k] contains two indices queries[k][0] = i and queries[k][1] = j.
For each query, we need to check if the characters at positions i and j in t are the same. If they are the same, print "Yes" for that query. Otherwise, print "No".

Examples:

Input: s = "geeksforgeeks", queries = [[0, 8], [8, 13], [6, 15]]
Output: [Yes, Yes, No]
Explanation: String t will be "geeksforgeeksgeeksforgeeks...."

  • For Query 1, index 0 and index 8 have same element i.e 'g'. So, the answer is Yes.
  • For Query 2, index 8 and index 13 have same element i.e 'g'. So, the answer is Yes.
  • For Query 3, index 6 = 'o' and index 15 = 'e' which are not same. So, the answer is No.

Input: s = "abc", queries = [[0, 1], [1, 4], [2, 5], [0, 2]]
Output: [No, Yes, Yes, No]
Explanation: String t will be "abcabcabcabc..."

  • For Query 1, Index 0 and index 1 have different elements 'a' and 'b', so the answer is No
  • For Query 2, Index 1 and index 4 have the same element 'b', so the answer is Yes
  • For Query 3, Index 2 and index 5 have the same element 'c', so the answer is Yes
  • For Query 4, Index 0 and index 2 have different elements 'a' and 'c', so the answer is No

Approach:

The idea is to leverage the repeating pattern in s. Let the length of s be n. Notice that characters at positions
0, n, 2n, 3n, ... are always the same. Similarly, for any index i, the characters at positions i, n+i, 2n+i, 3n+i, ... remain identical.

For each query, we compute i % n and j % n to map the given indices to their respective positions in s. If the characters at these mapped positions are the same in s, we return "Yes", otherwise "No"

Below is the implementation of the above approach:

C++
// C++ Code to check character equality in an infinitely
// repeating string using given queries
#include <bits/stdc++.h>
using namespace std;

// Function to check if characters at positions
// i and j are same
vector<string> checkQueries(string s, 
              vector<vector<int>> &queries) {
                  
    vector<string> result;
    int n = s.length();

    // Process each query
    for (auto &q : queries) {
        int i = q[0] % n;
        int j = q[1] % n;
        
        // Compare characters and store result
        if (s[i] == s[j]) {
            result.push_back("Yes");
        } else {
            result.push_back("No");
        }
    }
    return result;
}

// Function to print vector of strings
void printArr(vector<string> &arr) {
    for (const string &res : arr) {
        cout << res << " ";
    }
    cout << endl;
}

int main() {
    
    string s = "geeksforgeeks";
    vector<vector<int>> queries 
             = {{0, 8}, {8, 13}, {6, 15}};

    vector<string> result = checkQueries(s, queries);
    printArr(result);
    
    return 0;
}
Java
// Java Code to check character equality in an infinitely
// repeating string using given queries
import java.util.*;

class GfG {

    // Function to check if characters at positions
    // i and j are same
    static String[] checkQueries(String s, int[][] queries) {
        
        String[] result = new String[queries.length];
        int n = s.length();

        // Process each query
        for (int k = 0; k < queries.length; k++) {
            int i = queries[k][0] % n;
            int j = queries[k][1] % n;
            
            // Compare characters and store result
            if (s.charAt(i) == s.charAt(j)) {
                result[k] = "Yes";
            } else {
                result[k] = "No";
            }
        }
        return result;
    }

    // Function to print array of strings
    static void printArr(String[] arr) {
        for (String res : arr) {
            System.out.print(res + " ");
        }
        System.out.println();
    }

    public static void main(String[] args) {
        
        // Hardcoded input
        String s = "geeksforgeeks";
        int[][] queries = {{0, 8}, {8, 13}, {6, 15}};

        String[] result = checkQueries(s, queries);
        printArr(result);
    }
}
Python
# Python Code to check character equality in an infinitely
# repeating string using given queries

# Function to check if characters at positions
# i and j are same
def checkQueries(s, queries):
    
    result = []
    n = len(s)

    # Process each query
    for q in queries:
        i = q[0] % n
        j = q[1] % n
        
        # Compare characters and store result
        if s[i] == s[j]:
            result.append("Yes")
        else:
            result.append("No")
    
    return result

# Function to print list of strings
def printArr(arr):
    for res in arr:
        print(res, end=" ")
    print()

if __name__ == "__main__":
    
    # Hardcoded input
    s = "geeksforgeeks"
    queries = [[0, 8], [8, 13], [6, 15]]

    result = checkQueries(s, queries)
    printArr(result)
C#
// C# Code to check character equality in an infinitely
// repeating string using given queries
using System;

class GfG {

    // Function to check if characters at positions
    // i and j are same
    static string[] CheckQueries(string s, int[][] queries) {
        
        string[] result = new string[queries.Length];
        int n = s.Length;

        // Process each query
        for (int k = 0; k < queries.Length; k++) {
            int i = queries[k][0] % n;
            int j = queries[k][1] % n;
            
            // Compare characters and store result
            if (s[i] == s[j]) {
                result[k] = "Yes";
            } else {
                result[k] = "No";
            }
        }
        return result;
    }

    // Function to print array of strings
    static void PrintArr(string[] arr) {
        foreach (string res in arr) {
            Console.Write(res + " ");
        }
        Console.WriteLine();
    }

    public static void Main() {
        
        // Hardcoded input
        string s = "geeksforgeeks";
        int[][] queries = {
            new int[] {0, 8}, 
            new int[] {8, 13}, 
            new int[] {6, 15}
        };

        string[] result = CheckQueries(s, queries);
        PrintArr(result);
    }
}
JavaScript
// JavaScript Code to check character equality in an infinitely
// repeating string using given queries

// Function to check if characters at positions
// i and j are same
function checkQueries(s, queries) {
    
    let result = [];
    let n = s.length;

    // Process each query
    for (let q of queries) {
        let i = q[0] % n;
        let j = q[1] % n;
        
        // Compare characters and store result
        if (s[i] === s[j]) {
            result.push("Yes");
        } else {
            result.push("No");
        }
    }
    return result;
}

// Function to print array of strings
function printArr(arr) {
    console.log(arr.join(" "));
}

// Hardcoded input
let s = "geeksforgeeks";
let queries = [[0, 8], [8, 13], [6, 15]];

let result = checkQueries(s, queries);
printArr(result);

Output
Yes Yes No 

Time Complexity: O(n), where n is the number of queries.
Space Complexity: O(n), as we store results for each query.


Next Article
Article Tags :
Practice Tags :

Similar Reads