Mars Exploration

Sort by

recency

|

216 Discussions

|

  • + 0 comments

    My solution in python:

    def marsExploration(s):
        nChanged = 0
        for i, value in enumerate(s):
            remainder =  i % 3
            if remainder in (0, 2) and value != "S":
                nChanged += 1
            elif remainder == 1 and value != "O":
                nChanged += 1
        return nChanged
    
  • + 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;
    }