HackerRank Js-Week2 8. Mars Exploration

    HackerRank

    A space explorer’s ship crashed on Mars! They send a series of SOS messages to Earth for help.
    Letters in some of the SOS messages are altered by cosmic radiation during transmission. Given the signal received by Earth as a string, s, determine how many letters of the SOS message have been changed by radiation.

    一個太空探索者的飛船墜毀在火星上!他們向地球發送一系列 SOS 信息尋求協助。
    在 SOS 信息字母在傳輸過程中會被宇宙輻射改變。給予地球以字母形式接收的信號 s,確定 SOS 信息中有多少字母因輻設而改變。
    

    Example 1:

    s = "SOSTOT"
    The original message was SOSSOS. Two of the message's characters were changed in transit.
    

    solution:
    試著將 SOS 標準信息找出規律,以此將遍歷的過程以三元運算式判斷,
    發現只要是 3 的倍數且餘 1,如符合則為 “O”,不符合則為 “S”。
    後續確認遍歷過程中,不符合標準信息之字母,運用 count += 1 紀錄後回傳。

    S O S S O S S O S S  O  S  S  O
    0 1 2 3 4 5 6 7 8 9 10 11 12 13
    

    Code 1: BigO(n)

    'use strict';
    
    const fs = require('fs');
    
    process.stdin.resume();
    process.stdin.setEncoding('utf-8');
    
    let inputString = '';
    let currentLine = 0;
    
    process.stdin.on('data', function(inputStdin) {
        inputString += inputStdin;
    });
    
    process.stdin.on('end', function() {
        inputString = inputString.split('\n');
    
        main();
    });
    
    function readLine() {
        return inputString[currentLine++];
    }
    
    /*
     * Complete the 'marsExploration' function below.
     *
     * The function is expected to return an INTEGER.
     * The function accepts STRING s as parameter.
     */
    
    function marsExploration(s) {
        // Write your code here
        let count = 0
        
        for (let i = 0; i < s.length; i++) {
          let letter = (i % 3 === 1) ? "O" : "S"
          
          if (s.charAt(i) !== letter) count++
        }
        return count;
    }
    
    function main() {
        const ws = fs.createWriteStream(process.env.OUTPUT_PATH);
    
        const s = readLine();
    
        const result = marsExploration(s);
    
        ws.write(result + '\n');
    
        ws.end();
    }
    

    FlowChart:
    Example 1

    s = "SOSTOT"
    
    STD = "SOSSOS"
    s   = "SOSTOT
    return count //2