We are given a problem: count the number of pairs (i,j) with i<j such that S[i] = reverse(S[j]).
We need to output the count.
Constraints: N up to 100,000, each string length up to 10, only alphabets (upper/lower case). So strings are case-sensitive.
We can just iterate over strings, for each string compute its reverse, and use a map/dictionary to count occurrences of each string, but we need pairs i<j. A common approach: as we traverse from left to right, we can keep a count of strings we have seen, but careful: for each new string s, we need to count how many previous strings equal reverse(s)? Actually the condition is S_i = reverse(S_j) with i<j. So for each j, we can count number of i<j such that S_i = reverse(S_j). So we can maintain a frequency map of strings as we go, and for each string s at index j, add freq[reverse(s)] to answer. Then increment freq[s] after counting.
But note that strings can be palindromes themselves: If s equals reverse(s), then careful: the pairs (i,j) where both are palindrome strings (i.e., s is palindrome) and we have condition S_i = reverse(S_j). For palindrome strings, reverse(s) = s, so we need to count pairs where both strings are equal and also s is palindrome? Wait, condition: S_i = reverse(S_j). If S_i = s and S_j = s, then reverse(S_j) = reverse(s) = s if s palindrome. Actually condition is: S_i == reverse(S_j). So if s is palindrome, then reverse(s)=s, so the condition reduces to S_i == s and S_j == s, i.e., both strings equal to s. So pairs (i,j) where both are s and i<j count. But if s is not palindrome, then we need distinct strings: one is s, the other is reverse(s). So our counting method works: For each j, we look up frequency of reverse(s) among previous indices. That counts all i<j with S_i = reverse(s). This includes cases where reverse(s) might be equal to s (if palindrome). So that's correct.
But careful: Are there any duplicates? For example, N=3: strings: "bba", "abb", "abb". Our method: start freq empty, ans=0.
j=0: s="bba", rev="abb". freq["abb"]=0 -> ans+=0. freq["bba"]=1.
j=1: s="abb", rev="bba". freq["bba"]=1 -> ans+=1. freq["abb"]=1.
j=2: s="abb", rev="bba". freq["bba"]=1 -> ans+=1. freq["abb"]=2.
Total ans=2. That's correct.
Edge cases: strings could be of length up to 10, so reverse is cheap. We'll use a hash map (dictionary) for frequencies. Since strings are only up to 10 characters, we could also use an array representation, but dictionary is fine.
Complexity: O(N * L) time, where L <=10, and O(N) space.
But wait: The problem statement says: "Panda has to select the Palin Pairs from the given strings . A Palin Pair is defined as : (i,j) is a Palin Pair if Si = reverse(Sj) and i < j". So it's exactly that.
Hence solution is straightforward.
But we must be careful about reading input: first line N, then N lines of strings. Output integer.
Example given: N=3, strings: bba, abb, abb output 2.
Now let's think if there is any twist: Might be case sensitivity, and strings consist of upper and lower case alphabets only. So treat as exact.
Constraints: N up to 100,000, so using dictionary is fine.
Therefore, we can produce code.
But this is a typical problem, perhaps from hackerearth. Usually they want to avoid double counting? But we count i<j. So we can either use two passes: count frequencies first and then compute combinations? But careful: If we count frequencies first, then we could compute pairs as freq[s] * freq[reverse(s)] for each distinct string, but that would count both ij, and also include i=j? Actually if we consider distinct strings, we could compute total pairs as sum over s ( freq[s] * freq[reverse(s)] ) for s <= reverse(s) to avoid double counting, but then we must subtract self pairs for palindromic strings? Let's think: For non-palindromic s and t = reverse(s) distinct, the condition requires i<j and S_i = s, S_j = t? Actually condition is S_i = reverse(S_j). For each pair (i,j) with i<j and S_i = reverse(S_j). This includes both cases where the first is s and second is reverse(s) and also where first is reverse(s) and second is s? Let's check: If we have s and t such that t = reverse(s) (and s != t), then for a pair where S_i = s and S_j = t, we have s = reverse(t)? Because reverse(t) = reverse(reverse(s)) = s, so indeed S_i = s = reverse(t) = reverse(S_j). So such a pair qualifies. Similarly, a pair where S_i = t and S_j = s, we have t = reverse(s) = reverse(S_j), so that also qualifies? Check condition: For (i,j) we need i<j. So (i,j) with i<j, S_i = t, S_j = s: then S_i = t, reverse(S_j)=reverse(s)=t. So yes, it qualifies. So both orders count as long as i<j. So in a multiset of strings, any unordered pair of indices (i,j) with i<j and (S_i, S_j) being either (s, reverse(s)) or (reverse(s), s) will satisfy condition? Actually condition requires S_i = reverse(S_j). So for a pair of strings (a,b), the condition is that a = reverse(b). So it's directed: the first element must equal reverse of the second. So given two distinct strings s and t where t = reverse(s), we have:
- If we take i with s, j with t: s = reverse(t) => qualifies if i<j.
- If we take i with t, j with s: t = reverse(s) => qualifies if i<j.
So both directed pairs qualify, but only when i<j. So in an unordered pair of indices, if the two strings are s and t such that t = reverse(s), then exactly one of the two directed pairs satisfies i<j, depending on the order of indices. Actually if we have two indices i and j with i<j, there is only one ordering: the earlier index is i, later is j. So given two positions, they either satisfy S_i = reverse(S_j) or not. For two positions with strings s and t, where s and t are reverse of each other, it's possible that the earlier string is s and later is t, then S_i = s, S_j = t, we check if s = reverse(t)? Since reverse(t) = reverse(reverse(s)) = s, yes that holds. If earlier is t and later is s, then we check if t = reverse(s) which is true. So in either case, the pair (i,j) will satisfy the condition! Because whichever string appears first, it equals reverse of the second. Let's verify: Suppose s = "ab", t = reverse(s) = "ba". Then pair (i with "ab", j with "ba") -> "ab" = reverse("ba")? reverse("ba") = "ab", yes. Pair (i with "ba", j with "ab") -> "ba" = reverse("ab")? reverse("ab") = "ba", yes. So indeed any unordered pair of indices where one string is the reverse of the other will always satisfy the condition regardless of order. That's interesting. So if we have counts c_s and c_t for s and t (with t = reverse(s), s != t), then the number of ordered pairs (i<j) that satisfy condition is exactly c_s * c_t. Because for any i from group s and j from group t, if i < j, we count? But careful: When we count all pairs where one index from s and another from t, we are counting both i in s, j in t and i in t, j in s. But we must only count i<j. However, as argued, any such unordered pair (i from s, j from t) will satisfy the condition regardless of order, but does it satisfy i<j? That depends on which index is smaller. If we count all unordered pairs across groups, each unordered pair corresponds to exactly one ordered pair with i<j (the one where the smaller index is i). So the number of ordered pairs (i<j) from two groups is indeed c_s * c_t. Because each combination of one index from group s and one from group t yields exactly one pair (i<j) - but we need to be careful: if we take an index from s and an index from t, there are c_s * c_t possible pairs, but each such pair has a specific ordering: whichever index is smaller. So it's indeed a valid count of (i<j) pairs. But does it double count? For each unordered pair, there is exactly one (i<j) ordering. So the total number of (i<j) pairs where the two strings are reverse of each other is c_s * c_t. So that's consistent.
Now consider s where s = reverse(s) (palindrome). Then condition reduces to S_i = S_j and also i<j, because S_i = reverse(S_j) = S_j. So we need pairs of indices with same palindrome string. The number of (i<j) pairs from that group is C(c_s, 2) = c_s*(c_s-1)/2.
Thus total answer = sum over all distinct strings s such that s < reverse(s) (lexicographically?) we could avoid double counting: For non-palindromic s, count c_s * c_rev(s), and for palindromic s, count C(c_s, 2). But careful: When iterating over s, we need to ensure we don't count same pair twice. The safe way is to only count when s <= reverse(s) (or use a set to mark visited). Actually if s != reverse(s), we would count c_s * c_rev(s) once. If s == reverse(s), we count C(c_s, 2). This is an alternative method that doesn't require scanning order, just frequencies.
But the simple forward count method (maintaining frequency and adding freq[reverse(s)] before increment) yields the same answer and is easier to implement. Let's verify: For palindrome strings, when we encounter a new occurrence of s, we add freq[reverse(s)] which is freq[s] (since reverse(s)=s). So we add previous count of s. So total sum over all occurrences will be sum over all previous counts = total number of pairs (i<j) with that string. That sum equals C(c_s, 2). For non-palindromic strings, when we process an occurrence of s, we add freq[reverse(s)]; when we process an occurrence of reverse(s), we add freq[s]. The total across all occurrences will be c_s * c_rev(s) because each unordered pair will be counted exactly once? Let's see: Suppose we have c_s = a, c_t = b, t = reverse(s). Process all strings in order. For each occurrence of s, we add freq[t] at that moment (which counts previous t's). For each occurrence of t, we add freq[s] (previous s's). So total = sum over all s occurrences of (number of t before it) + sum over all t occurrences of (number of s before it). This equals the total number of (i<j) pairs where one is s and other is t. Because any unordered pair (i from s, j from t) with i<j: if the s index is earlier, then it will be counted when processing the later t? Let's analyze: suppose we have indices i (s) and j (t) with i<j. When we process j (the t), we add freq[s] which includes i (since i < j and i is s). So that pair counted exactly once. Similarly, if i (t) and j (s) with i<j, it will be counted when processing j (s) adding freq[t] which includes i. So all unordered pairs are counted exactly once. So total sum = a * b. So the forward count method yields correct answer. It's O(N) time and O(distinct strings) memory.
Thus we can implement.
Now note: Input size: N up to 100k, strings length up to 10, so we can store them and reverse. No issues.
Implementation details:
Read integer N (maybe from input()).
Initialize ans = 0, freq = defaultdict(int).
Loop for each string s (read strip). Compute rev = s[::-1]. ans += freq[rev]. Then freq[s] += 1.
Print ans.
We need to handle large N, but Python can handle.
Edge Cases: N=1, ans=0.
Case: N=2, strings: "a", "a". s1="a", rev="a". ans += freq["a"]=0 -> ans=0. Then freq["a"]=1. Next s2="a", rev="a". ans += freq["a"]=1 -> ans=1. freq["a"]=2. Output 1. Which is C(2,2)=1. Good.
Case: "ab", "ba". j0: s="ab", rev="ba", freq["ba"]=0 -> ans=0; freq["ab"]=1. j1: s="ba", rev="ab", freq["ab"]=1 -> ans=1; freq["ba"]=1. Output 1 = 1*1. Good.
Case: "ab", "ab", "ba". j0: "ab": rev "ba", freq["ba"]=0 ans0; freq["ab"]=1. j1: "ab": rev "ba", freq["ba"]=0 ans0; freq["ab"]=2. j2: "ba": rev "ab", freq["ab"]=2 ans2; freq["ba"]=1. Total ans=2. Unordered pairs: there are indices: (0,2) and (1,2) = 2. Good.
Thus algorithm works.
Now complexity: O(N * L) where L <=10, plus dictionary overhead. Good.
Now we just need to produce final answer.
But let's check the problem statement again: "Panda has a thing for palindromes. Hence he was a given a problem by his master. The master will give Panda an array of strings S having N strings. Now Panda has to select the Palin Pairs from the given strings. A Palin Pair is defined as: (i,j) is a Palin Pair if Si = reverse(Sj) and i < j" So it's exactly that.
We output number of such pairs.
So answer is straightforward.
We might also consider a solution using sorting and two-pointer but not needed.
Now implement.
Potential pitfalls: Input might contain spaces? No.
Now test with given sample.
SAMPLE INPUT:
3
bba
abb
abb
Our code: ans = 2. Good.
Now large constraints: N=100k, strings length 10, dictionary size up to N. Fine.
Time: For each string, compute reverse, dictionary lookup/increment. That is fine.
Now final code:
import sys
from collections import defaultdict
def main():
data = sys.stdin.read().split()
if not data: return
n = int(data[0])
strings = data[1:1+n]
freq = defaultdict(int)
ans = 0
for s in strings:
rev = s[::-1]
ans += freq[rev]
freq[s] += 1
print(ans)
if name == "main":
main()
But careful: The input might have more than N strings? The data read includes all tokens; if there are extra lines, but specification says exactly N strings. So it's fine.
Alternatively, we could use sys.stdin.readline() loop. Both fine.
Now ensure we handle large output (ans might be up to N*(N-1)/2 ~ 5e9, which fits in Python int).
Now we can produce final answer.
Thus solution.
Now, as the user asked to "Write the code in python3". So we output the code.
But we need to provide reasoning? The chat asked: "Write a program to solve the problem." Possibly just code. But also might need explanation. However typical answer: provide solution description and code. I'll include explanation and code.
But the conversation: The user gave problem statement and sample. They want a solution. So I'll provide a clear answer with explanation and final code.
Let's structure:
-
Explanation of approach: we maintain a frequency map of strings seen so far. For each new string, we add the count of its reverse from previous strings, because those form valid pairs (i<j). Then increment its count.
-
Complexity: O(N * L) time, O(N) space.
-
Implementation details.
-
Provide code.
Now finalize. We can solve this by scanning the strings in order and using a frequency map. For each string s, we compute its reverse rev. The number of previous strings that are equal to rev gives the number of pairs where the current index j is the second element and some earlier index i satisfies S_i = reverse(S_j). We add this count to the answer and then increment the frequency of s itself. This way each valid pair (i, j) is counted exactly once when processing j.
The length of each string is at most 10, so reversing is cheap. The algorithm runs in O(N * L) time and O(N) memory.
import sys
from collections import defaultdict
def main():
data = sys.stdin.read().split()
if not data:
return
n = int(data[0])
strings = data[1:1 + n]
freq = defaultdict(int)
ans = 0
for s in strings:
rev = s[::-1]
ans += freq[rev]
freq[s] += 1
print(ans)
if __name__ == "__main__":
main()