有一个二进制字符串,可以选择该串中的任意一段区间进行取反(可以进行一次或不进行),取反指将
变为
,将
变为
。那么取反之后的
可能的最大的字典序是多少呢。如有
,将区间
取反变为
是字典序最大的。
从左开始连续的 0 翻转即可
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param num string字符串
* @return string字符串
*/
function maxLexicographical( num ) {
// write code here
let newStr = "", status = "";
for(let i = 0; i < num.length; i ++) {
if(+num[i] === 0 && (!status || status === "open")) {
newStr += 1;
status = "open";
} else if(+num[i] === 1 && status === "open") {
newStr += num[i];
status = "close";
} else {
newStr += num[i];
}
}
return newStr;
}
module.exports = {
maxLexicographical : maxLexicographical
};