Here a new problem!
Here the 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 ‘q’, ‘e’ to ‘w’, ‘l’ to ‘e’ and ‘o’ to ‘r’, the encoded message will be “qweer”. 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”).
Here my code:
using System;
using System.Collections.Generic;
public class TheEncryptionDivTwo
{
public string encrypt(string message)
{
string[] abc = { “a”, “b”, “c”, “d”, “e”, “f”, “g”, “h”, “i”, “j”, “k”, “l”, “m”, “n”, “o”, “p”, “q”, “r”, “s”, “t”, “u”, “v”, “w”, “x”, “y”, “z”};
List<string> lista = new List<string>();
int x = 1;
for (int i = 0; i < message.Length; i++)
{
if (lista.Count == 0)
{
lista.Add(message[i].ToString());
message = message.Insert(i, abc[i]);
message = message.Remove(i + 1, 1);
}
else
{
bool y = false;
int o = 0;
for (int a = 0; a < lista.Count; a++)
{
if (message[i].ToString() == lista[a])
{
o = a;
y = true;
break;
}
}
if (y == true)
{
message = message.Insert(i, abc[o]);
message = message.Remove(i + 1, 1);
}
else
{
lista.Add(message[i].ToString());
message = message.Insert(i, abc[x]);
message = message.Remove(i + 1, 1);
x++;
}
}
}
return message;
}
}
Here an image with the solution passing all the system test:
If you have another approach, share with me in a comment!
Thank you very much


not as efficient but looks a bit simpler, essentially inner joining substitute alpha / distinct list of the chars in message, back onto the message
private string Enc(string message)
{
if (string.IsNullOrEmpty(message))
{
return “intput message cannot be empty”;
}
Dictionary subList = new Dictionary();
List distinctSubChars = message.Distinct().ToList();
int length = distinctSubChars.Count;
Enumerable.Range(97, 26).ToList().ForEach(
x => subList.Add(
(char)x,
(x – 97) > length -1 ? ’0′ : distinctSubChars[x - 97]
));
var alphaCipher = (from c in message
join subC in subList on c equals subC.Value
where subC.Value != ’0′
select subC.Key
).ToArray();
return new string(alphaCipher);
}
Hi dvr!
Thank you for your new solution.. Do you use LINQ right?
I am trying to run this code, and i couldn´t, i will check it.
Are you sure that this solution makes exactly what the problem statement ask for?
Btw, I saw this code harder to understand than mine.. at least for me, i think is because i know a little about LINQ, but, thanks a lot for your code!
Are you using 2010 or 2012, I’ll email you the project, I ran asserts against you first 4 tests
Hi Dvr, i am using 2012, but if you have 2010 version, i have 2010 too. Then dont worry, send it, i will check.
Send to my mail: oscarbralo@gmail.com, thanks