使用 javascript 将字符串从 aaabccdd 解密为 a3b1c2d2?
Using javascript decipher a string from aaabccdd to a3b1c2d2?
我试过这种方法,但没有得到字符串的第一个元素。我试图以相反的方式进行转换,这太容易了。但在这一个不知何故我错过了一些东西。问题是,我们需要破译从“aabbcdd”到“a2”的字符串
b2c1d2
function check(len, string){
// var obj = {};
var new_string = "";
var count =1;
//console.log(string);
for(var i=0; i < string.length; i++){
let str = string[i];
if(string[i]===string[i+1] ){
count++;
new_string = new_string + string[i+1] + count;
}
if(string[i]!== string[i-1] && string[i]!== string[i+1] && string[i-1] !==undefined){
count = 1;
new_string = new_string + string[i-1] + count;
}
// console.log(string[i],string[i+1], count)
}
console.log(new_string);
}
谢谢
您需要以不同于其他字符的方式处理字符串的第一个字符,并且您还需要在处理完最后一个字符后再次更新字符串。
这里有一个评论很多的例子,演示了如何做到这一点。如果有什么不清楚的地方,请随时在评论中要求澄清。
You might find it useful to reference string
iterators on MDN.
// Regular expression used to validate each unit (character) of the string
// in the following function, but we instantiate it once instead of
// every time the function is called
const lowerAlphaCharacterRegex = /^[a-z]$/;
// Validate the expected character
function validate (str) {
if (!lowerAlphaCharacterRegex.test(str)) throw new Error('Invalid input');
}
function encode (input) {
let result = '';
// Get an interator for the string to handle each unit
const iter = input[Symbol.iterator]();
// Get the first unit and assign it to the working character state
let previous = iter.next().value;
// Validate that it meets the criteria
validate(previous);
// Set the intiial count to 1
let count = 1;
// Iterate over the remaining units in the string
for (const char of iter) {
// Validate that it meets the criteria
validate(char);
// Simply update the count and continue to the next loop iteration
// if the current character is the same as the previous one
if (char === previous) {
count += 1;
continue;
}
// Otherwise...
// Update the result
result += `${previous}${count}`;
// Update the character state
previous = char;
count = 1;
}
// Update the result from the remaining state
result += `${previous}${count}`;
return result;
}
const input = 'aabbcdd';
const expected = 'a2b2c1d2';
const actual = encode(input);
console.log({
actual,
valid: actual === expected,
});
我试过这种方法,但没有得到字符串的第一个元素。我试图以相反的方式进行转换,这太容易了。但在这一个不知何故我错过了一些东西。问题是,我们需要破译从“aabbcdd”到“a2”的字符串 b2c1d2
function check(len, string){
// var obj = {};
var new_string = "";
var count =1;
//console.log(string);
for(var i=0; i < string.length; i++){
let str = string[i];
if(string[i]===string[i+1] ){
count++;
new_string = new_string + string[i+1] + count;
}
if(string[i]!== string[i-1] && string[i]!== string[i+1] && string[i-1] !==undefined){
count = 1;
new_string = new_string + string[i-1] + count;
}
// console.log(string[i],string[i+1], count)
}
console.log(new_string);
}
谢谢
您需要以不同于其他字符的方式处理字符串的第一个字符,并且您还需要在处理完最后一个字符后再次更新字符串。
这里有一个评论很多的例子,演示了如何做到这一点。如果有什么不清楚的地方,请随时在评论中要求澄清。
You might find it useful to reference
string
iterators on MDN.
// Regular expression used to validate each unit (character) of the string
// in the following function, but we instantiate it once instead of
// every time the function is called
const lowerAlphaCharacterRegex = /^[a-z]$/;
// Validate the expected character
function validate (str) {
if (!lowerAlphaCharacterRegex.test(str)) throw new Error('Invalid input');
}
function encode (input) {
let result = '';
// Get an interator for the string to handle each unit
const iter = input[Symbol.iterator]();
// Get the first unit and assign it to the working character state
let previous = iter.next().value;
// Validate that it meets the criteria
validate(previous);
// Set the intiial count to 1
let count = 1;
// Iterate over the remaining units in the string
for (const char of iter) {
// Validate that it meets the criteria
validate(char);
// Simply update the count and continue to the next loop iteration
// if the current character is the same as the previous one
if (char === previous) {
count += 1;
continue;
}
// Otherwise...
// Update the result
result += `${previous}${count}`;
// Update the character state
previous = char;
count = 1;
}
// Update the result from the remaining state
result += `${previous}${count}`;
return result;
}
const input = 'aabbcdd';
const expected = 'a2b2c1d2';
const actual = encode(input);
console.log({
actual,
valid: actual === expected,
});