Hackerrank - Mars Exploration

Hackerrank - Problem Statement

A description of the problem can be found on Hackerrank.

Solution

I divided the whole string into to 3-character parts. Then I checked for each triplet if first character is equal to S, second to O and third to S. And counted the changed letters.

I created solution in:

All solutions are also available on my GitHub.

Java

JavaScript

Scala

Ruby

2 thoughts on “Hackerrank - Mars Exploration

  1. This program will not work if input has one less char for e.e SOSSO or SO...

    correct program will be:-

    public class Solution {

    public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    String S = in.next();
    //int i=0;
    int count=0;
    int len=S.length();
    for(int i=0;i<len-1;i=i+3)
    {
    if(S.charAt(i)!='S' || ((int)S.charAt(i)==0))
    {
    count++;
    }
    if(S.charAt(i+1)!='O'|| ((int)S.charAt(i+1)==0))
    {
    count++;
    }
    if(S.charAt(i+2)!='S' || ((int)S.charAt(i+2)==0 ))
    {
    count++;
    }

    }
    System.out.println(count);
    }
    }

    1. You do not need to check that. Statement constraints are following

      • 1 < = |S| <= 99
      • |S| % 3 = 0

      The input string will never "has one less char"

Leave a Reply