Problem Statement John is obsessed with security He is writi

Problem Statement

John is obsessed with security. He is writing a letter to his friend Brus and he wants nobody else to be able to read it. He uses a simple substitution cipher to encode his message. Each letter in the message is replaced with its corresponding letter in a substitution alphabet. A substitution alphabet is a permutation of all the letters in the original alphabet. In this problem, the alphabet will consist of only lowercase letters (\'a\'-\'z\').

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\".

Given the original message, determine the cipher that will produce the encoded string that comes earliest alphabetically. Return this encoded string. In the example above, the second cipher produces the alphabetically earliest encoded string (\"abccd\").

Notes and Constraints

- If A and B are two strings of the same length, then A comes earlier alphabetically than B if it contains a smaller character at the first position where the strings differ.

- message will contain between 1 and 50 characters, inclusive.

- message will contain only lowercase letters (\'a\'-\'z\').

Starter code (Bolded code CANNOT be changed, and all work must be done within the bolded code):

string encrypt(string message){
// you write code here
  
  
}

Solution

Here is the code for the given scenario:

import java.util.*;

import java.util.regex.*;

import java.text.*;

import java.math.*;

public class TheEncryptionDivTwo

{

public String encrypt(String message)

{

                char s = \'a\';

                StringBuilder sb = new StringBuilder();

                Map<Character, Character> map = new HashMap<Character, Character>();

                for(Character c : message.toCharArray()){

                if(!map.containsKey(c)){

                                sb.append(s);

                                map.put(c,s);

                                ++s;

                }

                else

                                sb.append(map.get(c));

                }

                return sb.toString();

}

<%:testing-code%>

}

Problem Statement John is obsessed with security. He is writing a letter to his friend Brus and he wants nobody else to be able to read it. He uses a simple sub
Problem Statement John is obsessed with security. He is writing a letter to his friend Brus and he wants nobody else to be able to read it. He uses a simple sub

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site