Given a string, find the number of pairs of characters that are same. Pairs (s[i], s[j]), (s[j], s[i]), (s[i], s[i]), (s[j], s[j]) should be considered different.
Example 1:
Input:
S = "air"
Output: 3
Explanation: 3 pairs that are equal:
(S[0], S[0]), (S[1], S[1]) and (S[2], S[2])
Ã¢â¬â¹Example 2:
Input: 
S = "aa"
Output: 4
Explanation: 4 pairs that are equal:
(S[0], S[0]), (S[0], S[1]), (S[1], S[0])
and (S[1], S[1])
Your Task:
You don't need to read input or print anything. Your task is to complete the function equalPairs() which takes the string S as input and returns the number of equal pairs as described in the problem description.
Expected Time Complexity: O(|S|).
Expected Auxiliary Space: O(1).
Constraints:
1<=|S|<=10^{5}

Source: geeksforgeeks | Difficulty: EASY
Tags: String algorithms

Data structures

Starter code: #User function Template for python3

class Solution:
    def equalPairs (self,s):
        # your code here

Solutions:
class Solution:

	def equalPairs(self, s):
		result = 0
		d = {}
		for i in s:
			d[i] = d.get(i, 0) + 1
		for i in d.values():
			result += i * i
		return result


class Solution:

	def equalPairs(self, s):
		dic = {}
		for i in s:
			if i in dic:
				dic[i] += 1
			else:
				dic[i] = 1
		c = 0
		for i in dic:
			if dic[i] > 1:
				c += dic[i] ** 2
			else:
				c += 1
		return c


from collections import Counter

class Solution:

	def equalPairs(self, s):
		d = Counter(s)
		prd = 0
		for (key, value) in d.items():
			prd = prd + value * value
		return prd


class Solution:

	def equalPairs(self, s):
		arr = [0] * 26
		pair = 0
		dpair = 0
		for i in range(len(s)):
			if s[i] in s:
				arr[ord(s[i]) - 97] += 1
		for j in arr:
			if j <= 1:
				dpair += j
			else:
				pair += j * j
		return dpair + pair


class Solution:

	def equalPairs(self, s):
		num = 0
		l = ''
		for i in s:
			if i not in l:
				num1 = s.count(i)
				num = num1 ** 2 + num
				l = l + i
		return num


class Solution:

	def equalPairs(self, s):
		d = {}
		for i in s:
			if i not in d:
				d[i] = 0
			d[i] += 1
		ans = 0
		for k in d:
			ans += d[k] * d[k]
		return ans


class Solution:

	def equalPairs(self, s):
		dict = {}
		for i in s:
			if i in dict:
				dict[i] += 1
			else:
				dict[i] = 1
		count = 0
		for i in dict:
			if dict[i] > 1:
				count += dict[i] ** 2
			else:
				count += 1
		return count


class Solution:

	def equalPairs(self, s):
		d1 = {}
		for i in s:
			if i not in d1:
				d1[i] = 1
			else:
				d1[i] += 1
		res = len(s)
		for i in d1:
			res += d1[i] * (d1[i] - 1)
		return res


from itertools import combinations

class Solution:

	def equalPairs(self, s):
		ans = len(s) * len(s)
		pairs = 0
		count = {}
		for i in s:
			if i in count:
				count[i] += 1
			else:
				count[i] = 1
		for i in count:
			pairs += count[i] * count[i]
		return pairs


class Solution:

	def equalPairs(self, s):
		d = {}
		for i in s:
			d[i] = 1 + d.get(i, 0)
		ans = 0
		for i in d:
			ans += d[i] * d[i]
		return ans


from collections import Counter

class Solution:

	def equalPairs(self, s):
		c = Counter(s)
		return sum([item * item for item in c.values()])


class Solution:

	def equalPairs(self, s):
		n = len(s)
		c = 0
		d = {}
		for i in s:
			if i not in d:
				d[i] = 1
			else:
				d[i] += 1
		for i in d:
			if d[i] > 1:
				c += pow(d[i], 2)
			else:
				c += 1
		return c


class Solution:

	def equalPairs(self, s):
		t = 0
		d = {k: 0 for k in set(s)}
		for i in s:
			d[i] += 1
		for i in d.values():
			t += i * i
		return t


class Solution:

	def equalPairs(self, s):
		freq = [0 for i in range(256)]
		for i in s:
			freq[ord(i)] += 1
		res = 0
		for i in range(256):
			res += freq[i] * freq[i]
		return res


class Solution:

	def equalPairs(self, s):
		d = {}
		for char in s:
			if char not in d:
				d[char] = 1
			else:
				d[char] += 1
		ans = 0
		for (key, val) in d.items():
			ans += val ** 2
		return ans


