Loading...

Sort 322 Discussions, By:

  • DC
    cys920622 + 16 comments

    Straightforward Java solution in constant space and O(n) time.

        public static int countChanges(String message) {
            String sos = "SOS";
            int count = 0;
            for (int i = 0; i < message.length(); i++) {
                if (message.charAt(i) != sos.charAt(i % 3)) count++;
            }
            return count;
        }
    
    • shravankumar1 + 2 comments

      Thank you

      • Phoenix98 + 0 comments

        include

        include

        include

        include

        include

        include

        include

        int main(){

        int i,len,counto,counts,total;
        char* S = (char *)malloc(10240 * sizeof(char));
        scanf("%s",S);
        len=strlen(S);
        len=len/3;
        counto=0;
        counts=0;
        total=0;
        for(i=0;i<len;i++) {
            if(S[3*i]!='S') {
                counts++;
            }
            if(S[2*i+2+i]!='S') {
                counts++;
            }
            if(S[3*i+1]!='O') {
                counto++;
            }
        }
        total=counts+counto;
        
        printf("%d",total);
        
        return 0;
        

        }

      • Naveen_Malla + 4 comments

        SIMPLE C CODE

        int main(){
            char* s = (char *)malloc(10240 * sizeof(char));
            int C=0;
            scanf("%s",s);
            for(int i=0;i<strlen(s);i=i+3)
            {
                if(s[i]!='S')
                    C++;
                if(s[i+1]!='O')
                    C++;
                if(s[i+2]!='S')
                    C++;
            
            }
            
            printf("%d",C);
            return 0;
        }
        
        • tyagisagar79 + 0 comments
          [deleted]
        • phoemur + 1 comment

          In this case its OK, but I just don't like seeing malloc and no free...

          • saiful007 + 0 comments

            here is my c++ code

            include

            using namespace std; int main(){ int count = 0; string s; cin >> s; for(int i=0; i

        • KS
          keshavashourie3 + 0 comments

          good one

        • Prakash221095 + 0 comments

          its ok, but there is a testcase as input SOSSWEWSSTTS .how can it be handled.

    • NA
      nayanansh + 0 comments
      [deleted]
    • AR
      Ankur_Beginning + 0 comments

      cool

    • JM
      javedmoh2001 + 0 comments

      Great soln...

    • satyakibose98 + 0 comments

      why the method is static?

    • jemmydng12 + 1 comment
      wts wrong in this code
      
      public static void main(String[] args) {
              Scanner in = new Scanner(System.in);
              String s = in.next();
             int l= s.length();
              int b=l/3;
              int count=0;
              int num;
              for(int i=0;i<b;i++)
                  {
                  num=(int)s.charAt(i);
                  
                  if(s.charAt(i) != s.charAt(i+b))
                      {
                      count ++;
                  }
              }
              System.out.println(count);
      
      • kashyap_rishab91 + 0 comments

        Why you are converting character to integer?

        num=(int)s.charAt(i);

        Since radiation can alter any character in string,so u need to scan all characters in string to check the change in message.

    • CM
      chrismin + 0 comments

      Amazing, thank you. I definitely have to make use of String methods a lot more doing these types of problems.

    • Srikanth_kalka + 1 comment

      if (message.charAt(i) != sos.charAt(i % 3)) count++;

      Can u explain above condition please....

      • akhilgautam123 + 1 comment

        Checking changes between each "SOS". Lets take message = "SOSTRSSOO" and sos = "SOS" for i = 0; i< 9; i++, we will have: message.charAt(i) != sos.charAt(i % 3) is false upto i = 2. Now when i = 3, message[3] != sos[3 % 3 i.e. 0] is true. So count will increase. Again at i =4: message[4] != sos[4 % 3 i.e. 1] is true.

        Briefly, the whole message is broken into set of 3 characters and compared to sos

        • Srikanth_kalka + 0 comments

          Thank you for your explanation Akhil. Appreciate it.

    • rishravi + 0 comments

      this approach is better(atleast better looking and compact) than using 3 if-statements. Thanks!

    • tyagisagar79 + 0 comments

      It seems that your code is wrong... For "ROR" the output should be 1.. but your code will give 2...

    • beckss + 0 comments
      [deleted]
    • abhinandkr + 0 comments

      A slight variant:

      string S;
      cin >> S;
      string sos = "SOS";
      int sum = 0;
      for (int i = 0; i < S.length(); i++) {
          if (S[i] - sos[i % 3]) {
              sum++;
          }
      }
      cout << sum << endl;
      
    • VM
      MVijayaanand + 0 comments

      Cute approach

    • AB
      adityabadve123 + 0 comments

      Did the same thing with c++

      int marsExploration(string s,int len) { 
          int error = 0;
          string sos = "SOS";
          for(int i=0;i<len;i++){
              if(s[i] != sos[i%3]){
                  error++;
              }
          }
          return(error);
      }
      
    • hari_4698 + 0 comments
      [deleted]
    • SS
      sanjay228 + 0 comments

      good approach

  • AN
    ajHash + 1 comment

    This thing kinda clicked..

    int main(){
        string s;
       char te[3];
        te[0]='S';
        te[1]='O';
        te[2]='S';
        cin >> s;int i=0,j=0,count=0;;
        while(s[i])
            {   if(j==3)j=0;
                if((s[i])!= (te[j]))count++;
                i++;j++;   
            }
        cout<<count;
        return 0;
    }
    
    • silja_tirronen + 0 comments

      I like this solution because it can be easily extended to handle an arbitrary pattern, of arbitrary length. In our case the pattern just happens to be "SOS". Here is my implementation in Java, which takes the pattern as a parameter.

      public static void main(String[] args) {
              
              Scanner in = new Scanner(System.in);
              String s = in.next();
              
              System.out.println(countCorruptSymbols(s, "SOS"));
      }
          
      private static int countCorruptSymbols(String input, String pattern) {
              
          int i = 0;
          int p = 0;
              
          int count = 0;
          while (i < input.length()) {
      
              if (input.charAt(i) != pattern.charAt(p)) {
                  count++;
              }
      
              i++;
              p = (p + 1) % pattern.length();
          }
              
          return count;
      
      }
      
  • SS
    shrishtee_ruchi + 2 comments

    java

    public static void main(String[] args) {
            Scanner in = new Scanner(System.in);
            String S = in.next();
            String sos = "SOS";
            int count =0;
            for (int i = 0; i < S.length(); i++) {
                if (S.charAt(i) != sos.charAt(i % 3)) count++;
            }
            System.out.println(count);
        }
    
    • SM
      smadden03 + 0 comments

      why did you do sos.charAt(i % 3)

    • PO
      parviz0492 + 1 comment

      My complexity is here O(n^2). But eventually in fact are my steps equal to yours ?

      public static void main(String[] args) {
              Scanner scanner = new Scanner(System.in);
      		
      		String message = scanner.nextLine().toUpperCase();
      			
      		int counter = 0;
      		if(message.length() > 0 && message.length() < 100 && message.length() % 3 == 0){
      			for(int i = 0; i < message.length() / 3 ; i++){
      				String str = message.substring(i * 3, (i + 1) * 3);
      				int localCounter = 0;
      				
      				for(int j = 0; j < 3; j++){
      					if(!(str.charAt(j) == "SOS".charAt(j)))
      						localCounter++;
      				}
      				
      				counter += localCounter;
      			}
      		}
      		
      		System.out.println(counter);
      		
      		scanner.close();
          }
      
      • AM
        muzic_maniac7 + 0 comments

        You don't need to convert the message into uppercase. It is already stated in the problem that the message will be in uppercase. And why are you checking for constraints! Your 'if' condition will always going to be true as stated in the problem. Also, your complexity isn't O(n^2), it is O(n) or O((n/3)*3). So, eventually you are doing the same thing in a different way. Cheers to different mind! :)

  • aminfara + 1 comment

    in Ruby

    puts gets.strip.each_char.with_index.map{ |c, i| (c == 'SOS'[i % 3]) ? 0 : 1 }.reduce(:+)
    
    • liahsheep + 0 comments

      some more levels of refactoring... :)

      puts gets.strip.each_char.with_index.count { |c, i| c != (i % 3 % 2 == 0 ? 'S' : 'O') }
      
  • zee1s + 0 comments

    Python

    import sys
    
    altered=0
    inp = input().strip()
    for i in range(len(inp)):
        x=i%3
        if(x==0 and inp[i]!='S'):
            altered+=1
        elif(x==1 and inp[i]!='O'):
            altered+=1
        elif(x==2 and inp[i]!='S'):
            altered+=1
    print(altered)
    
  • anubrij + 0 comments
    function main() {
        var S = readLine();
        var n = 0;
        for(var i = 0; i < S.length ; i += 3){
            if(S[i] != "S") n++;
            if(S[i + 1] != "O") n++;
            if( S[i + 2] != "S") n++;
            
        }
       console.log(n);
    }
    
  • dipak_majhi + 2 comments
    import java.io.*;
    import java.util.*;
    import java.text.*;
    import java.math.*;
    import java.util.regex.*;
    
    public class Solution {
    
        public static void main(String[] args) {
            Scanner in = new Scanner(System.in);
            String s = in.next();
            int count =0;
            for (int i =0;i<s.length();i+=3){
                if(s.charAt(i)!='S') count++;
                if(s.charAt(i+1)!='O') count++;
                if(s.charAt(i+2)!='S') count++;
            }
            System.out.println(count);
        }
    }
    
    • mndeep_kmr_shrma + 0 comments

      for(char sos : "SOS".toCharArray())

      could have written this

    • akhilgautam123 + 0 comments

      I like this solution more than the one-liners. Comparing whole SOS in one iteration.. Nice logic..

  • Pretty_Pearl + 0 comments

    You have placed a pic too? Really(?) admin, you are way too into this :D Thats good though.

  • Devendra_Narayan + 1 comment

    simple code in java

    public static void main(String[] args) {
            Scanner s= new Scanner(System.in);
            String x=s.next();
            String y="SOS";
            int l,i,cnt=0;
            l=x.length();
            l=l/3;
            for(i=0;i<l;i++)
                y=y+"SOS";
            for(i=0;i<x.length();i++)
                if(x.charAt(i)!=y.charAt(i))
                    cnt++;
            System.out.println(cnt);
        }
    
    • sinha_amrit093 + 0 comments

      good thinking bro!

  • PA
    mail_prateek_ag1 + 2 comments

    Fellow coders, any feedback?

    print(sum(1 for x, y in zip(S, 'SOS' * (len(S) // 3)) if x != y))
    
    • SebastianNielsen + 1 comment

      This was my approach:

      s = input()
      print(sum(1 for a, b in zip(s, ['S','O','S'] * int(len(s) / 3)) if a != b))
      

      It is very similiar to your solution except for the 'SOS' part, the string will get converted to a list as in my code anyway. (before getting zipped with 's') So for performance reasons, just write it as in my code from the beginning - even though the performance different in this piece of code is none existent.

      But just letting you know, since you asked for feedback.

      • PA
        mail_prateek_ag1 + 0 comments

        Thanks Sebastian. Appreciate the pointer since I am newly initiated to Python.

    • PK
      prakash_kkpr + 1 comment

      Is the input 'SRR' is not a valid one ? If it is a valid input above solution will fail, right ?

      • PA
        mail_prateek_ag1 + 0 comments

        It should not. Did you run it and saw it failing? it shoul output 2 since RR don't match OS