Mars Exploration Hackerrank problem solution

Mars Exploration Hackerrank

Suppose there is a spaceship in the space which is crashed on Mars.Let’s say it was of Saifi’s spaceship. He was trying to send sequential messages to Earth for help which are of n Sequential SOS messages.But unfortunately, the message string is hindered by cosmic rays(radiation) during transmission to Earth.

Let’s say the Message in the string form is ‘S’.Now we have to find how many Saifi’s message letter get hindered by cosmic radiation while reaching to the earth.

https://coderinme.com/mars-exploration…problem-solution/

This Hackerrank question is through:

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

Mars Exploration Hackerrank

 

Input Format

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

Note: As the original message is just SOS repeated n times, S’s length will be a multiple of 3.

Constraints

1<=|S|<=99
S will contain only upper case English letters.
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
Sample Input 1

SOSSOT
Sample Output 1

1

Explanation

Sample 0

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

Expected signal: SOSSOSSOSSOS
Recieved signal: SOSSPSSQSSOR

We print the number of changed letters, which is 3.

Sample 1

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

Expected Signal: SOSSOS
Received Signal: SOSSOT

We print the number of changed letters, which is 1.

Solution

This is an easy string problem. Construct the expected string first. It can be done by concatenating “SOS” n times where n=length(S)/3. Now use a simple loop to count how many characters mismatch. Check the problem-setters code for this approach.

#include <iostream>
#include <algorithm>

using namespace std;


int main(){
    int res = 0;//Counter variable
    string S, m = "SOS";//Input String
    cin >> S;//Custom Input according to test case

    for(int i=0; i<S.size(); i=i+3){
        string s= S.substr(i,3);
        
        cout<<s<<endl;
        if(s!=m)
        res+=1;
    }
    cout<<res;
    return 0;
}
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();
        
        String sos = "SOS";
        int count = 0;
        for (int i = 0; i < S.length(); i++) {
            if (S.charAt(i) != sos.charAt(i % 3)) count++;
        }
        System.out.println(count);
    }
}
#include <cstring>
#include <cstdlib>
#include <fstream>
#include <numeric>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <unordered_map>

using namespace std;


int main(){
    int res = 0;//Counter variable 
    string S, m = "SOS";//Input String
    cin >> S;//Custom Input according to test case
    for(int i = 0;i < int(S.size());i++) res += (S[i] != m[i%3]);
    printf("%d\n", res);
    return 0;
}
S=raw_input()
assert len(S)%3==0 and len(S)<=99
n=len(S)/3
exp="SOS"*n #Expected string
ans=0
for i in range(len(S)):
    if exp[i]!=S[i]:
        ans=ans+1
print ans

All rights reserved. No part of this Post may be copied, distributed, or transmitted in any form or by any means, without the prior written permission of the website admin, except in the case of brief quotations embodied in critical reviews and certain other noncommercial uses permitted by copyright law. For permission requests, write to the owner, addressed “Attention: Permissions Coordinator,” to the admin@coderinme.com

A web developer(Front end and Back end), and DBA at csdamu.com. Currently working as Salesforce Developer @ Tech Matrix IT Consulting Private Limited. Check me @about.me/s.saifi

Leave a reply:

Your email address will not be published.