String Equality (Codeforces 1451C)
Tags: Greedy, hashing

Ashish has two strings $$$a$$$ and $$$b$$$, each of length $$$n$$$, and an integer $$$k$$$. The strings only contain lowercase English letters.He wants to convert string $$$a$$$ into string $$$b$$$ by performing some (possibly zero) operations on $$$a$$$.In one move, he can either   choose an index $$$i$$$ ($$$1 \leq i\leq n-1$$$) and swap $$$a_i$$$ and $$$a_{i+1}$$$, or  choose an index $$$i$$$ ($$$1 \leq i \leq n-k+1$$$) and if $$$a_i, a_{i+1}, \ldots, a_{i+k-1}$$$ are all equal to some character $$$c$$$ ($$$c \neq$$$ 'z'), replace each one with the next character $$$(c+1)$$$, that is, 'a' is replaced by 'b', 'b' is replaced by 'c' and so on. Note that he can perform any number of operations, and the operations can only be performed on string $$$a$$$. Help Ashish determine if it is possible to convert string $$$a$$$ into $$$b$$$ after performing some (possibly zero) operations on it.

Input: The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^5$$$) — the number of test cases. The description of each test case is as follows.The first line of each test case contains two integers $$$n$$$ ($$$2 \leq n \leq 10^6$$$) and $$$k$$$ ($$$1 \leq k \leq n$$$).The second line of each test case contains the string $$$a$$$ of length $$$n$$$ consisting of lowercase English letters.The third line of each test case contains the string $$$b$$$ of length $$$n$$$ consisting of lowercase English letters.It is guaranteed that the sum of values $$$n$$$ among all test cases does not exceed $$$10^6$$$.

Output: For each test case, print "Yes" if Ashish can convert $$$a$$$ into $$$b$$$ after some moves, else print "No".You may print the letters of the answer in any case (upper or lower).

Note: In the first test case it can be shown that it is impossible to convert $$$a$$$ into $$$b$$$.In the second test case,"abba" $$$\xrightarrow{\text{inc}}$$$ "acca" $$$\xrightarrow{\text{inc}}$$$ $$$\ldots$$$ $$$\xrightarrow{\text{inc}}$$$ "azza".Here "swap" denotes an operation of the first type, and "inc" denotes an operation of the second type.In the fourth test case,"aaabba" $$$\xrightarrow{\text{swap}}$$$ "aaabab" $$$\xrightarrow{\text{swap}}$$$ "aaaabb" $$$\xrightarrow{\text{inc}}$$$ $$$\ldots$$$ $$$\xrightarrow{\text{inc}}$$$ "ddaabb" $$$\xrightarrow{\text{inc}}$$$ $$$\ldots$$$ $$$\xrightarrow{\text{inc}}$$$ "ddddbb" $$$\xrightarrow{\text{inc}}$$$ $$$\ldots$$$ $$$\xrightarrow{\text{inc}}$$$ "ddddcc".

Sample Inputs:
4
3 3
abc
bcd
4 2
abba
azza
2 1
zz
aa
6 2
aaabba
ddddcc

Sample Outputs:
No
Yes
No
Yes

Solution (Python):
import random
import math
from collections import defaultdict
import itertools
from sys import stdin, stdout
import sys
import operator
from decimal import Decimal
p2D = lambda x: print(*x, sep='\n')

def II():
    return int(sys.stdin.buffer.readline())

def MI():
    return map(int, sys.stdin.buffer.readline().split())

def LI():
    return list(map(int, sys.stdin.buffer.readline().split()))

def LLI(rows_number):
    return [LI() for _ in range(rows_number)]

def BI():
    return sys.stdin.buffer.readline().rstrip()

def SI():
    return sys.stdin.buffer.readline().rstrip().decode()

def main():
    mod = 10 ** 9 + 7
    for _ in range(II()):
        (n, k) = MI()
        a = BI()
        b = BI()
        dab = [0] * 26
        for ai in a:
            dab[ai - 97] -= 1
        for bi in b:
            dab[bi - 97] += 1
        flag = 0
        sab = 0
        for i in range(25, -1, -1):
            if dab[i] % k != 0:
                flag = 1
                break
            sab += dab[i]
            if sab < 0:
                flag = 1
                break
        if flag == 0:
            print('YES')
        else:
            print('NO')
if __name__ == '__main__':
    main()

Solution (Python):
import sys
sys.setrecursionlimit(10 ** 5)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep='\n')

def II():
    return int(sys.stdin.buffer.readline())

def MI():
    return map(int, sys.stdin.buffer.readline().split())

def LI():
    return list(map(int, sys.stdin.buffer.readline().split()))

def LLI(rows_number):
    return [LI() for _ in range(rows_number)]

def BI():
    return sys.stdin.buffer.readline().rstrip()

def SI():
    return sys.stdin.buffer.readline().rstrip().decode()

def cal(s):
    res = [0] * 26
    for c in s:
        res[c - 97] += 1
    return res

def ok():
    cnt = 0
    for (c1, c2) in zip(cc1, cc2):
        d = c1 - c2
        if d == 0:
            continue
        if abs(d) % k:
            return False
        cnt += d // k
        if cnt < 0:
            return False
    return True
for _ in range(II()):
    (n, k) = MI()
    s = BI()
    t = BI()
    cc1 = cal(s)
    cc2 = cal(t)
    if ok():
        print('Yes')
    else:
        print('No')

Solution (Python):
for _ in range(int(input())):
    (n, k) = map(int, input().split())
    a = [0] * 26
    for x in input():
        a[ord(x) - 97] += 1
    for x in input():
        a[ord(x) - 97] -= 1
    a = [x for x in a if x != 0]
    t = 0
    pas = True
    for x in a:
        if abs(x) % k == 0 and t > -1:
            t += x
        else:
            pas = False
            break
    print('Yes' if pas else 'No')