String

S8P1-ALTERNATING CODE



Alternating Code
It is IPL Season and the first league match of Dhilip’s favorite team, "Chennai Super Kings". The CSK team is playing at the IPL after 2 years and like all Dhoni lovers, Dhilip is also eagerly awaiting to see Dhoni back in action.

After waiting in long queues, Dhilip succeeded in getting the tickets for the big match. On the ticket, there is a letter-code that can be represented as a string of upper-case Latin letters.

Dhilip believes that the CSK Team will win the match in case exactly two different letters in the code alternate. Otherwise, he believes that the team might lose. Please see note section for formal definition of alternating code.

You are given a ticket code. Please determine, whether CSK Team will win the match or not based on Dhilip’sconviction. Print "YES" or "NO" (without quotes) corresponding to the situation.

Note:
Two letters x, y where x != y are said to be alternating in a code, if code is of form "xyxyxy...".


Input Format:
First and only line of the input contains a string S denoting the letter code on the ticket.

Output Format:
Output a single line containing "Yes" (without quotes) based on the conditions given and "No" otherwise.
Refer sample input and output for formatting specifications.


Sample Input1:
ABABAB
Sample Output1:
Yes

Sample Input2:
ABC
Sample Output2:
No

Sample Input3:
XYXYX
Sample Output3:
Yes

Solution:
import java.util.Scanner;
public class Main
{
    public static void main(String [] args)
    {
        Scanner sc=new Scanner(System.in);
        String s=sc.nextLine();
        int count=0;
        if(s.length()<3)
        System.out.println("No");
        
        if(s.charAt(0)==s.charAt(1))
        System.out.println("No");
        else
        {
        for(int i=0;i<s.length()-2;i++)
        {
            
                if(s.charAt(i)==s.charAt(i+2))
                {
                    count++;
                }
            
        }
        if(count==s.length()-2)
        System.out.println("Yes");
        else
        System.out.println("No");
        }
    }
}

S8P2-NUMBER CHALLENGE

Number Challenge
Mike set off with great zeal to the "Kracker Jack Fun Fair 2017". There were numerous activities in the fair, though Mike being a math expert, liked to participate in the Number Challenge.

Mike was given a string D of numbers containing only digits 0's and 1's. His challenge was to make the number to have all the digits same. For that, he should change exactly one digit, i.e. from 0 to 1 or from 1 to 0. If it is possible to make all digits equal (either all 0's or all 1's) by flipping exactly 1 digit then he has to output "Yes", else print "No" (without quotes).

Write a program to help Mike win over his challenge.


Input Format:
First and only input contains a string D of numbers made of only digits 1's and 0's.

Output Format:
Output “Yes" or a "No", depending on whether its possible to make it all 0s or 1s or not. 
Refer sample input and output for formatting specifications.


Sample Input1:
101
Sample Output1:
Yes

Sample Input2:
1100
Sample Output2:
No
Solution:
import java.util.Scanner;
public class Main
{
    public static void main(String[] args)
    {
        Scanner sc=new Scanner(System.in);
        String s=sc.nextLine();
        System.out.println(isSame(s)?"Yes":"No");
    }
    static boolean isSame(String s)
    {
        int zero=0,one=0;
        for(int i=0;i<s.length();i++)
        {
            char c=s.charAt(i);
            if(c=='0')
            zero++;
            else
            one++;
        }
        return(zero==1 || one==1);
    }
}

S8P4-CAPTION CONTEST

Caption Contest
Exeter Caption Contest is a competition open to all writers worldwide. The entrants will have one day to compose and submit a caption that will be based on the theme posted on the competition page.
 
Robin, a creative writer had penned two captions for the contest but he unknowingly misplaced them. After searching long, he managed to locate his captions, but some letters in them have become unreadable. The captions were in two very old sheets of paper, each of which originally contained a string of lowercase English letters. The strings on both the sheets have equal lengths.
 
Robin would like to estimate the difference between these strings. Let's assume that the first string is named S1, and the second S2. The unreadable symbols are specified with the question mark symbol '?'. The difference between the strings equals to the number of positions i, such that S1i is not equal to S2i, where S1i and S2i denote the symbol at the i th position in S1 and S2, respectively.
 
Robin would like to know the minimal and the maximal difference between the two strings, if he changes all unreadable symbols to lowercase English letters. Robin is not an expertise in programming and so he needs your help solving this problem!


Input Format:
The first line of the input contains a string S1.
The second line of the input contains a string S2.
Both strings consist of lowercase English letters and question marks in places where the symbols are unreadable.


Output Format:
Output the minimal and the maximal difference between two given strings separated with a single space.
Refer sample input and output for formatting specifications.


Sample Input1:
a?c
??b

Sample Output1:
1 3

Sample Input2:
???a
???a

