我正在创建一个方法,当给定替换代码时,它会返回可用于解码任何消息的替换代码
我的意思的一个例子在下面
English Alphabet = ABCDEFGHIJKLMNOPQRSTUVWXYZ
substitution = XVSHJQEMZKTUIGAPOYLRWDCFBN
Output I want = OYWVGXNDMEJSHZQPFTCKLBUARI
正如您在上面所看到的,替换中的">A"映射到英文字母表上的">
O",因此为什么输出">
O是第一个字母。"<<em>替换中的strong>B映射到英文字母表的'Y',因此它是第二个字母,所以是第四个。。。
我创建的代码
public static String getRev(String s)
{
char normalChar[]
= { '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' };
String revString = "";
for (int i = 0; i < s.length(); i++) {
for (int j = 0; j < 26; j++) {
if (s.indexOf(i) == normalChar[j])
{
revString += normalChar[j];
break;
}
}
}
return revString;
}
l
input = "XVSHJQEMZKTUIGAPOYLRWDCFBN"
Expected output = "OYWVGXNDMEJSHZQPFTCKLBUARI"
My output = "XVSHJQEMZKTUIGAPOYLRWDCFBN"
-
对于输入
"XVSHJQEMZKTUIGAPOYLRWDCFBN"
和相同的替换"XVSHJQEMZKTUIGAPOYLRWDCFBN"
;正常的";应返回字母表。 -
为了获得所提供的替换的
"OYWVGXNDMEJSHZQPFTCKLBUARI"
和作为输入的正常字母表,来自input
的字符索引位于substitution
中,该索引用于查找正常字母表中的字符:
public static String getRev(String s) {
String normal = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
String substitution = "XVSHJQEMZKTUIGAPOYLRWDCFBN";
StringBuilder sb = new StringBuilder();
for (char c : s.toCharArray()) {
int index = substitution.indexOf(c);
if (index >-1) {
sb.append(normal.charAt(index));
}
}
return sb.toString();
}
测试:
System.out.println(getRev("ABCDEFGHIJKLMNOPQRSTUVWXYZ")); // OYWVGXNDMEJSHZQPFTCKLBUARI
System.out.println(getRev("XVSHJQEMZKTUIGAPOYLRWDCFBN")); // ABCDEFGHIJKLMNOPQRSTUVWXYZ