Find characters which when increased by K are present in String
Last Updated :
03 Nov, 2022
Given a string s of lowercase English alphabets and integer K. the task is to check if each character after increasing their ASCII by K is present in the string or not. Return only unique characters and the first index where they are present.
Examples:
Input: s = “dszepvaxuo”, k = 3
Output: {{1, ‘s’}, {6, ‘a’}, {8, ‘u’}}
Explanation: Moving each character 3 steps gives:
‘d’ -> ‘g’, ‘g’ is not present in s
‘s’ -> ‘v’, ‘v’ is present in s, thus, indices array becomes{{1, ‘s’}}
‘z’ -> ‘c’, ‘c’ is not present in s
‘e’ -> ‘h’, ‘h’ is not present in s
‘p’ -> ‘s’, ‘s’ is present in s, but is already present in indices array
‘v’ -> ‘y’, ‘y’ is not present in s
‘a’ -> ‘d’, ‘d’ is present in s, thus, indices array becomes {{1, ‘s’}. {6, ‘a’}}
‘x’ -> ‘a’, ‘a’ is present in s, but is already present in indices array
‘u’ -> ‘x’, ‘x’ is present in s, thus, indices array becomes {{1, ‘s’}, {6, ‘a’}, {8, ‘u’}}
‘o’ -> ‘r’, ‘r’ is not present in s
Thus, final indices array is {{1, ‘s’}, {6, ‘a’}, {8, ‘u’}}
Input: s = “abcdefg”, k = 2
Output: {{0, ‘a’}, {1, ‘b’}, {4, ‘e’}}
Approach: The approach to solve the problem is as follows:
Store each character in a data structure (like set) for easy look up. Iterate over the string, check for each character if it satisfies the condition and is and add valid character’s indices to the output array if that character is not considered already.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
vector<pair< int , char > > characters_after_k_steps( int k,
string s)
{
set< char > st;
for ( int i = 0; i < s.size(); i++) {
st.insert(s[i]);
}
vector<pair< int , char > > vec;
vector< int > vis(26, 0);
for ( int i = 0; i < s.size(); i++) {
char ans = char (( int (s[i]) + k));
if (ans >= 'a' && ans <= 'z' ) {
if (st.find(ans) != st.end() && !vis[ans - 'a' ]
&& !vis[s[i] - 'a' ]) {
vis[ans - 'a' ] = vis[s[i] - 'a' ] = 1;
vec.push_back(make_pair(i, s[i]));
}
}
}
return vec;
}
int main()
{
string s = "dszepvaxuo" ;
int K = 3;
vector<pair< int , char > > ans
= characters_after_k_steps(K, s);
for ( auto it : ans)
cout << it.first << " " << it.second << endl;
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG {
static class pair {
int first;
char second;
public pair( int first, char second)
{
this .first = first;
this .second = second;
}
}
static List<pair> characters_after_k_steps( int k,
String s)
{
Set<Character> st = new HashSet<>();
for ( int i = 0 ; i < s.length(); i++) {
st.add(s.charAt(i));
}
List<pair> vec = new ArrayList<>();
boolean [] vis = new boolean [ 26 ];
for ( int i = 0 ; i < s.length(); i++) {
char ans = ( char )(( int )s.charAt(i) + k);
if (ans >= 'a' && ans <= 'z' ) {
if (st.contains(ans) && !vis[ans - 'a' ]
&& !vis[s.charAt(i) - 'a' ]) {
vis[ans - 'a' ] = vis[s.charAt(i) - 'a' ]
= true ;
vec.add( new pair(i, s.charAt(i)));
}
}
}
return vec;
}
public static void main(String[] args)
{
String s = "dszepvaxuo" ;
int K = 3 ;
List<pair> ans = characters_after_k_steps(K, s);
for ( int i = 0 ; i < ans.size(); i++) {
pair it = (pair)ans.get(i);
System.out.println(it.first + " " + it.second);
}
}
}
|
Python3
def characters_after_k_steps(k, s):
st = set ()
for i in s:
st.add(i)
arr = []
vis = [ 0 for i in range ( 26 )]
for i, x in enumerate (s):
ans = chr ( ord (x) + k)
if ans > = 'a' and ans < = 'z' :
a = ord (ans) - ord ( 'a' )
b = ord (x) - ord ( 'a' )
if (ans in st) and vis[a] = = 0 and vis[b] = = 0 :
vis[a] = 1
vis[b] = 1
arr.append([i, x])
return arr
if __name__ = = "__main__" :
s = "dszepvaxuo"
k = 3
ans = characters_after_k_steps(k, s)
for i in ans:
print (i[ 0 ], i[ 1 ])
|
C#
using System;
using System.Collections.Generic;
public class GFG {
class pair {
public int first;
public char second;
public pair( int first, char second)
{
this .first = first;
this .second = second;
}
}
static List<pair> characters_after_k_steps( int k,
String s)
{
HashSet< char > st = new HashSet< char >();
for ( int i = 0; i < s.Length; i++) {
st.Add(s[i]);
}
List<pair> vec = new List<pair>();
bool [] vis = new bool [26];
for ( int i = 0; i < s.Length; i++) {
char ans = ( char )(( int )s[i] + k);
if (ans >= 'a' && ans <= 'z' ) {
if (st.Contains(ans) && !vis[ans - 'a' ]
&& !vis[s[i] - 'a' ]) {
vis[ans - 'a' ] = vis[s[i] - 'a' ]
= true ;
vec.Add( new pair(i, s[i]));
}
}
}
return vec;
}
public static void Main(String[] args)
{
String s = "dszepvaxuo" ;
int K = 3;
List<pair> ans = characters_after_k_steps(K, s);
for ( int i = 0; i < ans.Count; i++) {
pair it = (pair)ans[i];
Console.WriteLine(it.first + " " + it.second);
}
}
}
|
Javascript
<script>
function characters_after_k_steps(k, s)
{
let st = new Set();
for (let i = 0; i < s.length; i++) {
st.add(s[i]);
}
let vec = [];
let vis = new Array(26).fill(0);
for (let i = 0; i < s.length; i++) {
let ans = String.fromCharCode(s[i].charCodeAt(0) + k);
if (ans >= 'a' && ans <= 'z' ) {
if (st.has(ans) && !vis[ans.charCodeAt(0) - 'a' .charCodeAt(0)]
&& !vis[s[i].charCodeAt(0) - 'a' .charCodeAt(0)]) {
vis[ans.charCodeAt(0) - 'a' .charCodeAt(0)] = vis[s[i].charCodeAt(0) - 'a' .charCodeAt(0)] = 1;
vec.push([i, s[i]]);
}
}
}
return vec;
}
let s = "dszepvaxuo" ;
let K = 3;
let ans = characters_after_k_steps(K, s);
for (let it of ans)
document.write(it[0] + " " + it[1] + "<br>" );
</script>
|
Time complexity: O(N * log(N))
Auxiliary Space: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...