Created
July 15, 2018 05:19
-
-
Save jianminchen/84d9964edd7fd64bcd010fe26976e839 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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