Mars Exploration

Sort by

recency

|

1074 Discussions

|

  • + 0 comments
    public static int marsExploration(String str) {
        int count = 0;
        for(int i=1; i<=str.length();i++) {
            char ch = str.charAt(i-1);
            int loc = i%3;
            if((loc == 1 && ch != 'S') || 
                (loc == 2 && ch != 'O') || 
                (loc == 0 && ch != 'S')) 
            count++;
        }
        return count;
    }
    
  • + 0 comments

    Python solution with O(n/3) time and O(1) time. I am not sure if there is a more efficient solution. If anyone can find something more efficient please comment:

    def marsExploration(s):
        i = 2
        res = 0
        while i < len(s):
            if s[i - 2] != 'S':
                res += 1
            if s[i - 1] != 'O':
                res += 1
            if s[i] != 'S':
                res += 1
                
            i += 3
        
        return res
    
  • + 0 comments

    For C#

    return string.Concat(Enumerable.Range(0, s.Length / 3) .Select(s => "SOS")) .Select((c, i) => c != s[i] ? 1 : 0) .Sum();

  • + 0 comments

    Here is problem solution in python java c++ c and Javascript - https://programmingoneonone.com/hackerrank-mars-exploration-problem-solution.html

  • + 0 comments

    Here is my c++ solution, you can find the explanation here : https://youtu.be/Gf0fpGE83r4

    int marsExploration(string s) {
       int result = 0;
        string base = "SOS";
        for(int i = 0; i < s.size(); i++) if(s[i] != base[i%3]) result++;
        return result;
    }