Skip to content
Programming101
Programming101

Learn everything about programming

  • Home
  • CS Subjects
    • IoT – Internet of Things
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programming101
Programming101

Learn everything about programming

HackerEarth Battle Of Words problem solution

YASH PAL, 31 July 2024
In this HackerEarth Battle Of Words problem solution, Alice and Bob are fighting over who is a superior debater. However, they wish to decide this in a dignified manner. So they decide to fight in the Battle of Words.
In each game, both get to speak a sentence. Because this is a dignified battle, they do not fight physically, the alphabets in their words do so for them. Whenever an alphabet spoken by one finds a match (same alphabet) spoken by the other, they kill each other. These alphabets fight till the last man standing.
A person wins if he has some alphabets alive while the other does not have any alphabet left.
Alice is worried about the outcome of this fight. She wants your help to evaluate the result. So kindly tell her if she wins, loses, or draws.
HackerEarth Battle Of Words problem solution

HackerEarth Battle Of Words problem solution.

#include<bits/stdc++.h>
using namespace std;

char a[1000005];
char b[1000005];

bool winner(char a[],char b[])
{int i;
int flag=1;
map<char,int> ctr;
for(i=0;a[i]!='';i++)
if(a[i]!=' ')
ctr[a[i]]++;
for(i=0;b[i]!='' && flag;i++)
{ if(b[i]==' ')
continue;
if(ctr.find(b[i])==ctr.end() || ctr[b[i]]==0)
flag=0;
else
ctr[b[i]]--;
}

bool valid=false;

if(flag)
{
for(map<char,int>::iterator it=ctr.begin();it!=ctr.end();it++)
if(it->second!=0)
valid=true;

}
if(flag && valid)
return 1;
return 0;

}


int main()
{ int t;
cin>>t;
gets(a);
while(t--)
{

gets(a);
gets(b);

if(winner(a,b))
puts("You win some.");
else if(winner(b,a))
puts("You lose some.");
else
puts("You draw some.");


}

}

Second solution

#include <bits/stdc++.h>
using namespace std ;

int T,A[26],B[26] ;
string a,b ;
int suma , sumb ;
int main(){

cin >> T ;
assert( T <= 10 && T >= 1 ) ;
getchar() ;
while(T--){
getline(cin,a) ;
getline(cin,b) ;
memset(A,0,sizeof A) ;
memset(B,0,sizeof B) ;
suma += a.length() ;
sumb += b.length() ;
assert(a.length() <= 100000 && a.length() >= 1) ;
assert(b.length() <= 100000 && b.length() >= 1) ;
for(int i=0;i<a.length();i++){
if(isalpha(a[i])){
A[a[i]-'a'] ++ ;
assert(a[i] <= 'z' && a[i] >= 'a') ;
}
}
for(int i=0;i<b.length();i++){
if(isalpha(b[i])){
B[b[i]-'a'] ++ ;
assert(b[i] <= 'z' && b[i] >= 'a') ;
}
}
for(int i=0;i<26;i++){
int x = min(A[i],B[i]) ;
A[i] -= x ;
B[i] -= x ;
}
bool ok1 = true , ok2 = false ;
for(int i=0;i<26;i++){
if(A[i]){
ok1 = false ;
}
if(B[i]){
ok2 = true ;
}
}
if(ok1 && ok2){
cout << "You lose some." << endl ;
continue ;
}
ok1 = false ;
ok2 = true ;
for(int i=0;i<26;i++){
if(A[i]){
ok1 = true ;
}
if(B[i]){
ok2 = false ;
}
}
if(ok1 && ok2){//
cout << "You win some." << endl ;
continue ;
}
cout << "You draw some." << endl ;
}/////
assert(suma <= 1000000) ;
assert(sumb <= 1000000) ;//
return 0 ;
}
coding problems

Post navigation

Previous post
Next post
  • How AI Is Revolutionizing Personalized Learning in Schools
  • GTA 5 is the Game of the Year for 2024 and 2025
  • Hackerrank Day 5 loops 30 days of code solution
  • Hackerrank Day 6 Lets Review 30 days of code solution
  • Hackerrank Day 14 scope 30 days of code solution
©2025 Programming101 | WordPress Theme by SuperbThemes