Saturday 31 December 2016

MARS EXPLORATION


MARS EXPLORATION:

Sami's spaceship crashed on Mars! She sends  sequential SOS messages to Earth for help.
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.
Input Format
There is one line of input: a single string, .
Note: As the original message is just SOS repeated  times, 's length will be a multiple of .
Constraints
  •  will contain only uppercase English letters.
Output Format
Print the number of letters in Sami's message that were altered by cosmic radiation.
Sample Input 0
SOSSPSSQSSOR
Sample Output 0
3
Sample Input 1
SOSSOT
Sample Output 1
1
Explanation
Sample 0
 = SOSSPSSQSSOR, and signal length . Sami sent  SOS messages (i.e.: ).
Expected signal: SOSSOSSOSSOS
Recieved signal: SOSSPSSQSSOR
We print the number of changed letters, which is .
Sample 1
 = SOSSOT, and signal length . Sami sent  SOS messages (i.e.: ).
Expected Signal: SOSSOS 
Received Signal: SOSSOT
We print the number of changed letters, which is .


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();//taking input string
        int n=S.length();  //getting length
        int r=n/3;  //we divide string by length of "SOS" string i.e. 3.
        String w="SOS";
        String q="SOS";
        for(int i=0;i<r-1;i++)
            {
          q=  q.concat(w);//making original string of "SOS" that was send by her
        }
        int count=0;
       for(int j=0;j<q.length();j++)
           {
            if(S.charAt(j)!=q.charAt(j))//comparing original string and the recieved string .
                count++;   //counting number of errors
         
             
        }
        System.out.println(count);//printing errors
    }
}

Wednesday 28 December 2016

Printing Star ,Slash Using wait(),notify() Method

Source Code:

import java.util.*;
class star  //class containing synchronized methods
{
int ch=0;

synchronized void printstar()
{
try{
                       if(ch==1)//star was printed last
{
wait();
}
System.out.print("*");
ch=1;
notify();
}catch(InterruptedException e){System.out.println("error");
 }
}
synchronized  void printslash()
{
try{ if(ch==0)//slash was printed previously
{
wait();
}

          System.out.print("/");
ch=0;//slash printed
notify();
}catch(InterruptedException e){};


}
}


class printx extends Thread  //for printing star
{
star r,s;
printx(star r)
{
this.r=r;
}

public void run()
{
int f=0;
while(f!=10)
{
r.printstar();
f++;
}
}
}

class print extends Thread///for printing slash
{
star r,s;
print(star r)
{
this.r=r;
}

public void run()
{
int f=0;
while(f!=15)
{
r.printslash();
f++;
}
}
}
class Demo
{
public static void main(String args[])
{
star q=new star();
print  p=new print(q);
printx px=new printx(q);
p.start();//thread 1 starts
px.start();//thread 2 starts
}

}



output:*/*/*/*/*/*/*/*/

explanation:we use synchronization here by using wait() and notify() method .
                     when a star is printed we make ch=1.
                      when a slsh is printed we make ch=0.