Given an original message, determine the cipher that will produce the encoded st
ID: 3796313 • Letter: G
Question
Given an original message, determine the cipher that will produce the encoded string that comes earliest alphabetically. Return this encoded string. In the example below, the second cipher produces the alphabetically earliest encoded string ("abccd").
For example, if John's message is "hello" and his cipher maps 'h' to 'd', 'e' to 'i', 'l' to 'p' and 'o' to 'y', the encoded message will be "dippy". If the cipher maps 'h' to 'a', 'e' to 'b', 'l' to 'c' and 'o' to 'd', then the encoded message will be "abccd".
My code (bolded code cannot be changed):
string encrypt(string message){
// you write code here
string unEncryp;
for(int i = 0; i < message.length()){
char temp = message[i];
switch(temp){
default:
break;
}
}
return unEncryp;
}
Explanation / Answer
public String encrypt(String message)
{
char s = 'a';
StringBuilder sb = new StringBuilder();
Map<Character, Character> map = new HashMap<Character, Character>(); // HashMap
for(Character c : message.toCharArray())
{
if(!map.containsKey(c)) // It checks whether map contains a mapping for c
{
sb.append(s);
map.put(c,s); // It stores alphabets for that character(storing the cipher value for each key)
++s; // alphabet is incremented
}
else
sb.append(map.get(c));
}
return sb.toString();
}
It is better to use HashMap than going for switch case. HashMap contains values based on keys.
containskey()function will return true if map contains a mapping for the specified key.
In this program,in the if condition we are checking whether it contains a mapping or not. If it is not containing a mapping then we are creating a map.
put() method stores the Value-key pairs.
the key ( alphabet) is incremented.
If the map is already having a key for that value then it is retrived and appended.
Finally the cipher text is returned.
public String encrypt(String message)
{
char s = 'a';
StringBuilder sb = new StringBuilder();
Map<Character, Character> map = new HashMap<Character, Character>(); // HashMap
for(Character c : message.toCharArray())
{
if(!map.containsKey(c)) // It checks whether map contains a mapping for c
{
sb.append(s);
map.put(c,s); // It stores alphabets for that character(storing the cipher value for each key)
++s; // alphabet is incremented
}
else
sb.append(map.get(c));
}
return sb.toString();
}
It is better to use HashMap than going for switch case. HashMap contains values based on keys.
containskey()function will return true if map contains a mapping for the specified key.
In this program,in the if condition we are checking whether it contains a mapping or not. If it is not containing a mapping then we are creating a map.
put() method stores the Value-key pairs.
the key ( alphabet) is incremented.
If the map is already having a key for that value then it is retrived and appended.
Finally the cipher text is returned.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.