Mars Exploration

Sort by

recency

|

213 Discussions

|

  • + 0 comments

    Here's my python code:

    def marsExploration(s):
        sos = "SOS" * (len(s)//3)
        changed = 0
        for index, item in enumerate(s):
            if item != sos[index]:
                changed += 1
        return changed
    
  • + 0 comments

    Herw my java code:

    public static int marsExploration(String s) {
     int    count = 0;
    char[] ch = s.toCharArray();
    for(int i=0; i<s.length(); i+=3) {
        if(ch[i] !='S')
            count++;
        if(ch[i+1] !='O')
            count++;
        if(ch[i+2] !='S')
           count++;
    }
    return count;
    }
    
  • + 0 comments

    Rust best solution

    If you’re looking for solutions to the 3-month preparation kit in either Python or Rust, you can find them below: my solutions

    pub fn mars_exploration(s: &str) -> i32 {
        //Time complexity: O(n)
        //Space complexity (ignoring input): O(1)
        let sos_array: Vec<_> = "SOS".chars().collect();
        let mut changed_letters = 0;
        for (index, letter) in s.char_indices(){
            if letter != sos_array[index%3]{
                changed_letters += 1;
            }
        }
        changed_letters
    }
    
  • + 0 comments

    Python best solution

    If you’re looking for solutions to the 3-month preparation kit in either Python or Rust, you can find them below: my solutions

    def mars_exploration(s):
        # Time complexity: O(n)
        # Space complexity (ignoring input): O(1)
        sos_array = "SOS"
        changed_letters = 0
        for index in range(0, len(s)):
            letter = s[index]
            if letter != sos_array[index % 3]:
                changed_letters += 1
    
        return changed_letters
    
  • + 0 comments
    //java
        public static int marsExploration(String s) {
        // Write your code here
        int changedLetters = 0;
        char[] chars = s.toCharArray();
        for(int i=0; i<s.length(); i+=3) {
            if(chars[i] != 'S')
                changedLetters++;
            if(chars[i+1] !='O')
                changedLetters++;
            if(chars[i+2] !='S')
                changedLetters++;
        }
        return changedLetters;
    }