Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.ivanasen.playground;
- import java.math.BigInteger;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Scanner;
- public class Emojis {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- String input = scanner.nextLine();
- solve(input);
- }
- public static void solve(String input) {
- BigInteger threshold = findCoolnessThreshold(input);
- System.out.println("Cool threshold: {" + threshold + "}");
- List<String> emojis = findEmojis(input);
- System.out.println(emojis.size() + " emojis found in the text. The cool ones are:");
- emojis.forEach(emoji -> {
- BigInteger emojiCoolness = findCoolnessOfEmoji(emoji);
- if (emojiCoolness.compareTo(threshold) >= 0) {
- System.out.println(emoji);
- }
- });
- }
- public static BigInteger findCoolnessOfEmoji(String emoji) {
- BigInteger result = BigInteger.ONE;
- for (int i = 2; i < emoji.length() - 2; i++) {
- BigInteger charValue = BigInteger.valueOf(emoji.charAt(i));
- result = result.add(charValue);
- }
- return result;
- }
- public static BigInteger findCoolnessThreshold(String input) {
- BigInteger result = BigInteger.ONE;
- for (int i = 0; i < input.length(); i++) {
- char c = input.charAt(i);
- if (Character.isDigit(c)) {
- result = result.multiply(new BigInteger(c + ""));
- }
- }
- return result;
- }
- public static List<String> findEmojis(String input) {
- List<String> emojis = new ArrayList<>();
- int currentStart = -1;
- for (int i = 0; i < input.length(); i++) {
- char c = input.charAt(i);
- if (currentStart == -1) {
- if (c == '*' || c == ':') {
- if (i < input.length() - 1 && input.charAt(i + 1) == c) {
- currentStart = i;
- }
- i++;
- }
- } else {
- if (c == '*' || c == ':') {
- if (input.charAt(i - 1) == c) {
- if (isValidEmoji(input, currentStart, i + 1)) {
- emojis.add(input.substring(currentStart, i + 1));
- }
- } else if (i < input.length() - 1 && input.charAt(i + 1) == c) {
- if (isValidEmoji(input, currentStart, i + 2)) {
- emojis.add(input.substring(currentStart, i + 2));
- }
- i++;
- }
- currentStart = -1;
- }
- }
- }
- return emojis;
- }
- public static boolean isValidEmoji(String s, int start, int end) {
- if (end - start < 7) {
- return false;
- }
- char usedChar = s.charAt(start);
- if (usedChar != '*' && usedChar != ':') {
- return false;
- }
- if (s.charAt(start + 1) != usedChar) {
- return false;
- }
- char capital = s.charAt(start + 2);
- if (!Character.isLetter(capital) || !Character.isUpperCase(capital)) {
- return false;
- }
- int innerCount = 1;
- for (int i = start + 3; i < end - 2; i++) {
- char c = s.charAt(i);
- if (!Character.isLetter(c) || !Character.isLowerCase(c)) {
- return false;
- }
- innerCount++;
- }
- return innerCount >= 3 && s.charAt(end - 1) == usedChar && s.charAt(end - 2) == usedChar;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement