Mars Exploration – HackerRank Solution

In this post, we will solve Mars Exploration HackerRank Solution. This problem (Mars Exploration) is a part of HackerRank Problem Solving series.

Task

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.

Example

s = ‘SOSTOT’

The original message was SOSSOS. Two of the message’s characters were changed in transit.

Function Description

Complete the marsExploration function in the editor below.

marsExploration has the following parameter(s):

  • string s: the string as received on Earth

Returns

  • int: the number of letters changed during transmission

Input Format

There is one line of input: a single string, s.

Constraints

  • 1 <= length of s <= 99
  • length of s modulo 3 = 0
  • s will contain only uppercase English letters, ascii[A-Z].

Sample Input 0

SOSSPSSQSSOR

Sample Output 0

3

Explanation 0

s = SOSSPSSQSSOR, and signal length |s| = 12. They sent 4 SOS messages (i.e.: 12/3 = 4).

Expected signal: SOSSOSSOSSOS
Recieved signal: SOSSPSSQSSOR
Difference:          X  X   X

Sample Input 1

SOSSOT

Sample Output 1

1

Explanation 1

s = SOSSOT, and signal length |s| = 6. They sent 2 SOS messages (i.e.: 6/3 = 2).

Expected Signal: SOSSOS     
Received Signal: SOSSOT
Difference:           X

Sample Input 2

SOSSOSSOS

Sample Output 2

0

Explanation 2

Since no character is altered, return 0.

Solution – Mars Exploration – HackerRank Solution

C++

#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;
}

Python

import sys

def marsExploration(s):
    res = 0
    for ind in range(0, len(s), 3):
        if s[ind] != 'S':
            res += 1
        if s[ind+1] != 'O':
            res += 1
        if s[ind+2] != 'S':
            res += 1
            
    return res
        

if __name__ == "__main__":
    s = input().strip()
    result = marsExploration(s)
    print(result)

Java

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

public class Solution {

    static int marsExploration(String s) {
       int l=s.length()/3;
        int x=0;
        int c=0;
        while(x<s.length())
        {
            String t=s.substring(x,x+3);

            if(t.charAt(0)!='S')
            {
                c++;
            }
                if(t.charAt(1)!='O')
                {
                    c++;
                }
                    if(t.charAt(2)!='S')
                    {
                        c++;
                    }
            x=x+3;
        }
        return c;
        // Complete this function
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String s = in.next();
        int result = marsExploration(s);
        System.out.println(result);
        in.close();
    }
}

Note: This problem (Mars Exploration) is generated by HackerRank but the solution is provided by CodingBroz. This tutorial is only for Educational and Learning purpose.

Leave a Comment

Your email address will not be published. Required fields are marked *