class Solution:

	def equalPairs(self, s):
		pairs = 0
		arr = [0] * 26
		for i in s:
			arr[ord(i) - 97] += 1
		for j in arr:
			pairs += j * j
		return pairs


class Solution:

	def equalPairs(self, s):
		dis = {}
		for x in s:
			if x in dis:
				dis[x] += 1
			else:
				dis[x] = 1
		sm = 0
		for x in s:
			sm += dis[x]
		return sm


class Solution:

	def equalPairs(self, s):
		d = {}
		for i in s:
			d[i] = d.get(i, 0) + 1
		ans = 0
		for (k, v) in d.items():
			ans += v ** 2
		return ans


class Solution:

	def equalPairs(self, s):
		count = [0] * 26
		total = 0
		for ele in s:
			pos = ord(ele) - ord('a')
			count[pos] += 1
		for ele in count:
			if ele > 0:
				total += ele * ele
		return total
t = int(input())
for tc in range(t):
	s = input()
	ob = Solution()
	print(ob.equalPairs(s))


class Solution:

	def equalPairs(self, s):
		d = {}
		for itr in range(len(s)):
			if s[itr] in d.keys():
				d[s[itr]] += 1
			else:
				d[s[itr]] = 1
		total = 0
		for itr in list(d.values()):
			total += itr * itr
		return total
t = int(input())
for tc in range(t):
	s = input()
	ob = Solution()
	print(ob.equalPairs(s))


class Solution:

	def equalPairs(self, s):
		arr = [0] * 26
		pair = 0
		dpair = 0
		for i in range(len(s)):
			if s[i] in s:
				arr[ord(s[i]) - 97] += 1
		for j in arr:
			if j <= 1:
				dpair += j
			else:
				pair += j * j
		return dpair + pair
t = int(input())
for tc in range(t):
	s = input()
	ob = Solution()
	print(ob.equalPairs(s))


class Solution:

	def equalPairs(self, s):
		p = 1
		m = {}
		for i in s:
			if i in m:
				m[i] += 1
			else:
				m[i] = 1
		c = 0
		for i in m:
			c += m[i] ** 2
		return c
t = int(input())
for tc in range(t):
	s = input()
	ob = Solution()
	print(ob.equalPairs(s))


class Solution:

	def equalPairs(self, s):
		mp = [0] * 26
		c = 0
		for i in s:
			x = ord(i) - ord('a')
			mp[x] += 1
		for i in mp:
			if i == 1:
				c += 1
			elif i != 0 and i > 1:
				c += i * i
		return c
t = int(input())
for tc in range(t):
	s = input()
	ob = Solution()
	print(ob.equalPairs(s))


class Solution:

	def equalPairs(self, s):
		MAX = 26
		ans = 0
		cnt = [0 for i in range(0, MAX)]
		for i in range(len(s)):
			cnt[ord(s[i]) - 97] += 1
		for i in range(0, MAX):
			ans += cnt[i] * cnt[i]
		return ans


class Solution:

	def equalPairs(self, s):
		li = [0] * 26
		res = 0
		for i in s:
			li[ord(i) - 97] += 1
		for i in li:
			res = res + i * i
		return res


class Solution:

	def equalPairs(self, s):
		s = list(s)
		d = {}
		count = 0
		for i in s:
			if i not in d:
				d[i] = 1
			else:
				d[i] += 1
		for i in d.keys():
			s1 = d[i] * d[i]
			count += s1
		return count


class Solution:

	def equalPairs(self, s):
		n = len(s)
		d = {}
		for ch in s:
			if ch not in d:
				d[ch] = 1
			else:
				d[ch] += 1
		ans = n
		for (k, v) in d.items():
			ans += v * (v - 1)
		return ans


class Solution:

	def equalPairs(self, s):
		a = 0
		d = {}
		for i in s:
			if i not in d.keys():
				d[i] = 1
			else:
				d[i] = d[i] + 1
		for i in d:
			a = a + d[i] ** 2
		return a


class Solution:

	def equalPairs(self, s):
		ans = 0
		c = [0 for i in range(256)]
		for i in range(len(s)):
			c[ord(s[i]) - 97] += 1
		for i in range(256):
			ans += c[i] ** 2
		return ans


from collections import Counter

class Solution:

	def equalPairs(self, s):
		ans = 0
		a = Counter(s)
		for i in a:
			if a[i] == 1:
				ans += 1
			else:
				ans += a[i] ** 2
		return ans


class Solution:

	def equalPairs(self, s):
		ans = 0
		c = 0
		bag = ''
		for i in s:
			if i in bag:
				continue
			else:
				bag += i
				ans += s.count(i) ** 2
		return ans