Read problems statements in Mandarin Chinese , Russian and Vietnamese as well. 

Sergey recently learned about country codes - two letter strings, denoting countries. For example, BY stands for Belarus and IN stands for India. Mesmerized by this new discovery, Sergey now looks for country codes everywhere!

Sergey has recently found a string S consisting of uppercase Latin letters. He wants to find the number of different country codes that appear in S as contiguous substrings. For the purpose of this problem, consider that every 2-letter uppercase string is a valid country code.

------ Input ------ 

The first line of input contains an integer T denoting the number of test cases. The description of T test cases follows.

The first and only line of each test case contains a string S, consisting of uppercase Latin letters.

------ Output ------ 

For each test case, output a single line containing the number of different country codes appearing in the given string.

------ Constraints ------ 

$1 ≤ T ≤ 100$
$Subtask 1 (35 points): 2 ≤ |S| ≤ 3$
$Subtask 2 (65 points): 2 ≤ |S| ≤ 10^{4}$

----- Sample Input 1 ------ 
2
INBY
BYBY
----- Sample Output 1 ------ 
3
2
----- explanation 1 ------ 
Example case 1. The codes are IN, NB and BY.
Example case 2. The codes are BY and YB.

Source: codechef | Difficulty: EASY
Tags: String algorithms

Data structures


Solutions:
from itertools import permutations
t = int(input())
for i in range(t):
	string = ''
	s = input()
	str_list = []
	i = 0
	while i < len(s) - 1:
		string += s[i] + s[i + 1]
		if string not in str_list:
			str_list.append(string)
		string = ''
		i += 1
	print(len(str_list))


for _ in range(int(input())):
	S = input()
	L = []
	for i in range(len(S) - 1):
		if S[i:i + 2] not in L:
			L.append(S[i:i + 2])
	print(len(L))


for i in range(int(input())):
	s = input()
	a = []
	for i in range(len(s) - 1):
		if s[i:i + 2] not in a:
			a.append(s[i:i + 2])
	print(len(a))


t = int(input())
for i in range(t):
	s = input()
	set_a = set()
	for i in range(len(s) - 1):
		set_a.add(s[i:i + 2])
	print(len(set_a))


for _ in range(int(input())):
	s = input()
	l = []
	for i in range(len(s)):
		s1 = s[i:i + 2]
		if s1 not in l and len(s1) > 1:
			l.append(s1)
	print(len(l))


for _ in range(int(input())):
	s = input()
	(l, c) = ([], 0)
	for i in range(len(s) - 1):
		a = s[i:i + 2]
		if a not in l:
			l.append(a)
			c += 1
	print(c)


for i in range(int(input())):
	x = input()
	l = []
	for j in range(0, len(x) - 1):
		p = x[j:j + 2]
		l.append(p)
	k = set(l)
	print(len(k))


for _ in range(int(input())):
	s = list(input())
	l = []
	for i in range(1, len(s)):
		k = s[i - 1] + s[i]
		l.append(k)
	l = set(l)
	print(len(l))


t = int(input())
for i in range(t):
	s = input()
	c = set()
	for i in range(len(s) - 1):
		c.add(s[i] + s[i + 1])
	print(len(c))


t = int(input())
for _ in range(t):
	s = input()
	l = []
	for i in range(0, len(s) - 1):
		a = s[i:i + 2]
		l.append(a)
	l = list(set(l))
	print(len(l))


t = int(input())
for i in range(t):
	s = input()
	news = []
	for a in range(len(s)):
		s1 = s[a:a + 2]
		if s1 not in news and len(s1) > 1:
			news.append(s1)
	print(len(news))


for _ in range(int(input())):
	str = input()
	s = set()
	for i in range(len(str) - 1):
		if str[i:i + 2] not in s:
			s.add(str[i:i + 2])
	print(len(s))


for i in range(int(input())):
	x = input()
	l = []
	for j in range(len(x) - 1):
		l.append(x[j] + x[j + 1])
	print(len(set(l)))


test = int(input())
while test > 0:
	string = input()
	list1 = []
	for i in range(0, len(string) - 1):
		element = string[i] + string[i + 1]
		if element not in list1:
			list1.append(element)
	test = test - 1
	print(len(list1))


n = int(input())
for i in range(n):
	x = input()
	li = []
	for i in range(len(x) - 1):
		li.append(x[i] + x[i + 1])
	d = set(li)
	print(len(d))


