Mars Exploration

Sort by

recency

|

1070 Discussions

|

  • + 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
    }
    
  • + 0 comments

    Java 8 code

    public static int findDifferentLetters(String s){ int count = 0; if(s.charAt(0)!='S'){ count++; } if(s.charAt(1)!='O'){ count++; } if(s.charAt(2)!='S'){ count++; } return count; }

    public static int marsExploration(String s) { // Write your code here int ans = 0; // int count =0; for(int i =0;i

    }

    public class Solution { public static void main(String[] args) throws IOException { BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in)); BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));

        String s = bufferedReader.readLine();
    
        int result = Result.marsExploration(s);
    
        bufferedWriter.write(String.valueOf(result));
        bufferedWriter.newLine();
    
        bufferedReader.close();
        bufferedWriter.close();
    }
    

    }

  • + 0 comments

    A short Python solution:

    def marsExploration(s):
        correct_signal = 'SOS' * (len(s) // 3)
        return sum(s[i] != correct_signal[i] for i in range(len(s)))
    
  • + 0 comments

    My Java solution:

    public static int marsExploration(String s) {
            int altMsgCount = 0;
            int counter = 0;
            
            for(int i = 0; i < s.length(); i++){
                counter++;
                char token = s.charAt(i);
                
                switch(counter){
                    case 1:
                        if(token != 'S') altMsgCount++;
                        break;
                    case 2:
                        if(token != 'O') altMsgCount++;
                        break;
                    case 3:
                        if(token != 'S') altMsgCount++;
                        break;
                }
                
                if(counter == 3) counter = 0; 
            }
            
            return altMsgCount;
        }