A. Love Story
Solution
Given that the length of the provided string is 10, the approach involves iterating through the string and comparing each character at position i with the corresponding character at position i in "codeforces."
Code
t = int(input())
c = "codeforces"
for _ in range(t):
s = input()
res = 0
for i in range(10):
if s[i] != c[i]:
res += 1
print(res)
B. Short Substrings
Hint
Consider the first character in each pair.
Solution
Take every other character starting from the first one. Since the last character can not be a starting letter for a pair, add it to 'a' last.
Code
t = int(input())
for _ in range(t):
b = input()
a = [] # Storing the characters in a list and appending gives a better time complexity compared to string concatenation
for index in range(0, len(b), 2):
a.append(b[index])
a.append(b[-1])
a = "".join(a)
print(a)
D. Same Differences
Hint
Can you rewrite the formula?
Solution
The equation $$$a_j - a_i = j - i$$$ can be transformed into $$$a_j - j = a_i - i$$$, and let $$$b_i = a_i - i$$$. Then we need to count the number of pairs $$$(i, j)$$$ where $$$b_i = b_j$$$. We can use a dictionary(hash map) to count occurrences of each difference $$$(b_i)$$$.
Code
T = int(input())
for _ in range(T):
N = int(input())
a = list(map(int,input().split()))
difference_count = {}
pair_count = 0
for i in range(N):
difference = a[i] - i
pair_count += difference_count.get(difference, 0)
difference_count[difference] = difference_count.get(difference, 0) + 1
print(pair_count)