FRom hackerrank


Letters in some of the SOS messages are altered by cosmic radiation during transmission. Given the signal received by Earth as a string, , determine how many letters of Sami's SOS have been changed by radiation.
For example, Earth receives SOSTOT. Sami's original message was SOSSOS. Two of the message characters were changed in transit.

Solution in C# -

class Solution {
    static int marsExploration(string s) {
        int count = 0;
        for(int i=0; i<s.Length; i+=3)
        {
            int x = i;
            if(!(s[x] == 'S'))
                count++;
            if(!(s[x+1] == 'O'))
                count++;
            if(!(s[x+2] == 'S'))
                count++;           
        }
        return count;
    }
    static void Main(string[] args) {
        TextWriter textWriter = new StreamWriter(@System.Environment.GetEnvironmentVariable("OUTPUT_PATH"), true);
        string s = Console.ReadLine();
        int result = marsExploration(s);
        textWriter.WriteLine(result);
        textWriter.Flush();
        textWriter.Close();
    }
}

Post a Comment

Previous Post Next Post