Codeforces 734A Anton and Danik Solution & Explanation

Difficulty : 800

Problem Description

Anton likes to play chess, and so does his friend Danik.

Once they have played n games in a row. For each game it’s known who was the winner — Anton or Danik. None of the games ended with a tie.

Now Anton wonders, who won more games, he or Danik? Help him determine this.

Input

The first line of the input contains a single integer n (1 ≤ n ≤ 100 000) — the number of games played.

The second line contains a string s, consisting of n uppercase English letters ‘A’ and ‘D’ — the outcome of each of the games. The i-th character of the string is equal to ‘A’ if the Anton won the i-th game and ‘D’ if Danik won the i-th game.

Output

If Anton won more games than Danik, print “Anton” (without quotes) in the only line of the output.

If Danik won more games than Anton, print “Danik” (without quotes) in the only line of the output.

If Anton and Danik won the same number of games, print “Friendship” (without quotes).

Eamples

Input6
ADAAAA
OutputAnton
Input7
DDDAADA
OutputDanik
Input6
DADADA
OutputFriendship

Solution

C# Solution

Solution1

int n = int.Parse(Console.ReadLine());
string text = Console.ReadLine();

int aCnt = 0;
int dCnt = 0;

foreach(var c in text){
    if(c=='A')
    {
        aCnt+=1;
    }
    else
    {
        dCnt+=1;
    }
    
}

string res = "Friendship";

if(aCnt>dCnt){
    res = "Anton";
}
else if(dCnt>aCnt)
{
    res = "Danik";
}

Console.WriteLine(res);

This code takes input for the number of games n and a string text representing the outcomes of each game (either ‘A’ for Anton or ‘D’ for Danik). It then iterates through each character in the string, counting the occurrences of ‘A’ and ‘D’ in variables aCnt and dCnt.

Solution2 – use one variable

int n = int.Parse(Console.ReadLine());
string text = Console.ReadLine();

int ans = 0;

foreach(var c in text){
    if(c=='A')
    {
        ans-=1;
    }
    else
    {
        ans+=1;
    }
    
}

string res = "Friendship";

if(ans<0){
    res = "Anton";
}
else if(ans>0)
{
    res = "Danik";
}

Console.WriteLine(res);

This approach is more concise as it utilizes a single variable (ans) to track the difference, eliminating the need for two separate counters.

Java Solution

Solution1

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String n = scanner.nextLine(); //no use    
        String text = scanner.nextLine();
        int ans = 0;
        
        for (char c : text.toCharArray()) {
            if(c=='A')
            {
                ans-=1;
            }
            else
            {
                ans+=1;
            }
        }
        
        String res = "Friendship";

        if(ans<0){
            res = "Anton";
        }
        else if(ans>0)
        {
            res = "Danik";
        }
        
        System.out.print(res);
        
    }
}

Python3 Solution

Solution1

n = int(input())
text = input()

ans = 0

for c in text:
    if(c=='A'):
        ans-=1
    else:
        ans+=1

res = "Friendship"

if(ans<0):
    res = "Anton"
    
elif(ans>0):
    res = "Danik"

print(res)

JavaScript Solution

Solution1

var n = parseInt(readline());
var text = readline();

var ans = 0

for(var c of text) {
    if(c=='A'){
        ans-=1;
    }
    else{
        ans+=1;
    }
}

res = "Friendship";

if(ans<0){
    res = "Anton";
}
    
else if(ans>0){
    res = "Danik";
}

print(res);

Conclusion

🧡If my solution helps, that is my honor!

🧡You can support me by sharing my posts, thanks you~~

✅If you got any problem about the explanation or you need other programming language solution, please feel free to let me know !!

The problem link : Problem – 734A – Codeforces

Random Codeforces Posts

Leave a Reply

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