Interview Problem Solving – Mars Exploration

By | April 26, 2019

Problem

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

SOSSPSSQSSOR

Sample Output

3

Download the complete problem statement from mars-exploration-English” rel=”noopener” target=”_blank”>here.

Solution

static int marsExploration(String s) {
    int altCount = 0;
    int i = 0;
    
    int length = s.length();
    
    while(true) {
        
        if(i >= length) {
            break;
        }
        
        String sub = s.substring(i, i+ 3);
        
        System.out.println(sub);
        if(sub.contentEquals("SOS")) {
            
        }else {
            
            char c1 = sub.charAt(0);
            char c2 = sub.charAt(1);
            char c3 = sub.charAt(2);
            
            if(c1 != 'S') {
                altCount++;
            }
            if(c2 != 'O') {
                altCount++;
            }
            if(c3 != 'S') {
                altCount++;
            }
        }
        
        i = i + 3;
        
    }
    return altCount;

}

Leave a Reply

Your email address will not be published. Required fields are marked *