Mars Exploration | HackerRank



Sami's spaceship crashed on Mars! She sends 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 Sami's SOS have been changed by the radiation.
For example, Earth receives SOSTOT. Sami's original message was SOSSOS. Two of the message characters were changed in transit.
Function Description
Complete the marsExploration function in the editor below. It should return an integer representing the number of letters changed during transmission.
marsExploration has the following parameter(s):
  • s: the string as received on Earth
Input Format
There is one line of input: a single string, .
Note: As the original message is just SOS repeated  times, 's length will be a multiple of .
Constraints
  •  will contain only uppercase English letters, ascii[A-Z].
Output Format
Print the number of letters in Sami's message that were altered by cosmic radiation.
Sample Input 0
SOSSPSSQSSOR
Sample Output 0
3
Explanation 0
 = SOSSPSSQSSOR, and signal length . Sami sent  SOS messages (i.e.: ).
Expected signal: SOSSOSSOSSOS
Recieved signal: SOSSPSSQSSOR
Difference:          X  X   X
We print the number of changed letters.

Solution (C++):

#include <bits/stdc++.h>
using namespace std;
int marsExploration(string s) {
    int count=0, num = s.size();
    while(s.size()!=0)
    {
        string r = s.substr(03);
        if(r!="SOS") count++;
        s.erase(0,3);
    }
    return count;

}
int main()
{
    ofstream fout(getenv("OUTPUT_PATH"));
    string s;
    getline(cin, s);
    int result = marsExploration(s);
    fout << result << "\n";
    fout.close();
    return 0;
}

Comments

Popular posts from this blog

Boxes through a Tunnel | HackerRank

Array Manipulation | HackerRank

String Validators | HackerRank