题目:编码实现求给定字符串(全为小写英文字母)的最小后继,如“abc”的最小后继为 “abd”,“dhz”的最小后继为“dj”。
方法:
function MinNextStr(s) {
var l, e;
if (typeof s !== 'string') {
return s;
} else {
s = s.toLowerCase();
}
for (l = s.length ; l--; ) {
e = s.charCodeAt(l);
if (e !== 122) {
break;
}
}
if (++e > 122) {
return '';
} else {
return s = s.substring(0, l) + String.fromCharCode(e);
}
}