2017-03-23 6 views
1

でAZを生成するために、私は、ここでの問題がどのように最善のタイトル私の問題をするのか分かりません[AZ]良いアルゴリズム与えられたAZ

の文字列のセット

出力yはxの一文字です。

EX:

X = A - > Yが= B

X = Z - > Yが= AA

X = ABは - > yはAC

X = ZZを= - > yは= AAA

X = ZZA - > Y = ZZB

誰もがjavascriでこれを実装するための良い方法を持っていますpt?

私は今、非常に完成した状態チェックで、ZならばA、1つの文字を時間です。

+2

したがって、ベース26カウントシステムを実装していますか? –

+1

@MikeC A = 1、Z = 26.の場合、問題0をどうやって解決しますか?A + = AならばAAAを表現する方法は? – YoungLearnsToCoding

+0

楽しい質問 - 面接でそれを使用するように誘惑:) – Triptych

答えて

2

これを行う1つの方法は、ストリームの最後の文字を見て、それを文字コードに変換して1つ増やします。すでにZであれば、最後の文字をAにリセットし、2番目の文字を最後の文字に置き換えます。あなたが最後になるまで繰り返す。

function increment(x) { 
 
    if (!x) { 
 
    return ''; 
 
    } 
 

 
    // Convert the string into an array of characters 
 
    let chars = x.split(''); 
 
    
 
    let i = chars.length - 1; 
 
    for (; i >= 0; i--) { 
 
    let oldChar = chars[i]; 
 
    if (oldChar === 'Z') { 
 
     // If it's a Z, reset to A and process 
 
     // the previous letter in the sequence 
 
     chars[i] = 'A'; 
 
    } else { 
 
     // Convert the character to a character code 
 
     let code = chars[i].charCodeAt(0); 
 
     // Increment that code and convert it back to a character 
 
     let newChar = String.fromCharCode(code + 1); 
 
     
 
     // Replace the existing letter 
 
     chars[i] = newChar; 
 
     break; 
 
    } 
 
    } 
 

 
    // If we got all the way to the end, that means that all 
 
    // of the letters were Z's that have now been converted 
 
    // to A's. Append one more A to finish the incrementing 
 
    if (i === -1) { 
 
    chars.push('A'); 
 
    } 
 

 
    // Join the array of characters together to form a string 
 
    return chars.join(''); 
 
} 
 

 
console.log(increment('A')); // B 
 
console.log(increment('Z')); // AA 
 
console.log(increment('AB')); // AC 
 
console.log(increment('ZZ')); // AAA 
 
console.log(increment('ZZA')); // ZZB

+0

これは良い、ありがとう:) – YoungLearnsToCoding

1

整数に変換します。

function decode(str) { 
    var i = str.toUpperCase().charCodeAt(str.length-1) - 65; // A=0, B=1, … 
    return str.length == 1 ? i : i+26*(1+decode(str.slice(0,-1))); 
} 
function encode(i) { 
    var c = String.fromCharCode((i % 26) + 65); 
    return i >= 26 ? encode(Math.floor(i/26)-1)+c : c; 
} 

しかし、その後、あなたの関数は簡単です::1は「先行ゼロ」の上にジャンプする必要があるので、それは少し奇妙だ

function f(x) { 
    return encode(1 + decode(x)); 
} 
1

別のエンコード/デコード・ソリューション、不可欠であり、w /テスト。

const decode = n => { 
    let r = []; 
    let i = n; 
    while (i) { 
    r.push((i-1) % 26); 
    i = Math.floor((i-1)/26); 
    } 
    return r.reverse().map(c => String.fromCharCode(c + 65)); 
}; 

const encode = s => { 
    const digits = s.split('').map(c => c.charCodeAt(0) - 65); 
    let n = 0; 
    while (digits.length) { 
    let c = digits.shift() + 1; 
    n *= 26; 
    n += c; 
    } 
    return n; 
}; 

const next = s => decode(encode(s) + 1).join(''); 

const tests = [ 
    ['A','B'], 
    ['Z', 'AA'], 
    ['AA','AB'], 
    ['BB','BC'], 
    ['BC','BD'], 
    ['ZZ', 'AAA'], 
    ['ZYZ', 'ZZA'], 
    ['ZZA', 'ZZB'], 
] 

tests.map(parts => { 
    if (next(parts[0]) !== parts[1]) { 
    throw 'test failed for ' + parts[0]; 
    } 
    console.log(parts[0], '->', next(parts[0])); 
}); 
関連する問題