# Given two strings check which string makes a palindrome first

Given two strings ‘A’ and ‘B’ of equal length. Two players play a game where they both pick a character from their respective strings (First picks from A and second from B) and put into a third string (which is initially empty). The player that can make the third string palindrome, is winner. If first player makes palindrome first then print ‘A’, else ‘B’. If strings get empty and no one is able to make a palindrome, then print ‘B’.
Examples:

```Input : A = ab
B = ab
Output : B
First player puts 'a' (from string A)
Second player puts 'a' (from string B)
which make palindrome.
The result would be same even if A picks
'b' as first character.

Input : A = aba
B = cde
Output : A

Input : A = ab
B = cd
Output : B
None of the string will be able to
make a palindrome (of length > 1)
in any situation. So B will win.
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

After taking few examples, we can observe that ‘A’ (or first player) can only win when it has a character that appears more than once and not present in ‘B’.

## C++

 `// Given two strings, check which string ` `// makes palindrome first. ` `#include ` `using` `namespace` `std; ` ` `  `const` `int` `MAX_CHAR = 26; ` ` `  `// returns winner of two strings ` `char` `stringPalindrome(string A, string B) ` `{ ` `    ``// Count frequencies of characters in ` `    ``// both given strings ` `    ``int` `countA[MAX_CHAR] = {0}; ` `    ``int` `countB[MAX_CHAR] = {0}; ` `    ``int` `l1 = A.length(), l2 = B.length(); ` `    ``for``(``int` `i=0; i1 && countB[i] == 0)) ` `           ``return` `'A'``; ` ` `  `    ``return` `'B'``; ` `} ` ` `  `// Driver Code ` `int` `main() ` `{ ` `    ``string a = ``"abcdea"``; ` `    ``string b = ``"bcdesg"``; ` `    ``cout << stringPalindrome(a,b); ` `    ``return` `0; ` `} `

## Java

 `// Java program to check which string ` `// makes palindrome first. ` `public` `class` `First_Palin { ` ` `  `    ``static` `final` `int` `MAX_CHAR = ``26``; ` ` `  `    ``// returns winner of two strings ` `    ``static` `char` `stringPalindrome(String A, String B) ` `    ``{ ` `        ``// Count frequencies of characters in ` `        ``// both given strings ` `        ``int``[] countA = ``new` `int``[MAX_CHAR]; ` `        ``int``[] countB = ``new` `int``[MAX_CHAR]; ` ` `  `        ``int` `l1 = A.length(); ` `        ``int` `l2 = B.length(); ` `         `  `        ``for` `(``int` `i = ``0``; i < l1; i++) ` `            ``countA[A.charAt(i) - ``'a'``]++; ` `         `  `        ``for` `(``int` `i = ``0``; i < l2; i++) ` `            ``countB[B.charAt(i) - ``'a'``]++; ` ` `  `        ``// Check if there is a character that ` `        ``// appears more than once in A and does ` `        ``// not appear in B ` `        ``for` `(``int` `i = ``0``; i < ``26``; i++) ` `            ``if` `((countA[i] > ``1` `&& countB[i] == ``0``)) ` `                ``return` `'A'``; ` ` `  `        ``return` `'B'``; ` `    ``} ` ` `  `    ``// Driver Code ` `public` `static` `void` `main(String args[]) ` `    ``{ ` `        ``String a = ``"abcdea"``; ` `        ``String b = ``"bcdesg"``; ` `        ``System.out.println(stringPalindrome(a, b)); ` `    ``} ` `} ` `// This code is contributed by Sumit Ghosh `

## Python3

 `# Given two strings, check which string ` `# makes palindrome first. ` ` `  `MAX_CHAR ``=` `26` ` `  `# returns winner of two strings ` `def` `stringPalindrome(A, B): ` `     `  `    ``# Count frequencies of characters  ` `    ``# in both given strings ` `    ``countA ``=` `[``0``] ``*` `MAX_CHAR ` `    ``countB ``=` `[``0``] ``*` `MAX_CHAR ` `    ``l1 ``=` `len``(A) ` `    ``l2 ``=` `len``(B) ` `    ``for` `i ``in` `range``(l1): ` `        ``countA[``ord``(A[i]) ``-` `ord``(``'a'``)] ``+``=` `1` `    ``for` `i ``in` `range``(l2): ` `        ``countB[``ord``(B[i]) ``-` `ord``(``'a'``)] ``+``=` `1` ` `  `    ``# Check if there is a character that ` `    ``# appears more than once in A and  ` `    ``# does not appear in B ` `    ``for` `i ``in` `range``(``26``): ` `        ``if` `((countA[i] > ``1` `and` `countB[i] ``=``=` `0``)): ` `            ``return` `'A'` `    ``return` `'B'` ` `  `# Driver Code ` `if` `__name__ ``=``=` `'__main__'``: ` `    ``a ``=` `"abcdea"` `    ``b ``=` `"bcdesg"` `    ``print``(stringPalindrome(a, b)) ` ` `  `# This code is contributed by Rajput-Ji `

## C#

 `// C# program to check which string ` `// makes palindrome first. ` `using` `System; ` ` `  `class` `First_Palin { ` ` `  `    ``static` `int` `MAX_CHAR = 26; ` ` `  `    ``// returns winner of two strings ` `    ``static` `char` `stringPalindrome(``string` `A, ``string` `B) ` `    ``{ ` `        ``// Count frequencies of characters in ` `        ``// both given strings ` `        ``int``[] countA = ``new` `int``[MAX_CHAR]; ` `        ``int``[] countB = ``new` `int``[MAX_CHAR]; ` ` `  `        ``int` `l1 = A.Length; ` `        ``int` `l2 = B.Length; ` `         `  `        ``for` `(``int` `i = 0; i < l1; i++) ` `            ``countA[A[i] - ``'a'``]++; ` `         `  `        ``for` `(``int` `i = 0; i < l2; i++) ` `            ``countB[B[i] - ``'a'``]++; ` ` `  `        ``// Check if there is a character that ` `        ``// appears more than once in A and does ` `        ``// not appear in B ` `        ``for` `(``int` `i = 0; i < 26; i++) ` `            ``if` `((countA[i] > 1 && countB[i] == 0)) ` `                ``return` `'A'``; ` ` `  `        ``return` `'B'``; ` `    ``} ` ` `  `    ``// Driver Code ` `    ``public` `static` `void` `Main() ` `    ``{ ` `        ``string` `a = ``"abcdea"``; ` `        ``string` `b = ``"bcdesg"``; ` `    ``Console.WriteLine(stringPalindrome(a, b)); ` `    ``} ` `} ` ` `  `// This code is contributed by vt_m. `

## PHP

 ` 1 && ``\$countB``[``\$i``] == 0)) ` `        ``return` `'A'``; ` ` `  `    ``return` `'B'``; ` `} ` ` `  `    ``// Driver Code ` `    ``\$a` `= ``"abcdea"``; ` `    ``\$b` `= ``"bcdesg"``; ` `    ``echo` `stringPalindrome(``\$a``,``\$b``); ` `     `  `// This code is contributed by mits ` `?> `

Output:

```A
```

This article is contributed by Akshay Rajput. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up

Improved By : vt_m, Rajput-Ji, Mithun Kumar

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.