对于一个长度为 n 字符串,我们需要对它做一些变形。
首先这个字符串中包含着一些空格,就像"Hello World"一样,然后我们要做的是把这个字符串中由空格隔开的单词反序,同时反转每个字符的大小写。
比如"Hello World"变形后就变成了"wORLD hELLO"。
数据范围:
, 字符串中包括大写英文字母、小写英文字母、空格。
进阶:空间复杂度
, 时间复杂度
给定一个字符串s以及它的长度n(1 ≤ n ≤ 10^6)
请返回变形后的字符串。题目保证给定的字符串均由大小写字母和空格构成。
"This is a sample",16
"SAMPLE A IS tHIS"
"nowcoder",8
"NOWCODER"
"iOS",3
"Ios"
export function trans(s, n) { // write code here let result = '' let tmpStr = '' for (let i =0;i<n;i++) { const item = s[i] if (/[A-Z]/.test(item)) { tmpStr += item.toLowerCase() } else if (/[a-z]/.test(item)) { tmpStr += item.toUpperCase() } else { result = item + tmpStr + result tmpStr = '' } } return tmpStr + result }
function trans(s, n) { //write code here return s.split(" ") .reverse() .map((item) => { return [...item].map((char) => char == char.toLowerCase() ? char.toUpperCase() : char.toLowerCase() ).join(''); }).join(' '); }