Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jianminchen/84d9964edd7fd64bcd010fe26976e839 to your computer and use it in GitHub Desktop.
Save jianminchen/84d9964edd7fd64bcd010fe26976e839 to your computer and use it in GitHub Desktop.
using System;
class Solution
{
public static string Decrypt(string word)
{
if(word == null || word.Length == 0)
return "";
var length = word.Length;
var source = new char[length];
var firstChar = word[0];
source[0] = firstChar == 'a' ? 'z' : (char)(word[0] - 1); // a -> z
for(int i = 1; i < length; i++)
{
//line 39 - (encrypt[i] - encrypt[i - 1]) + 26 * m -> a - z, for i >= 1
int diff = word[i] - word[i-1]; // 10 -> 10 + 26 * 4 = 114
// 111 - 110 + 26 * 4 = 105
while(diff < 'a')
{
diff += 26;
}
source[i] = (char)diff;
}
return new string(source);
}
static void Main(string[] args)
{
}
}
/*
c r i m e
99 114 105 109 101
step 2 100 100+114 224 + 105
214 319 <- encrypt[i] = encrypt[i - 1] + source[i], i >= 1 -- Formula 1
step 3: 100 214 -26 * x
d a - z
d n o t q
based on formula 1, I can imply that
source[i] = (char)(encrypt[i] - 1), i = 0
= (encrypt[i] - encrypt[i - 1]) + 26 * m -> a - z, for i >= 1
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment