In this HackerRank Mars Exploration problem, you have Given the signal received by Earth as a string, determine how many letters of the SOS message have been changed by the radiation.

HackerRank Mars Exploration problem solution


Problem solution in Python programming.

#!/bin/python3

import sys


S = input().strip()

num_errors = 0

for i, char in enumerate(S):
    if i%3 == 1:
        if char != 'O':
            num_errors += 1
    else:
        if char != 'S':
            num_errors += 1
  
print(num_errors)


Problem solution in Java Programming.

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String S = in.next();
        int count =0;
        for(int i=0;i<S.length();i =i+3){
            if(S.charAt(i)!='S'){
                count++;
            } 
            if(S.charAt(i+1)!='O'){
                count++;
                
            }
            
            if(S.charAt(i+2)!='S'){
                count ++;
            }
            
        }
        
        System.out.println(count);
        
    }
}


Problem solution in C++ programming.

#include <map>
#include <set>
#include <list>
#include <cmath>
#include <ctime>
#include <deque>
#include <queue>
#include <stack>
#include <string>
#include <bitset>
#include <cstdio>
#include <limits>
#include <vector>
#include <climits>
#include <cstring>
#include <cstdlib>
#include <fstream>
#include <numeric>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <unordered_map>

using namespace std;

int main(){
    ios::sync_with_stdio(false);
    string str;
    cin >> str;
    int ans = 0;
    for( int i = 0; i < str.size(); i += 3 ){
        if( str[i] != 'S' ) ans++;
        if( str[i+1] != 'O' ) ans++;
        if( str[i+2] != 'S' ) ans++;
    }
    cout << ans << '\n';
    return 0;
}


Problem solution in C programming.

#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <stdbool.h>

int main(){
    char* S = (char *)malloc(10240 * sizeof(char));
    scanf("%s",S);
    int len = strlen(S);
    int iTemp = 0;
    int alt = 0;
    for(iTemp=0;iTemp<len;iTemp=iTemp+3)
    {
            if(S[iTemp] != 'S')
            {
                alt++;
            }
            if(S[iTemp+1] != 'O')
            {
                alt++;    
            }
            if(S[iTemp+2] != 'S')
            {
                alt++;    
            }
    }
    printf("%d\n",alt);
    return 0;
}


Problem solution in JavaScript programming.

process.stdin.resume();
process.stdin.setEncoding('ascii');

var input_stdin = "";
var input_stdin_array = "";
var input_currentline = 0;

process.stdin.on('data', function (data) {
    input_stdin += data;
});

process.stdin.on('end', function () {
    input_stdin_array = input_stdin.split("\n");
    main();    
});

function readLine() {
    return input_stdin_array[input_currentline++];
}

/////////////// ignore above this line ////////////////////

function main() {
    var S = readLine();
    var num=0;
    var test;
    for(var i=0,len=S.length;i<len;i++){
        test=(i%3==1) ? 'O' : 'S';
        if(S.charAt(i)!=test){
            num++;
        }
    }
    console.log(num);
}