Mars Exploration

Published on August 06, 2021
Last updated August 06, 2021

Given problem statment

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.

solution in java

import java.util.*;

class Solution{
    public static void main(String[] args){
        int result = 0;
        Scanner sc = new Scanner(System.in);
        String message = sc.next();
        int message_length = message.length();
        for(int i = 0; i < message_length ; i+=3){
            if(message.charAt(i) != 'S')
                result++;
            if(message.charAt(i+1) != 'O')
                result++;
            if(message.charAt(i+2) != 'S')
                result++;
        }
        System.out.println(result);
        sc.close();
    }
}


Tags :