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.
Example:
Input: s = "i will win", s1 = "will i"
Output: You win some.
Approach
C++
#include <bits/stdc++.h>using namespace std;void battleOfWords(string s, string s1){int i = 0;int n = s.size(), m = s1.size();int f[26] = {0};int f1[26] = {0};int cnt1 = 0, cnt2 = 0;for (int i = 0; i < n; i++){if (s[i] != ' '){f[s[i] - 'a']++;}}for (int i = 0; i < m; i++){if (s1[i] != ' '){f1[s1[i] - 'a']++;}}for (int i = 0; i < 26; i++){if (f[i] > f1[i]){f[i] = f[i] - f1[i];f1[i] = 0;}else{f1[i] = f1[i] - f[i];f[i] = 0;}}sort(f, f + 26);sort(f1, f1 + 26);if (f[25] == 0 && f1[25] != 0)cout << "You lose some.\n";else if (f1[25] == 0 && f[25] != 0)cout << "You win some.\n";elsecout << "You draw some.\n";}int main(){string s = "i will win";string s1 = "will i";battleOfWords(s, s1);return 0;}
No comments:
Post a Comment