Mars Exploration

Sort by

recency

|

1073 Discussions

|

  • + 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;
    }
    
  • + 0 comments
    fn marsExploration(s: &str) -> i32 {
        let temp: Vec<char> = s.chars().collect();
        let mut valid = 0;
    
        for (idx, c) in temp.iter().enumerate() {
            if (idx % 3 == 0 || idx % 3 == 2) && *c == 'S' {
                valid += 1;
            } else if idx % 3 == 1 && *c == 'O' {
                valid += 1;
            }
        }
        s.len() as i32 - valid
    }