Hello Programmers, In this post, you will learn how to solve HackerRank Mars Exploration Solution. This problem is a part of the HackerRank Algorithms Series.
One more thing to add, don’t directly look for the solutions, first try to solve the problems of Hackerrank by yourself. If you find any difficulty after trying several times, then you can look for solutions.
HackerRank Mars Exploration Solution
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.
HackerRank Mars Exploration Solutions
Mars Exploration 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 Solution in Cpp
#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(){ string str; cin >> str; string pattern = "SOS"; int count = 0; for (int i = 0; i < str.length(); ++i) { if (str[i] != pattern[i%3]) ++count; } cout << count << endl; return 0; }
Mars Exploration 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 Solution in Python
X = raw_input() ans = 0 while X: ans += [1, 0][X[-1] == "S"] ans += [1, 0][X[-2] == "O"] ans += [1, 0][X[-3] == "S"] X = X[:-3] print ans
Mars Exploration Solution using JavaScript
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 res = 0; var sosCount = Math.floor(S.length/3); for (var i = 0; i<sosCount;i++){ if (S.charAt(i*3) !== 'S') res++; if (S.charAt(i*3+1) !== 'O') res++; if (S.charAt(i*3+2) !== 'S') res++; } process.stdout.write(res); }
Mars Exploration Solution in Scala
object Solution { def main(args: Array[String]) = { val in = new java.util.Scanner(System.in) var inputString = in.nextLine() var result = 0 while(inputString.nonEmpty) { result += countDiff(inputString.substring(0, 3)) inputString = inputString.substring(3) } println(result) } def countDiff(stringToChek: String) = { var result = 0 if(stringToChek.substring(0, 1) != "S") result += 1 if(stringToChek.substring(1, 2) != "O") result += 1 if(stringToChek.substring(2, 3) != "S") result += 1 result } }
Mars Exploration Solution in Pascal
var a:array[0..2]of char; d,i,n:longint; s:string; procedure rd; begin readln(s); n:=length(s); a[0]:='S'; a[1]:='S'; a[2]:='O'; end; procedure xl; begin for i:=1 to n do if a[i mod 3]<>s[i] then inc(d); writeln(d); end; begin rd; xl; end.
Disclaimer: This problem (Mars Exploration) is generated by HackerRank but the Solution is Provided by BrokenProgrammers. This tutorial is only for Educational and Learning purposes.