n = int(input())
for i in range(n):
	a = input()
	lst1 = []
	for j in range(len(a)):
		if j == len(a) - 1:
			break
		lst1.append(a[j:j + 2])
	print(len(set(lst1)))


for i in range(int(input())):
	s = input()
	l = []
	for i in range(0, len(s)):
		d = s[i:i + 2]
		if len(d) == 2:
			l.append(d)
	s1 = set(l)
	print(len(s1))


for _ in range(int(input())):
	s = input()
	c = []
	for i in range(0, len(s)):
		c.append(s[i:i + 2])
	print(len(set(c)) - 1)


b = int(input())
for i in range(b):
	a = list(input())
	h = []
	for i in range(len(a) - 1):
		if a[i] + a[i + 1] not in h:
			h = h + [a[i] + a[i + 1]]
	print(len(h))


t = int(input())
while t != 0:
	t = t - 1
	s = input()
	l = []
	count = 0
	for i in range(len(s) - 1):
		if s[i:i + 2] not in l:
			l.append(s[i:i + 2])
			count += 1
	print(count)


T = int(input())
for cases in range(T):
	S = input()
	repeat = []
	count = 0
	for combinations in range(len(S) - 1):
		country = S[combinations] + S[combinations + 1]
		if country not in repeat:
			count += 1
			repeat.append(country)
	print(count)


for _ in range(int(input())):
	s = input()
	l = []
	for i in range(len(s) - 1):
		l.append(s[i:i + 2])
	print(len(set(l)))


t = int(input())
for i in range(t):
	n = input()
	l = []
	for i in range(len(n) - 1):
		for j in range(i + 1, i + 2):
			l.append(n[i] + n[j])
	print(len(set(l)))


from collections import Counter
for i in range(0, int(input())):
	l = list(input())
	p = []
	for j in range(len(l) - 1):
		p.append(l[j] + l[j + 1])
	d = Counter(p)
	print(len(d))


for _ in range(int(input())):
	s = input()
	w = set()
	for i in range(len(s) - 1):
		if (s[i] + s[i + 1]).isupper():
			w.add(s[i] + s[i + 1])
	print(len(w))


T = int(input())
for i in range(T):
	S = input()
	li = []
	c = 0
	for j in range(1, len(S)):
		if S[j - 1] + S[j] in li:
			continue
		else:
			li.append(S[j - 1] + S[j])
	print(len(li))


import string
t = int(input())
for i in range(t):
	s = input()
	a = len(s)
	l = []
	for i in range(a - 1):
		k = s[i:i + 2]
		l.append(k)
	print(len(set(l)))


for _ in range(int(input())):
	s = input()
	lst = []
	for i in range(len(s) - 1):
		lst.append(s[i] + s[i + 1])
	print(len(set(lst)))


for i in range(int(input())):
	s = input()
	l = []
	st = ''
	for i in range(len(s) - 1):
		st = s[i] + s[i + 1]
		if st not in l:
			l.append(st)
	print(len(l))


t = int(input())
for i in range(t):
	s = input()
	l = list(s.strip(''))
	k = []
	p = []
	for j in range(len(s) - 1):
		k.append(l[j:j + 2])
	for j in range(len(k)):
		if k[j] not in p:
			p.append(k[j])
	print(len(p))


for _ in range(int(input())):
	s = input()
	d = []
	for i in range(1, len(s)):
		d.append(s[i - 1] + s[i])
	di = set(d)
	print(len(di))


for i in range(int(input())):
	s = input()
	f = []
	for i in range(len(s) - 1):
		f.append(s[i] + s[i + 1])
	print(len(set(f)))


n = int(input())
for i in range(1, n + 1):
	s = input()
	d = {}
	for i in range(len(s) - 1):
		s1 = s[i] + s[i + 1]
		if s1 in d:
			d[s1] += 1
		else:
			d[s1] = 1
	print(len(d))


for i in range(int(input())):
	str = input()
	l = []
	for i in range(len(str) - 1):
		k = str[i] + str[i + 1]
		l.append(k)
	print(len(set(l)))


t = int(input())
for i in range(t):
	s = input()
	m = 0
	L = []
	n = 0
	for j in range(1, len(s)):
		k = s[m] + s[j]
		if k not in L:
			L.append(k)
			n = n + 1
		m = m + 1
	print(n)


