Skip to content
Programmingoneonone
Programmingoneonone
  • Engineering Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
    • 100+ C++ Programs
  • Solutions
    • HackerRank
      • Algorithms Solutions
      • C solutions
      • C++ solutions
      • Java solutions
      • Python solutions
    • Leetcode Solutions
    • HackerEarth Solutions
  • Work with US
Programmingoneonone
Programmingoneonone

HackerEarth Battle Of Words problem solution

YASH PAL, 31 July 202413 February 2026
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 solutions HackerEarth HackerEarth

Post navigation

Previous post
Next post

Leave a Reply

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

Programmingoneonone

We at Programmingoneonone, also known as Programming101 is a learning hub of programming and other related stuff. We provide free learning tutorials/articles related to programming and other technical stuff to people who are eager to learn about it.

Pages

  • About US
  • Contact US
  • Privacy Policy

Practice

  • Java
  • C++
  • C

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2026 Programmingoneonone | WordPress Theme by SuperbThemes