Sample Output2:
0 3
Solution:
import java.util.Scanner;
public class Main
{
    public static void main(String[] args)
    {
        String name1,name2;
        int count=0,countq=0,i;
        Scanner sc=new Scanner(System.in);
        name1=sc.next();
        name2=sc.next();
        if(name1.length()==name2.length())
        {
            for(i=0;i<name1.length();i++)
            {
                if((name1.charAt(i)!='?'&&(name2.charAt(i)!='?')&&(name1.charAt(i)!=name2.charAt(i))))
                {
                    count++;
                }
            }
        
        for(i=0;i<name1.length();i++)
        {
            if((name1.charAt(i)=='?') || (name2.charAt(i)=='?'))
            {
                countq++;
            }
        }
        }
        countq=count+countq;
        System.out.println(count+" "+countq);
    }
}

S8P5-NELA'S BIRTHDAY AND COLORFULL BALLOONS

Nela's Birthday and Colorfull Balloons
Nella is an eight-year-old princess who will inherit the kingdom of Castlehaven. It is her birthday today and her Dad, the King of Castlehaven has arranged for a grand party.Nella loves colorful balloons and so her Dad planned to decorate the entire palace with balloons of the colors that Nella loved. So he asked her about her color preferences. The sophisticated princess that Nella is, she likes only two colors — amber and brass. Her Dad boughtn balloons, each of which was either amber or brass in color.
 
You are provided this information in a string s consisting of characters 'a' and 'b' only, where 'a' denotes that the balloon is amber, where 'b' denotes it being brass colored. When Nella saw the balloons, she was furious with anger as she wanted all the balloons of the same color. In her rage, she painted some of the balloons with the opposite color (i.e., she painted some amber ones brass and vice versa) to make all balloons appear to be the same color.

It took her a lot of time to do this, but you can probably show her the right way of doing so, thereby teaching her a lesson to remain calm in difficult situations, by finding out the minimum number of balloons needed to be painted in order to make all of them the same color.


Input Format:
The first and only line of input contains a string s.

Output Format:
Output a single line containing an integer — the minimum number of flips required.
Refer sample input and output for formatting specifications.

 
Sample Input1:
ab
Sample Output1:
1

Sample Input2:
baaba
Sample Output2:
2
Solution:
import java.util.Scanner;
public class Main
{
    public static void main(String[] args)
    {
        Scanner sc=new Scanner(System.in);
        String s=sc.nextLine();
        int aCount=0,bCount=0;
        for(int i=0;i<s.length();i++)
        {
            if(s.charAt(i)=='a')
            aCount++;
            else if(s.charAt(i)=='b')
            bCount++;
        }
        if(aCount<=bCount)
        System.out.println(aCount);
        else
        System.out.println(bCount);
    }
}

S8P6-LITTLE AUTHORS

Little Authors
"Little Authors" Slogan Writing Competition was organized for School students at Orchids Senior School. Any student who is creative and effective in communicating ideas in short, yet powerful about any instant topic can participate in the competition. The important guideline for the contest is that the Slogan should contain a string where the number of occurrences of some character in it is equal to the sum of the numbers of occurrences of other characters in the string. 
 
Write a program to help the event organizers to determine whether the submitted Slogans adhere to the given condition.

 
Input Format:
First and only line of input contains one string S consisting of lowercase letters.
 
Output Format:
Output a single line containing "Yes"(without quotes) if the string satisfies the condition given above or "No"(without quotes) otherwise.
Refer sample input and output for formatting specifications.


Sample Input1:
acab
Sample Output1:
Yes

Sample Input2:
abc
Sample Output2:
No
Solution:
import java.util.Scanner;
public class Main
{
    public static void main(String[] args)
    {
        String name;
        int i,j,k=0,val=0,c=1;
        int[] count;
        count=new int[50];
        Scanner sc=new Scanner(System.in);
        name=sc.nextLine();
        char[] a=name.toCharArray();
        for(i=0;i<name.length();i++)
        {
            c=1;
            for(j=i+1;j<name.length();j++)
            {
                if(name.charAt(i)==name.charAt(j) &&a[j]!='#')
                {
                    a[j]='#';
                    count[k]=c+1;
                    k++;
                    c++;
                }
            }
        }
        int max=count[0];
        for(i=1;i<k;i++)
        {
            if(max<count[i])
            {
                max=count[i];
            }
        }
        val=max+max;
        if(val==name.length())
        System.out.println("Yes");
        else
        System.out.println("No");
    }
}

ADJACENT CHARACTERS

 Adjacent characters
 
Given a string, write a program to compute a new string where identical chars that are adjacent in the original string are separated from each other by a "*"
Input and Output Format:
Refer sample input and output for formatting specifications.
All text in bold corresponds to input and the rest corresponds to output.

Sample Input and Output :
Enter the string
hello
The processed string is hel*lo

Comments

  1. can u help how to convert this java program to PYTHON

    ReplyDelete
    Replies
    1. sure! you have to use the python code for it, the logic will be same as it is in java.

      Delete
  2. Can u write c language codes for the same questions please

    ReplyDelete

Post a Comment

Popular posts from this blog

2-D Arrays

Conditional Statements