Mars Exploration

Sort by

recency

|

204 Discussions

|

  • + 0 comments

    Js solution:

    return s.split('').reduce((accumulator ,letter, currentIndex: number) => {
        if(
            ((currentIndex%3 === 0 || currentIndex%3 === 2) && letter !== 'S') ||
            (currentIndex%3 === 1 && letter !== 'O')
        ){
            return accumulator += 1;
        }
        return accumulator;
    }, 0);
    

    }

  • + 0 comments

    counter = 0 for i in range(0, len(s), 3): if s[i] !='S': counter+=1 if s[i+1] != 'O': counter+=1 if s[i+2] !='S': counter+=1 return counter

  • + 0 comments

    My TypeScript solution:

    function marsExploration(s: string): number {
        const length = s.length;
        let changedChars = 0;
        
        for (let i = 0; i < length; i++) {
            const originalChar = (
                i % 3 === 0 ||
                (i + 1) % 3 === 0
            )  ? "S" : "O";
            if (s[i] !== originalChar) changedChars ++;
            
        }
        
        return changedChars;
    }
    
  • + 0 comments

    My rust solution:

     const SOS_LEN: usize = 3;
    
    fn marsExploration(s: &str) -> i32 {
        (0..s.len())
            .step_by(3)
            .map(|i| {
                s[i..i + 3].bytes()
                    .zip([b'S', b'O', b'S'])
                    .filter(|&zip| zip.0 != zip.1)
                    .count() as i32
            })
            .sum()
    }
    
  • + 0 comments

    Python 1 liner.

        return len([c for i, c in enumerate(s) if c != 'SOS'[i % 3]])