Given a 26 letter character set, which is equivalent to character set of English alphabet i.e. (abcd….xyz) and act as a relation. We are also given several sentences and we have to translate them with the help of given new character set.
Examples:
New character set : qwertyuiopasdfghjklzxcvbnm
Input : "utta"
Output : geek
Input : "egrt"
Output : code
Idea behind conversion of new character set is to use hashing. Perform hashing of new character set where element of set is index and its position will be new alphabet value.
Approach1:
Given New character set = "qwertyuiopasdfghjklzxcvbnm"
First character is q, during hashing we will place 'a' (for position )
at index q i.e. (17th).
After hashing our new character set is "kxvmcnophqrszyijadlegwbuft".
For input "egrt" =
hash[e -'a'] = c
hash[g -'a'] = o
hash[r -'a'] = d
hash[t -'a'] = e
For "egrt" is equivalent to "code".
Implementation:
C++
#include<bits/stdc++.h>
using namespace std;
void conversion( char charSet[], string &str)
{
int n = str.length();
char hashChar[26];
for ( int i = 0; i < 27; i++)
hashChar[charSet[i]- 'a' ] = 'a' + i;
for ( int i = 0; i < n; i++)
str[i] = hashChar[str[i]- 'a' ];
}
int main()
{
char charSet[27] = "qwertyuiopasdfghjklzxcvbnm" ;
string str = "egrt" ;
conversion(charSet, str);
cout << str;
return 0;
}
|
Java
class GFG {
static String conversion( char charSet[], String str) {
int n = str.length();
char hashChar[] = new char [ 26 ];
for ( int i = 0 ; i < 26 ; i++) {
int ch = Math.abs(charSet[i] - 'a' );
hashChar[ch] = ( char ) ( 'a' + i);
}
String s= "" ;
for ( int i = 0 ; i < n; i++) {
s += hashChar[str.charAt(i) - 'a' ];
}
return s;
}
public static void main(String[] args) {
char charSet[] = "qwertyuiopasdfghjklzxcvbnm" .toCharArray();
String str = "egrt" ;
str = conversion(charSet, str);
System.out.println(str);
}
}
|
Python3
from string import ascii_lowercase
def conversion(char_set: str , string: str ) - > str :
hash_char = dict ()
for alphabet, new_char in zip (ascii_lowercase, char_set):
hash_char[new_char] = alphabet
string2 = ""
for i in string:
string2 + = hash_char[i]
return string2
if __name__ = = "__main__" :
char_set = "qwertyuiopasdfghjklzxcvbnm"
string = "egrt"
print (conversion(char_set, string))
|
C#
using System;
class GFG
{
static String conversion( char []charSet,
String str)
{
int n = str.Length;
char []hashChar = new char [26];
for ( int i = 0; i < 26; i++)
{
int ch = Math.Abs(charSet[i] - 'a' );
hashChar[ch] = ( char ) ( 'a' + i);
}
String s = "" ;
for ( int i = 0; i < n; i++)
{
s += hashChar[str[i] - 'a' ];
}
return s;
}
public static void Main(String[] args)
{
char []charSet = "qwertyuiopasdfghjklzxcvbnm" .ToCharArray();
String str = "egrt" ;
str = conversion(charSet, str);
Console.WriteLine(str);
}
}
|
Javascript
<script>
function conversion(charSet, str)
{
var n = str.length;
var hashChar = Array(26);
for ( var i = 0; i < 26; i++)
{
var ch = Math.abs(charSet[i].charCodeAt(0)-
'a' .charCodeAt(0));
hashChar[ch] =
String.fromCharCode( 'a' .charCodeAt(0) + i);
}
var s = "" ;
for ( var i = 0; i < n; i++)
s += (hashChar[str[i].charCodeAt(0)- 'a' .charCodeAt(0)]);
return s;
}
var charSet = "qwertyuiopasdfghjklzxcvbnm" .split( '' );
var str = "egrt" ;
str = conversion(charSet, str);
document.write( str);
</script>
|
Time Complexity: O(N) where N is length of string.
Auxiliary Space: O(1)
Approach2:
- Initialize two strings, one with actual set of alphabets and another with modified one.
- Get the string to be converted from the user.
- Retrieve the first element of the string, find its index in the modified set of alphabets(eg:0 for ‘q’).
- Find the element of same index in the actual set of alphabets and concatenate it with the result string.
- Repeat the above steps for all the remaining elements of the input string.
- Return the result string.
C++
#include <bits/stdc++.h>
using namespace std;
string conversion(string charSet, string str)
{
string alphabets = "abcdefghijklmnopqrstuvwxyz" ;
string s2 = "" ;
for ( char i : str) {
s2 += alphabets[charSet.find_first_of(i)];
}
return s2;
}
int main()
{
string charSet = "qwertyuiopasdfghjklzxcvbnm" ;
string str = "egrt" ;
cout << conversion(charSet, str);
return 0;
}
|
Java
class GFG
{
static char [] alphabets = "abcdefghijklmnopqrstuvwxyz" .toCharArray();
static String conversion(String charSet, char [] str1)
{
String s2 = "" ;
for ( char i : str1)
s2 += alphabets[charSet.indexOf(i)];
return s2;
}
public static void main(String[] args)
{
String charSet = "qwertyuiopasdfghjklzxcvbnm" ;
String str1 = "egrt" ;
System.out.print(conversion(charSet, str1.toCharArray()));
}
}
|
Python3
def conversion(charSet,str1):
s2 = ""
for i in str1:
s2 + = alphabets[charSet.index(i)]
return s2
if __name__ = = '__main__' :
alphabets = "abcdefghijklmnopqrstuvwxyz"
charSet = "qwertyuiopasdfghjklzxcvbnm"
str1 = "egrt"
print (conversion(charSet,str1))
|
C#
using System;
class GFG
{
static char [] alphabets = "abcdefghijklmnopqrstuvwxyz" .ToCharArray();
static String conversion(String charSet, char [] str1)
{
String s2 = "" ;
foreach ( char i in str1)
s2 += alphabets[charSet.IndexOf(i)];
return s2;
}
public static void Main(String[] args)
{
String charSet = "qwertyuiopasdfghjklzxcvbnm" ;
String str1 = "egrt" ;
Console.Write(conversion(charSet, str1.ToCharArray()));
}
}
|
Javascript
<script>
var alphabets = "abcdefghijklmnopqrstuvwxyz" .split( "" );
function conversion(charSet, str1) {
var s2 = "" ;
str1.forEach((i) => {
s2 = s2 + alphabets[charSet.indexOf(i)];
});
return s2;
}
var charSet = "qwertyuiopasdfghjklzxcvbnm" ;
var str1 = "egrt" ;
document.write(conversion(charSet, str1.split( "" )));
</script>
|
Time Complexity: O(N) where N is length of string.
Auxiliary Space: O(1).
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
25 Jul, 2022
Like Article
Save Article