Mars Exploration

Sort by

recency

|

1062 Discussions

|

  • + 0 comments
    public static int marsExploration(String s) {
      // Write your code here
      int diff = 0;
      for (int i = 0; i < s.length(); i++) {
        if ((i % 3 == 0 || i % 3 == 2) && s.charAt(i) != 'S') {
          diff++;
        } else if (i % 3 == 1 && s.charAt(i) != 'O') {
          diff++;
        }
      }
      return diff;
    }
    
  • + 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
    def marsExploration(s):
        c=0
        for i in range(0,len(s)//3):
            if s[0+3*i]!='S':
                c+=1
            if s[1+3*i]!='O':
                c+=1
            if s[2+3*i]!='S':
                c+=1
            
        return c
    
  • + 0 comments

    Java 8:

    public static int marsExploration(String s) {
            String sos = "SOS";
            int totalChangedLetters = 0;
            for (int i = 0; i < s.length(); i++) {
                if (s.charAt(i) != sos.charAt(i % 3)) {
                    totalChangedLetters++;
                }
            }
            return totalChangedLetters;
        }
    
  • + 0 comments
    public static int changedLetters(char s1, char O, char s2){
            int count=0;
            if (s1!='S') {
                count++;
            }
            if (s2!='S') {
                count++;
            }
            if (O!='O') {
                count++;
            }
            return count;
        }
        public static int marsExploration(String s) {
        // Write your code here
        int output=0;
        for (int i = 0; i < s.length(); i+=3) {
            int count=0;
            count=changedLetters(s.charAt(i+0),s.charAt(i+1),s.charAt(i+2));
            output=output+count;
            System.err.println("count="+count+"output="+output);
        }
        return output;
    
        }