Mars Exploration

Sort by

recency

|

215 Discussions

|

  • + 0 comments

    swift

    func marsExploration(s: String) -> Int {
        var changed = 0
        for (index, c) in s.enumerated() {
            switch index % 3 {
                case 0, 2:
                   if c != "S" {
                        changed = changed + 1
                   }
                case 1:
                    if c != "O" {
                        changed = changed + 1
                   }
                default:
                    break
            }
        }
        return changed
    }
    
  • + 0 comments

    We need to keep track of the position since an S could also be changed for an O. So just counting isn't enough

      public static int marsExploration(String s) {
            
            int counter = 0;
            int position = 1;
            for(int i = 0; i < s.length(); i++){            
                System.out.println(position);
                
                if((position == 1 || position == 3) && s.charAt(i) != 'S') {
                counter++;
                }else if(position == 2 && s.charAt(i) != 'O'){
                counter++;
                }   
                
                if(position == 3){
                    position = 1;
                }else{
                    position++;
                }                                                                                                                                            
            }
            
            return counter;
        }
    
  • + 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
    }