Mars Exploration

Sort by

recency

|

1077 Discussions

|

  • + 0 comments

    int marsExploration(char* s) { char* a = malloc(strlen(s)*sizeof(char)); strcpy(a,s); int count=0; for(int i=0;a[i]!='\0';i++){ if(a[i]!='S' && a[i]!='O') count++; } return count; } Why it is showing only some test cases are fail?

  • + 0 comments

    Here is my c++ solution

    int marsExploration(string s) { int sz = s.size(); string strSos("SOS");

    int no_sos = sz/ 3;
    
    for(int i = 0; i < no_sos-1; i++)
        strSos.append("SOS");
    
    int cnt = 0;
    for( int i = 0; i < sz; i++)
    {
        if( strSos[i] != s[i] )
            cnt++;
    }
    return cnt;
    

    }

  • + 0 comments

    C Solution:

    int marsExploration(char* s) {
        int count=0,i=0;
        char SOS[]="SOS";
        while(*s){
            if(i>=3){
                i=0;
            }
            if(SOS[i]!=*s){
                count++;
            }
            i++;
            s++;
        }
        return count;
    }
    
  • + 0 comments
    public static int marsExploration(String str) {
        int count = 0;
        for(int i=1; i<=str.length();i++) {
            char ch = str.charAt(i-1);
            int loc = i%3;
            if((loc == 1 && ch != 'S') || 
                (loc == 2 && ch != 'O') || 
                (loc == 0 && ch != 'S')) 
            count++;
        }
        return count;
    }
    
  • + 0 comments

    Python solution with O(n/3) time and O(1) time. I am not sure if there is a more efficient solution. If anyone can find something more efficient please comment:

    def marsExploration(s):
        i = 2
        res = 0
        while i < len(s):
            if s[i - 2] != 'S':
                res += 1
            if s[i - 1] != 'O':
                res += 1
            if s[i] != 'S':
                res += 1
                
            i += 3
        
        return res