Find Characters Which When Increased by K are Present in String


In this problem, we will find all unique characters of the string with their first index, which is present in the string, after incrementing all characters of the given string by K.

For a problem solution, we can take each unique character of the given string. Next, we can update each character individually and check whether the updated character is present in the string and not updated to another character to get the answer.

Problem statement − We have given a string alpha and positive integer K. We need to increment each character’s ASCII value of the given string by K and check whether an updated character is present in the string. We need to print the valid unique character with the first index.

Sample examples

Input

alpha = "absstdrc"; K = 2;

Output

0 a

1 b

6 r

Explanation − Let’s update each character individually to get the answer.

  • ‘a’ + 2 −> ‘c’ = The ‘c’ is present in the string and not updated yet.

  • ‘b’ + 2 −> ‘d’ = The ‘d’ is present in the string and not updated yet.

  • ‘b’ + 2 −> ‘d’ = The ‘d’ is present in the string and not updated yet.

  • ‘t’ + 2 −> ‘v’ = The ‘v’ is not in the string.

  • ‘d’ + 2 −> ‘f’ = The ‘f’ is absent in the string.

  • ‘r’ + 2 −> ‘t’ = The ‘t’ is not updated to ‘v’. So, we can consider the ‘r’ as an output.

  • ‘c’ + 2 = ‘e’ = The ‘e’ is not in the given string.

Input

alpha = "abcdefg"; K = 0;

Output

0 a

1 b

2 c

3 d

4 e

5 f

6 g

Explanation − All characters will be presented after updating as we updated it by 0.

Input

alpha = "pqrpqrt"; K = 2;

Output

0 p 

Explanation − We can take only ‘p’, as ‘p’ + 2 = ‘r’. After that, we can’t update the ‘r’. Also, ‘q’ + 2 is ‘s’. So, we can’t consider the ‘q’ in the output.

Approach 1

In this approach, we will store the unique character of the given string in the set. After that, we will traverse each character of the string, increment the character by K, and check whether the updated character is present in the string and has not previously visited the original and updated character. If all conditions are true, we can include the original character with the index in the output.

Algorithm

Step 1 − Create a ‘pairSet’ set of characters.

Step 2 − Traverse the string, and insert each string character in the set.

Step 3 − Create a list to store the pair of index and character. Also, create the visited[] of length 26 to track whether or not the particular character was visited previously.

Step 4 − Start traversing the string, and take the updated character value in the ‘res’ variable after adding K to the ASCII value of the original character.

Step 5 − If the ‘res’ value is between ‘a’ and ‘z’, follow the steps below. Otherwise, move to the next iteration.

Step 6 − If the ‘res’ character is present in the map, the ‘res’ character is not visited previously, and alpha[p] is also not visited, update the visited[] array for the original and updated character, and insert the pair containing the index p and alpha[p] to the list.

Step 7 − Return the ‘list’ at the end.

Example

#include <bits/stdc++.h>
using namespace std;

vector<pair<int, char>> getCharacters(int k, string alpha) {
    set<char> pairSet;
    // Add unique characters in the set
    for (int p = 0; p < alpha.size(); p++) {
        pairSet.insert(alpha[p]);
    }
    // To store final answer
    vector<pair<int, char>> list;
    // To store visited characters
    vector<int> visited(26, 0);
    // Traverse the string
    for (int p = 0; p < alpha.size(); p++) {
        // Get updated character
        char res = char((int(alpha[p]) + k));
        // If the updated character is an alphabetical character
        if (res >= 'a' && res <= 'z') {
            // When the character is present in the set and not visited
            if (pairSet.find(res) != pairSet.end() && !visited[res - 'a'] && !visited[alpha[p] - 'a']) {
                visited[res - 'a'] = visited[alpha[p] - 'a'] = 1;
                // Push index and character in the list
                list.push_back(make_pair(p, alpha[p]));
            }
        }
    }
    return list;
}
int main() {
    string alpha = "absstdrc";
    int K = 2;
    vector<pair<int, char>> res = getCharacters(K, alpha);
    cout << "The characters which are present in the string after incrementing its ASCII values by K are - " << endl;
    for (auto pair : res)
        cout << pair.first << " " << pair.second << endl;
    return 0;
}

Output

The characters which are present in the string after incrementing its ASCII values by K are − 
0 a
1 b
6 r

Time complexity − O(N) to insert the character in the set.

Space complexity − O(1) as we always need to use the set of size 26.

We learned to find all string characters that exist in the given string after incrementing it by K. Programmers may try to solve the problem required to find unique characters present in the string after decrementing the K.

Updated on: 17-Jul-2023

39 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements