1.问题
You are given the strings key and message, which represent a cipher key and a secret message, respectively. The steps to decode message are as follows:
Use the first appearance of all 26 lowercase English letters in key as the order of the substitution table.
Align the substitution table with the regular English alphabet.
Each letter in message is then substituted using the table.
Spaces ’ ’ are transformed to themselves.
- For example, given key = “happy boy” (actual key would have at least one instance of each letter in the alphabet), we have the partial substitution table of (‘h’ -> ‘a’, ‘a’ -> ‘b’, ‘p’ -> ‘c’, ‘y’ -> ‘d’, ‘b’ -> ‘e’, ‘o’ -> ‘f’).
Return the decoded message.
**Example 1:
Input: key = “the quick brown fox jumps over the lazy dog”, message = “vkbs bs t suepuv”
Output: “this is a secret”
Explanation: The diagram above shows the substitution table.
It is obtained by taking the first appearance of each letter in “the quick brown fox jumps over the lazy dog”.
Example 2:
Input: key = “eljuxhpwnyrdgtqkviszcfmabo”, message = “zwx hnfx lqantp mnoeius ycgk vcnjrdb”
Output: “the five boxing wizards jump quickly”
Explanation: The diagram above shows the substitution table.
It is obtained by taking the first appearance of each letter in “eljuxhpwnyrdgtqkviszcfmabo”.
Constraints:
- 26 <= key.length <= 2000
- key consists of lowercase English letters and ’ '.
- key contains every letter in the English alphabet (‘a’ to ‘z’) at least once.
- 1 <= message.length <= 2000
- message consists of lowercase English letters and ’ '.**
2.解题思路
方法1:
未使用hashmap的方式
1.新建返回值变量result,newk为key中26个未重复的字母,alp为26个字母
2.去掉key中的空格
3.遍历26个字母
4.去掉key中重复的元素,最后剩下26个字母,newk和alp的长度相等
5.遍历message的字符,如果和newk中的元素相等,在结果result中添加alp指定索引的元素,如果message的字符有空格,添加空格
6.返回result
方法2:
1.使用 String Builder 附加字符串
2.删除空格
3.将键映射到哈希表中,键的所有字母都将映射到它们各自的原始字母。利用map.containsKey()去掉重复的key
4.遍历元素,map.containsKey()去掉重复的message中的元素
如果message包含key的元素,result取出map的key对应的值
如果对于message字母以外的字符,空格添加到result中
5.返回ans的String类型
3.代码
代码1:
class Solution {
public String decodeMessage(String key, String message) {
String result ="";//1.新建返回值变量result,newk为key中26个未重复的字母,alp为26个字母
String newk ="";
String alp="";
key = key.replace(" ", "");//2.去掉key中的空格
for(char c='a';c<='z';++c){//3.遍历26个字母
alp +=c;
}
for(int i =0;i<key.length();i++){//4.去掉key中重复的元素,最后剩下26个字母,newk和alp的长度相等
//如果是用hashmap,hashmap不允许重复的键,利用这方法去掉键保存到map中
if(newk.indexOf(key.charAt(i))<0){
newk +=key.charAt(i);
}
}
for(int z=0;z<message.length();z++){//5.遍历message的字符,如果和newk中的元素相等,在结果result中添加alp指定索引的元素,如果message的字符有空格,添加空格
for(int e =0;e<newk.length();e++){
if(message.charAt(z)==newk.charAt(e)){
result += alp.charAt(e) ;
}
}
if(message.charAt(z)==' '){
result +=' ';
}
}
return result;//6.返回result
}
}
代码2:
class Solution {
public String decodeMessage(String key, String message) {
StringBuilder result = new StringBuilder();//1.使用 String Builder 附加字符串
key = key.replaceAll(" ", "");//2.删除空格
HashMap<Character,Character> map = new HashMap<>();
//3.将键映射到哈希表中,键的所有字母都将映射到它们各自的原始字母。利用map.containsKey()去掉重复的key
char alp = 'a';
for (int i = 0; i < key.length() ; i++) {
if (!map.containsKey(key.charAt(i))){
map.put(key.charAt(i),alp++);
}
}
//4.遍历元素,map.containsKey()去掉重复的message中的元素
for (int i = 0; i < message.length(); i++) {
if (map.containsKey(message.charAt(i))){
//.如果message包含key的元素,result取出map的key对应的值,
result.append(map.get(message.charAt(i)));
}else{
result.append(message.charAt(i));
//.对于message字母以外的字符,空格添加到result中
}
}
return result.toString();//5.返回result的String类型
}
}
和代码2的解题思路相同
class Solution {
public String decodeMessage(String key, String message) {
Map<Character, Character> map = new TreeMap<Character, Character>();
char alp = 'a';
for (char c : key.replaceAll(" ", "").toCharArray())
if (!map.containsKey(c)) {
map.put(c, alp);
alp++;
}
StringBuilder result = new StringBuilder();
for (char c : message.toCharArray()) {
if (!Character.isWhitespace(c)) {
result.append(map.get(c));
} else {
result.append(c);
}
}
return result.toString();
}
}
代码3:
class Solution {
public String decodeMessage(String key, String message) {
Map<Character, Character> map = new HashMap<>();
int alp = 97;//利用a的char的int为97,在map的value的值++,最后转换char格式
for(char c : key.toCharArray()){//遍历key的值
if(c == ' '){//如果为key不进行运算
continue;
}
else{
if(!map.containsKey(c)){//如果为key放入map中,利用map.containsKey()去重复的key
map.put(c, (char)alp);//在map的value的值++,最后转换char格式
alp++;
}
}
}
StringBuilder result = new StringBuilder();//使用 String Builder 附加字符串
for(char c1 : message.toCharArray()){//
if(c1 != ' '){
result.append(map.get(c1));//如果message不为" ",添加元素,取出map的key对应的值
}
else{
result.append(" ");//如果message为" ",添加" "
}
}
return result.toString();//返回String类型
}
}
和代码3的解题思路相同
class Solution {
public String decodeMessage(String key, String message) {
HashMap<Character,Character> map = new HashMap<>();
char alp = 'a';
for(int i = 0 ; i < key.length() ; i ++){
if(key.charAt(i) == ' '){
continue;
}
if(!map.containsKey(key.charAt(i))){
map.put(key.charAt(i),alp++);
}
}
String result = "";
for(int i = 0; i < message.length(); i++){
if(message.charAt(i) == ' '){
result = result + " ";
}else{
result = result + map.get(message.charAt(i));
}
}
return result;
}
}
}