[Solved] Little Authors with Java, C++, Python

“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();
          //sauravhathi
        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++;
                    //sauravhathi
                }
            }
        }
        int max=count[0];
        for(i=1;i<k;i++)
        {
            if(max<count[i])
            {
                max=count[i];
            }
        }
        val=max+max;
          //sauravhathi
        if(val==name.length())
        System.out.println("Yes");
        else
        System.out.println("No");
    }
}
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
    string name;
    int i, j, k = 0, val = 0, c = 1;
    int count[50];
    cin >> name;
    char a[100];
    strcpy(a, name.c_str());
    for (i = 0; i < strlen(name); i++)
    {
        c = 1;
        for (j = i + 1; j < strlen(name.c_str()); j++)
        {
            //sauravhathi
            if (name[i] == name[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])
        {
            //sauravhathi
            max = count[i];
        }
    }
    val = max + max;
    if (val == strlen(name.c_str()))
        cout << "Yes";
    else
        cout << "No";
    return 0;
}
k = 0
c = 1
for i in range(len(name)):
    c = 1
    for j in range(i + 1, len(name)):
        if name[i] == name[j] and a[j] != '#':
            a[j] = '#'
            count[k] = c + 1
            k += 1
            c += 1
max = count[0]
for i in range(1, k):
    if max < count[i]:
        max = count[i]
val = max + max
if val == len(name):
    print("Yes")
else:
    print("No")

Happy Learning – If you require any further information, feel free to contact me.

Share your love
Saurav Hathi

Saurav Hathi

I'm currently studying Bachelor of Computer Science at Lovely Professional University in Punjab.

๐Ÿ“Œ Nodejs and Android ๐Ÿ˜Ž
๐Ÿ“Œ Java

Articles: 444

Leave a Reply

Your email address will not be published. Required fields are marked *