Mars Exploration HackerRank Solution in C, C++, Java, Python

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,|s|=12 and signal length . 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.

 

Mars Exploration HackerRank Solution in C

#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 i;
    int count=0;
    for(i=0;S[i]!='\0';i+=3){
        if(S[i]!='S'){
            count++;
        }
        if(S[i+1]!='O'){
            count++;
        }
        if(S[i+2]!='S'){
            count++;
        }
    }
    printf("%d",count);
    return 0;
}

 

Mars Exploration HackerRank Solution in C++

#include <string>
#include <vector>
#include <algorithm>
#include <numeric>
#include <set>
#include <map>
#include <queue>
#include <iostream>
#include <sstream>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <cstring>
#include <cctype>
#include <cassert>
#include <limits>
#include <functional>
#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))
#define rer(i,l,u) for(int (i)=(int)(l);(i)<=(int)(u);++(i))
#define reu(i,l,u) for(int (i)=(int)(l);(i)<(int)(u);++(i))
#if defined(_MSC_VER) || __cplusplus > 199711L
#define aut(r,v) auto r = (v)
#else
#define aut(r,v) __typeof(v) r = (v)
#endif
#define each(it,o) for(aut(it, (o).begin()); it != (o).end(); ++ it)
#define all(o) (o).begin(), (o).end()
#define pb(x) push_back(x)
#define mp(x,y) make_pair((x),(y))
#define mset(m,v) memset(m,v,sizeof(m))
#define INF 0x3f3f3f3f
#define INFL 0x3f3f3f3f3f3f3f3fLL
using namespace std;
typedef vector<int> vi; typedef pair<int, int> pii; typedef vector<pair<int, int> > vpii; typedef long long ll;
template<typename T, typename U> inline void amin(T &x, U y) { if(y < x) x = y; }
template<typename T, typename U> inline void amax(T &x, U y) { if(x < y) x = y; }

int main() {
    string S;
    while(cin >> S) {
        int ans = 0;
        rep(i, S.size())
            ans += S[i] != "SOS"[i % 3];
        printf("%d\n", ans);
    }
    return 0;
}

 

Mars Exploration HackerRank Solution in Java

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 numChanged = 0;
        
        for(int i = 0; i < S.length(); i++)
        {
            if(i % 3 == 1)
            {
                if(S.charAt(i) != 'O')
                {
                    numChanged++;
                }
            }
            else
            {
                if(S.charAt(i) != 'S')
                {
                    numChanged++;
                }
            }
        }
        
        System.out.println(numChanged);
    }
}

 

Mars Exploration HackerRank Solution in Python

S = raw_input().strip()
print len(S) - S[::3].count('S') - S[1::3].count('O') - S[2::3].count('S')

 

Mars Exploration HackerRank Solution in C#

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
class Solution {

    static void Main(String[] args) {
       string s = Console.ReadLine().Trim();
        int changedLetters = 0;
        for (int i = 0; i < s.Length; i += 3) {
            if (s[i] != 'S') { changedLetters++; }
            if (s[i+1] != 'O') { changedLetters++; }
            if (s[i+2] != 'S') { changedLetters++; }
        }

        Console.WriteLine(changedLetters);
    }
}

 

Attempt Mars Exploration HackerRank Challenge

Link – https://www.hackerrank.com/challenges/mars-exploration

Next HackerRank Challenge Solution 

Link – https://exploringbits.com/running-time-of-algorithms-hackerrank-solution/

 

Leave a Comment