n = int(input())
for i in range(n):
	s = input()
	c = 0
	l = []
	for i in range(len(s) - 1):
		l.append(s[i] + s[i + 1])
	print(len(set(l)))


for _ in range(int(input())):
	s = input()
	lis = []
	count = 0
	for i in range(1, len(s)):
		if s[i - 1:i + 1] not in lis:
			lis.append(s[i - 1:i + 1])
			count += 1
	print(count)


for _ in range(int(input())):
	string = input()
	lst = list(string)
	lst1 = []
	for i in range(len(lst) - 1):
		lst1.append(string[i:i + 2])
	x = set(lst1)
	print(len(x))


for i in range(int(input())):
	s = input()
	dic = {}
	for i in range(len(s) - 1):
		str = s[i] + s[i + 1]
		if str in dic:
			dic[str] += 1
		else:
			dic[str] = 1
	print(len(dic))


t = int(input())
while t > 0:
	s = input()
	p = set()
	for i in range(len(s) - 1):
		p.add(s[i] + s[i + 1])
	print(len(p))
	t -= 1


for i in range(int(input())):
	v = input()
	d = []
	for i in range(len(v) - 1):
		if (v[i], v[i + 1]) not in d:
			d.append((v[i], v[i + 1]))
	print(len(d))


for i in range(int(input())):
	s = input()
	p = []
	for i in range(len(s) - 1):
		p.append(s[i:i + 2])
	print(len(set(p)))


for i in range(int(input())):
	s = input()
	s = list([s[i:i + 2] for i in range(len(s) - 1)])
	print(len(set(s)))


t = int(input())
for i in range(t):
	n = input()
	k = list(n)
	li = []
	for i in range(0, len(k) - 1):
		if k[i] and k[i + 1]:
			li.append((k[i], k[i + 1]))
	p = set(li)
	print(len(p))


for _ in range(int(input())):
	s = input()
	k = []
	for i in range(len(s) - 1):
		if s[i] + s[i + 1] not in k:
			k.append(s[i] + s[i + 1])
	print(len(k))


import collections
tt = int(input())
for i in range(tt):
	s = input()
	countries = []
	for i in range(len(s) - 1):
		z = s[i:i + 2]
		countries.append(z)
	d = collections.Counter(countries)
	print(len(d.keys()))


t = int(input())
for tc in range(t):
	s = input()
	d = []
	for j in range(1, len(s)):
		d.append(s[j] + s[j - 1])
	print(len(set(d)))


import collections
tt = int(input())
for i in range(tt):
	s = input()
	d = {}
	for i in range(len(s) - 1):
		z = s[i:i + 2]
		d[z] = d.get(z, 0) + 1
	print(len(d.keys()))


for i in range(int(input())):
	s = input()
	lis = []
	for i in range(len(s) - 1):
		s1 = s[i] + s[i + 1]
		if s1 not in lis:
			lis.append(s1)
	print(len(lis))


for i in range(int(input())):
	s = input()
	s1 = set()
	for i in range(len(s) - 1):
		s1.add(s[i] + s[i + 1])
	print(len(s1))


n = int(input())
for i in range(n):
	s = set()
	a = input()
	for j in range(0, len(a) - 1):
		s.add(a[j:j + 2])
	s.add(a[len(a) - 2:])
	print(len(s))


t = int(input())
while t > 0:
	s = input()
	a = set()
	for i in range(len(s) - 1):
		a.add(s[i:i + 2])
	print(len(a))
	t = t - 1


t = int(input())
for i in range(t):
	s = input()
	s2 = ''
	s1 = set()
	for j in range(len(s) - 1):
		if (ord(s[j]) >= 65 and ord(s[j]) <= 90) and (ord(s[j + 1]) >= 65 and ord(s[j + 1]) <= 90):
			s2 = s[j] + s[j + 1]
			s1.add(s2)
	print(len(s1))


for i in range(int(input())):
	code = []
	a = str(input())
	for i in range(len(a) - 1):
		if a[i:i + 2] not in code:
			code.append(a[i:i + 2])
	print(len(code))


for i in range(int(input())):
	l = list(map(str, input()))
	b = []
	for i in range(len(l) - 1):
		b.append(l[i] + l[i + 1])
	print(len(set(b)))