Only one letter#8979

Only one letter (Editorial)

Algorithm Analysis

Read the input string. Count the number of letters a in it. If it equals 0, then output -1. Otherwise, output as many letters a as there are in the input string.

Algorithm Implementation

Read the input string.

getline(cin, s);

In the variable cnt, count the number of letters a.

cnt = 0;
for (i = 0; i < s.size(); i++)
    if (s[i] == 'a') cnt++;

Depending on the value of cnt, output the answer.

if (cnt == 0) cout << "-1" << endl;
else cout << string(cnt, 'a') << endl;

Java Implementation

import java.util.*;

public class Main
{
    public static void main(String[] args)
    {
        Scanner con = new Scanner(System.in);
        String s = con.nextLine();
        int flag = 0;
        for (int i = 0; i < s.length(); i++)
            if (s.charAt(i) == 'a')
            {
                System.out.print(s.charAt(i));
                flag = 1;
            }
        if (flag == 0) System.out.print("-1");
        System.out.println();
        con.close();
    }
}

Python Implementation

s = input()
cnt = s.count('a')
if cnt == 0:
    print('-1')
else:
    print('a' * cnt)