华为OD --- 敏感字段加密
华为OD --- 敏感字段加密
- 题目
- 独立实现
- 思路
- 源码实现
- 参考实现
题目
独立实现
思路
通过便利字符串把所有“关键字”找出来,然后将第N个关键字替换成******,最后再通过 “_” 拼接起来即可
源码实现
const rl = require("readline").createInterface({ input: process.stdin });
var iter = rl[Symbol.asyncIterator]();
const readline = async () => (await iter.next()).value;
void (async function () {
// 输入字符串个数
const allStrLength = []
// 保存关键字
const keywordIndex = Number(await readline());
let inputStr = await readline();
let currentKeyStr = ''
let startFlag = false
for (let i = 0; i < inputStr.length; i++) {
if (inputStr[i] === '"') {
if (startFlag === false) {
// 特殊关键词处理开始
startFlag = true
currentKeyStr += '"'
continue
}
if (startFlag === true) {
startFlag = false
currentKeyStr += '"'
allStrLength.push(currentKeyStr)
currentKeyStr = ''
continue
}
}
if (inputStr[i] === '_' && startFlag === false) {
if (currentKeyStr !== '') allStrLength.push(currentKeyStr)
currentKeyStr = ''
continue;
}
if (inputStr[i] !== '_' && inputStr[i] !== '"' || startFlag === true) {
currentKeyStr += inputStr[i]
}
if (i === inputStr.length - 1 && currentKeyStr !== '') {
allStrLength.push(currentKeyStr)
}
}
if(keywordIndex < allStrLength.length){
allStrLength[keywordIndex] = '******'
const transformInputStr = allStrLength.map(item => item === '' ? '""' : item).join('_')
console.log(transformInputStr)
}else{
console.log('ERROR')
}
})();
参考实现
在网上看了参考实现和题主的独立实现思路差不多,固不搬运了