Mars Exploration HackerRank Problem Solution

In this Mars Exploration HackerRank Problem, 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, , determine how many letters of the SOS message have been changed by radiation.

Example

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, .

Constraints

•  will contain only uppercase English letters, ascii[A-Z].

Explanation

Sample 0

= SOSSPSSQSSOR, and signal length . Sami sent  SOS messages (i.e.: ).

Expected signal: SOSSOSSOSSOS
Recieved signal: SOSSPSSQSSOR

We print the number of changed letters, which is .

Sample 1

= SOSSOT, and signal length . Sami sent  SOS messages (i.e.: ).

Expected Signal: SOSSOS
Received Signal: SOSSOT

We print the number of changed letters, which is .

Mars Exploration Hacker Rank Problem Solutions

I will Provide solution in Multiple programming languages for you. If you are not able to find the code in required language then please share in comments so that our team can help you.

Problem Solution in JavaScript

function marsExploration(s) {
let numberOfSos = s.length / 3;
let correctSignal = 'SOS'.repeat(numberOfSos);
let numberOfChanges = 0;
for (let i=0; i< s.length; i++){
if(s[i] !== correctSignal[i]){
numberOfChanges +=1;
}
}
return numberOfChanges;
}

Problem Solution in Python

def marsExploration(s):
length = len(s)
numner_messages = length/3
expected_signal = 'SOS'*int(numner_messages)
count = 0
for char1, char2 in zip(s,expected_signal):
if char1 != char2:
count +=1
return(count)

Problem Solution in Java

public static int marsExploration(String s) {

int count = 0;
for(int i = 0; i<s.length(); i=i+3){
String sub = s.substring(i,i+3);
if(!sub.equals("SOS")){
if(sub.charAt(0)!='S'){
count++;
}
if(sub.charAt(1)!='O'){
count++;
}
if(sub.charAt(2)!='S'){
count++;
}
}
}
return count;
}