iT邦幫忙

第 12 屆 iThome 鐵人賽

DAY 27
1
自我挑戰組

今年我想陪著 30 天系列 第 27

今年我想陪著 30 天之 27

  • 分享至 

  • xImage
  •  

1309. Decrypt String from Alphabet to Integer Mapping

Given a string s formed by digits ('0' - '9') and '#' . We want to map s to English lowercase characters as follows:
Characters ('a' to 'i') are represented by ('1' to '9') respectively.
Characters ('j' to 'z') are represented by ('10#' to '26#') respectively.
Return the string formed after mapping.
It's guaranteed that a unique mapping will always exist.

  • Example 1:
    Input: s = "10#11#12"
    Output: "jkab"
    Explanation: "j" -> "10#" , "k" -> "11#" , "a" -> "1" , "b" -> "2".

  • Example 2:
    Input: s = "1326#"
    Output: "acz"

  • Example 3:
    Input: s = "25#"
    Output: "y"

var freqAlphabets = function(s) {
  const map = { "1":"a", "2":"b", "3":"c", "4":"d", "5":"e",
				"6":"f", "7":"g", "8":"h", "9":"i", "10":"j", 
				"11":"k", "12":"l", "13":"m", "14":"n", "15":"o",
				"16":"p", "17":"q", "18":"r", "19":"s", "20":"t",
				"21":"u", "22":"v", "23":"w", "24":"x", "25":"y",
				"26":"z" };
  let result = '';
  for (let i = 0; i < s.length; ++i) {
    result += map[s[i + 2] === '#' ? (i += 2, s[i - 2] + s[i - 1]) : s[i]];
  }
  return result;
};

上一篇
今年我想陪著 30 天之 26
下一篇
今年我想陪著 30 天之 28
系列文
今年我想陪著 30 天30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言