We need to determine if S and T can be made equal by performing swaps of letters globally. Each operation: choose two distinct letters c1 and c2, then swap all occurrences of c1 with c2 and vice versa. This is essentially we can permute the alphabet arbitrarily via a sequence of swaps. But note that operations are applied to S (and we can apply them multiple times) and we want to see if there exists a sequence of swaps such that after applying them to S, we get T. Since each operation is a transposition on the alphabet (a permutation), the composition of such operations yields some permutation of the alphabet (since transpositions generate the symmetric group). Thus, any permutation of the alphabet can be achieved by a sequence of swaps. However, there is a catch: The operations are applied to S; we can only swap letters, but we can choose swaps adaptively. However, the set of all possible outcomes from S is {apply a permutation pi to the letters of S}, where pi is any bijection from the alphabet to itself? Actually, we can apply any permutation of the alphabet (26 letters) because the symmetric group on 26 elements is generated by transpositions. So we can map each letter in S to some other letter via a global permutation. However, we must be careful: The operation swaps two letters simultaneously; after a series of swaps, each original letter will have been mapped to some final letter. For any desired permutation pi, we can implement it by a series of transpositions (like bubble sort). So yes, any permutation is possible.
Thus the problem reduces to: Is there a permutation pi of the alphabet such that for every i, pi(S_i) = T_i? Actually, careful: If we apply permutation pi to S, we replace each letter c in S by pi(c). We need to get T. So we need existence of a bijection pi: letters -> letters such that for all positions i, pi(S_i) = T_i.
But there may be constraints: For the same letter in S, it must map to the same letter in T (since pi is a function). So if S_i = S_j, then pi(S_i) = T_i and pi(S_i) = T_j, so T_i must equal T_j. So we need that for every letter a, all positions where S has a map to a consistent target letter. That is, if two positions have same character in S, they must have same character in T. That is a necessary condition: for any i,j with S_i = S_j, we must have T_i = T_j. Equivalently, the mapping from characters in S to characters in T must be well-defined. Additionally, we also need that mapping is injective? Actually pi must be a bijection; it's a permutation of the 26 letters. So it must be one-to-one and onto. For it to be a permutation, it's a bijection from alphabet to itself. If we only require existence of a bijection pi such that pi(S_i)=T_i, then there are additional constraints: the mapping from source letters to target letters must be injective (since pi is injective). Because if two different source letters a and b both map to same target letter c, that would violate pi being bijective (since pi is one-to-one). Actually, wait: pi is a permutation, so it's a bijection from the 26 letters to 26 letters. So for distinct a,b, pi(a) != pi(b). So if we have positions where S has a and maps to T_i, and other positions where S has b maps to T_j, it must be that T_i != T_j if a!=b. So the mapping from S letters to T letters must be injective.
But is it also required to be surjective onto the target letters appearing? Since pi is onto all 26 letters, but we don't care about letters not used; it's fine if some letters are not used. So surjectivity onto the set of target letters is not required: if some target letter appears but its preimage is some letter that maybe not in S? Actually if target letter d appears, it must come from some source letter c such that pi(c)=d. So there must exist some c in S such that pi(c)=d. That's automatically satisfied if we define pi consistently: For each target letter d that appears, there must be at least one source letter c that maps to d. Since pi is a bijection, the mapping from source letters to target letters is a one-to-one correspondence between the set of letters used in S? Not exactly: pi is defined on all 26 letters. The letters that appear in S are a subset; they map to some letters. The letters that appear in T are the images of the letters in S. Additionally, there could be letters not in S that map to letters that appear? No, because if a target letter appears, it must be pi(c) for some c in S; else there is no source. So the set of target letters is a subset of the image of the set of source letters. Since pi is bijective, the image of the source set is exactly the set of letters that are pi(c) for c in source set. That's a set of same size as source set (since injection). So the size of the set of distinct characters in T must equal the size of distinct characters in S. Because pi restricted to source letters is injective and its image is exactly the target set. So |set(S)| = |set(T)|. That's another necessary condition.
However, is it sufficient? If we have a well-defined mapping f: source letters -> target letters that is bijective between the two sets (i.e., injective and surjective onto T set), we can extend it to a permutation of the full alphabet by arbitrarily mapping the remaining letters to the remaining ones. So yes, existence of such a bijection between the sets of letters present is sufficient.
But wait: there is nuance: The operation sequence must consist of swapping two letters at a time. Since any permutation can be achieved, we only need existence of a permutation pi such that pi(S) = T. That is exactly the condition: there exists a bijection (permutation) pi: alphabet -> alphabet such that for all i, pi(S_i) = T_i. This is equivalent to: For each letter a, all occurrences of a in S must map to the same letter b in T, and distinct a map to distinct b. That is the necessary and sufficient condition.
But check the examples:
-
S=azzel, T=apple. Let's see: positions: a->a, z->p, z->p (consistent), e->l, l->e? Wait S: a z z e l, T: a p p l e? Actually T = apple: a p p l e? No apple: a p p l e? Actually "apple" is a p p l e? Yes. So mapping: a->a, z->p, e->l, l->e. So mapping: a->a, z->p, e->l, l->e. Distinct source letters: a,z,e,l. Targets: a,p,l,e. All distinct? a,p,l,e are distinct. So injection holds? a maps to a, z to p, e to l, l to e. Distinct sources map to distinct targets. So condition satisfied. Also |set(S)|=4, |set(T)|=4. So Yes.
-
chokudai, redcoder: S: c h o k u d a i, T: r e d c o d e r? Let's write: redcoder: r e d c o d e r? Actually redcoder: r e d c o d e r? Wait length 8: positions: r e d c o d e r? That's r e d c o d e r -> letters: r,e,d,c,o,d,e,r? So mapping: c->r, h->e, o->d, k->c, u->o, d->d, a->e, i->r? Check: S[0]=c -> T[0]=r => c->r; S[1]=h->e => h->e; S[2]=o->d => o->d; S[3]=k->c => k->c; S[4]=u->o => u->o; S[5]=d->d? Actually T[5]=d? Wait T[5] is d? Let's index: T: r(0),e(1),d(2),c(3),o(4),d(5),e(6),r(7). So S[5]=d -> T[5]=d => d->d; S[6]=a -> T[6]=e => a->e; S[7]=i -> T[7]=r => i->r. Now we have mapping: c->r, h->e, o->d, k->c, u->o, d->d, a->e, i->r. Check consistency: For source c, mapping r; source i also maps to r -> conflict because two different source letters (c and i) both map to r? Actually check: c->r and i->r, so distinct sources c and i both map to the same target r. That violates injectivity. Also source h->e and a->e also conflict. So condition fails -> No.
-
S=abcdefghijklmnopqrstuvwxyz, T=ibyhqfrekavclxjstdwgpzmonu. That's a permutation of the alphabet: each source letter maps to a unique target letter? Let's check quickly: a->i, b->b? Actually T: i b y h q f r e k a v c l x j s t d w g p z m o n u? Need to see mapping: S: a b c d e f g h i j k l m n o p q r s t u v w x y z. T: i b y h q f r e k a v c l x j s t d w g p z m o n u? Let's write T string: "ibyhqfrekavclxjstdwgpzmonu". Length 26. Let's pair: 0:i,1:b,2:y,3:h,4:q,5:f,6:r,7:e,8:k,9:a,10:v,11:c,12:l,13:x,14:j,15:s,16:t,17:d,18:w,19:g,20:p,21:z,22:m,23:o,24:n,25:u. So mapping: a->i, b->b, c->y, d->h, e->q, f->f, g->r, h->e, i->k, j->a, k->v, l->c, m->l, n->x, o->j, p->s, q->t, r->d, s->w, t->g, u->p, v->z, w->m, x->o, y->n, z->u. Are all targets distinct? Let's see: i,b,y,h,q,f,r,e,k,a,v,c,l,x,j,s,t,d,w,g,p,z,m,o,n,u -> all 26 distinct? Yes appear all 26 letters. So mapping is a bijection (since source set is all 26). So condition satisfied. So Yes.
Thus the problem reduces to checking if the mapping from letters in S to letters in T is consistent (function) and injective (distinct sources map to distinct targets). Alternatively, we can think in terms of graph: we need to check if we can achieve T by a permutation of letters; that is equivalent to S and T must have the same multiset of letter pairs? Another way: There exists a permutation pi of the alphabet such that T_i = pi(S_i). So we need that for any two positions i,j, S_i = S_j implies T_i = T_j, and also S_i != S_j implies T_i != T_j (since pi is injective). But the second condition can be relaxed? Actually if pi is injective, then S_i != S_j => pi(S_i) != pi(S_j) => T_i != T_j. So indeed, if S_i != S_j but T_i = T_j, that would be a violation because two distinct source letters would map to the same target. However, could we still achieve T via some permutation if such equality happens? Let's test: Suppose S = "ab", T = "aa". Then mapping: a->a, b->a. That would require pi(a)=a and pi(b)=a, impossible because pi is not a function? Actually pi is a function, so pi(b) must be something; if pi(b)=a, then pi is not injective; but could we still transform S to T using swaps? Let's see: Starting with "ab". Can we swap some letters to get "aa"? Operations swap two letters globally. For instance, we could swap 'b' with 'a'? If we swap a and b, then S becomes "ba". Not "aa". If we swap b with some other letter? Not produce two a's because we have only one a originally. Since we can only permute letters, the multiset of letters is preserved. The operation is a permutation of the alphabet, so it just renames letters, so the multiset of letters in S after any sequence is just the multiset of the original letters renamed accordingly. So the multiset of letters in S (i.e., counts of each letter) will be transformed to a multiset where the counts are the same but letters are renamed. Specifically, if we have count of each original letter x, after permutation pi, the count of letter y in the new string is the sum of counts of original letters x such that pi(x)=y. So the multiset of counts (as a multiset of numbers) is the same, but the labels change. However, the multiset of counts as numbers? Actually, if we permute the alphabet, the frequencies of letters in the transformed string are just the frequencies of the preimages. So the multiset of frequencies (the histogram values) remains the same? Not exactly: Suppose original S has frequencies f_a, f_b, ... After applying pi, the frequency of letter y is f_{pi^{-1}(y)}. So the multiset of frequencies (as numbers) is the same, just assigned to different letters. So the sorted list of frequencies is invariant. But in the case S="ab" and T="aa", original frequencies: a:1, b:1 => frequencies {1,1}. Target frequencies: a:2, others:0 => frequencies {2,0} (but we consider all 26 letters; after transformation frequencies of some letters become zero). The sorted multiset changes? The multiset of frequencies over all 26 letters? The original has 1,1,0,... (many zeros). After transformation, we would have frequencies: for some letter y, count is 1 if y = pi(a) or pi(b) else 0. So we could have pi(a) = some letter X, pi(b) = some other letter Y distinct => then frequencies X:1, Y:1, others 0. To get "aa", we need pi(a)=pi(b)=same letter => then that letter gets frequency 2, others 0. But that would require pi not injective, which is not allowed because pi is a permutation. So it's impossible. So indeed injection is necessary.
Thus the condition is: For all pairs i,j, S_i = S_j <=> T_i = T_j. Equivalent to: The relation between positions is such that the pattern of equalities among characters is the same. That is, the strings S and T must have the same "letter partition" pattern. In other words, there exists a bijection between the set of distinct letters in S and that in T such that the mapping from positions holds.
We can check by building mapping arrays from characters of S to characters of T. For each character c, if we encounter S[i]=c, we record that T[i] must be the same for all occurrences. Also we need that no two different c map to same T[i]. So we can maintain an array map[26] initialized to -1 (or None), and also usedTarget[26] boolean to ensure injectivity.
Algorithm:
Initialize map[26] = None.
Initialize used[26] = False.
For i from 0 to n-1:
s = S[i], t = T[i]
if map[s] is None:
if used[t] is True: then conflict (since t already assigned to some other source) -> No.
else map[s] = t, used[t] = True.
else:
if map[s] != t: conflict -> No.
If finish loop: Yes.
That's O(n) time, O(1) space.
But wait: Are there any subtleties? Since the operation is swapping letters, we might need to consider that we can also apply multiple swaps sequentially, but we already argued that any permutation is possible. However, is there any restriction that the swaps must be applied to S, not to the alphabet as a whole? The operation is: choose two distinct letters c1 and c2, then replace every occurrence of c1 with c2, and every occurrence of c2 with c1. That's effectively swapping the labels of two letters. Over multiple operations, we can realize any permutation, but is there any limitation because we can't swap letters that are not present? Actually, we can choose any two letters, even if they don't appear in S. But swapping two letters that don't appear does nothing. So it's fine. So any permutation is reachable.
But is it always possible to achieve any permutation? Since the symmetric group on 26 elements is generated by transpositions, yes. However, we might need to consider that the operation is applied to S (the string) and not to the entire alphabet abstractly. But each operation swaps all occurrences of two letters, which is exactly applying a transposition to the alphabet and then applying that to the string. So after a sequence, the effect is applying the composition of those transpositions, which is a permutation.
Thus the condition is correct.
But wait, there's a known problem: AtCoder ABC 232 C? Actually this seems like a typical problem from AtCoder (maybe ABC 110 B? Or ABC 207? Not sure). But often there's a twist: The operation is allowed any number of times, and we need to check if S can be transformed to T. There's also a known solution of checking that for each character, the set of positions in S and T must be same after mapping? Actually typical solution: For each character in S, the corresponding character in T must be consistent, and also the mapping from T to S must be consistent (i.e., it's a bijection). So yes.
But let's double-check with some tricky cases. What if S = "a", T = "b"? Mapping: a->b, and since only one letter, injection is trivial. So Yes. We can swap a and b. That's valid.
What if S = "ab", T = "ba"? Mapping: a->b, b->a. That's a bijection between {a,b} and {b,a}. So Yes. We can swap a and b.
What if S = "aa", T = "bb"? Mapping: a->b for both positions, injection trivial (only a). So Yes: swap a with b (swap all a's with b's). But swapping a and b when S is "aa": swap all a with b yields "bb". Yes.
What if S = "ab", T = "aa"? Already we said No. Check condition: map[a]=a? Actually S[0]=a -> T[0]=a, map[a]=a; S[1]=b -> T[1]=a, map[b] not set, but used[a] is true because a already used as target for a, so conflict. So No.
What if S = "abc", T = "def"? All distinct letters, mapping a->d, b->e, c->f. Distinct targets, so Yes.
What if S = "aba", T = "cbc"? Mapping: a->c, b->b, a->c consistent. Distinct sources: a->c, b->b, targets distinct? c and b are distinct. So Yes.
What if S = "aba", T = "cbd"? S: a->c, b->b, a->c (consistent). But then b->b, a->c, but target d appears? Actually T = "c b d"? That would be positions: 0:c,1:b,2:d. So mapping: S[0]=a->c, S[1]=b->b, S[2]=a->d -> conflict because a already mapped to c but now requires d. So No.
What about S = "ab", T = "ab"? trivial Yes.
Thus algorithm works.
But is there any scenario where mapping is consistent and injective but still impossible due to some parity or something? Consider we need to achieve a specific permutation pi. If we can implement any permutation, no problem. However, is it always true that any permutation of the alphabet can be achieved by a sequence of swaps (transpositions) applied to the string? Yes, because we can just compose transpositions to build any permutation. For instance, to apply permutation pi, we can decompose pi into transpositions and apply them one by one. But careful: The operation applies a transposition to the current string by swapping two letters globally. If we apply transposition (a b), then later apply (b c), the composition is not necessarily the same as applying (a c) directly? But we can achieve any permutation because the group generated by transpositions is the full symmetric group. The process is: We have a current mapping from original letters to current letters. Starting from identity, each swap composes with the current mapping. So we can get any permutation by an appropriate sequence. So yes.
Thus problem solved.
But wait, maybe there's a nuance: The operation swaps every occurrence of two letters simultaneously. That means that if we want to map letter a to b and b to a, we just do one swap. But if we want to map a to b, b to c, and c to a, we might need multiple swaps. But it's doable: e.g., swap a and b, then swap b and c, etc. So yes.
Thus the answer is Yes if and only if the mapping from each character in S to T is a function and injective.
But we should also consider if there could be a case where the mapping is consistent but not injective, yet we could still achieve T by a non-permutation? Because the operations are swaps, which are permutations, so the overall transformation is a permutation. So no.
Thus solution is straightforward.
Now, check constraints: length up to 2e5, O(n) is fine.
We'll implement: read S and T lines. Ensure newline handling.
Edge cases: empty? constraints say length at least 1.
Now, let's produce Python code.
But wait, sometimes there is a known problem with this: AtCoder ABC 228 C? No. Actually I recall a problem "Coloring" but not this.
Nonetheless, we should verify with sample.
Now, I'll write solution.
Potential pitfalls: The mapping must be bijective between the sets of letters used. But our algorithm ensures that: For each source letter we assign a target, and we ensure each target is used at most once (injection). Additionally, we must also ensure that there is no case where a source letter maps to a target that is also the image of a different source via multiple assignments? That's injection. But what about surjectivity onto the set of target letters? Suppose S uses letters a,b,c; T uses letters x,y,z (distinct). Injection ensures a->x, b->y, c->z (or some mapping). This is bijection between the sets, so it's fine. But suppose T uses only two letters? Could that happen? Example: S="ab", T="aa" we already saw injection fails. If T uses x,y, but S uses a,b, injection requires distinct targets, so okay. If S uses three letters and T uses three letters, injection ensures distinct targets, so the images of a,b,c are three distinct letters, covering exactly the three letters of T? Could it be that the three distinct images are not exactly the three letters of T? Wait, if T contains three distinct letters, then for each position i, T[i] must be one of those three. The mapping from source letters to targets gives each source a target. Since each position's T is determined, the set of targets used by the mapping is exactly the set of distinct letters appearing in T, because every target letter appears at some position, and that position corresponds to some source letter. But could there be a target letter d that appears in T, but the source mapping from some letter maps to something else? Actually, consider S uses letters a,b; T uses letters x,y. Suppose mapping: a->x, b->x? That fails injection. Suppose mapping: a->x, b->y, then T positions: must have x and y accordingly. That works if the positions correspond. But what if T has a letter z that appears, but no source maps to z? That would be impossible because for each position where T is z, the source letter at that position, say c, must map to z. So c would map to z. So that target z will be assigned to that source. So the mapping's image always equals the set of distinct target letters that appear. So injection ensures a bijection between source set and target set. So condition is exactly mapping is a bijection.
Thus algorithm is correct.
However, I recall that there might be a nuance about the possibility of cycles requiring temporary third letter? But since we can swap any two letters, we can simulate any permutation. Even if we have mapping a->b, b->c, c->a, we can achieve by swapping a and b, then b and c, etc. For example S="abc", want T="bca". We can: swap a and b: S becomes "bac". Then swap a and c: now a becomes c, c becomes a? Actually careful: After first swap (a,b), the string is "bac". Now we want "bca". Currently we have b, a, c. To get b,c,a, we need to map a->c and c->a. So we swap a and c: after swap, a becomes c, c becomes a, so string becomes "bca". Works. So all good.
Thus final solution: read strings, then check condition.
Complexity: O(n).
Now, write code.
But wait, is there any trick with large constraints? Python string input fine.
Now, produce output.
I'll write:
import sys
def solve():
S = sys.stdin.readline().strip()
T = sys.stdin.readline().strip()
n = len(S)
mapping = {}
used = set()
for i in range(n):
s = S[i]
t = T[i]
if s in mapping:
if mapping[s] != t:
print("No")
return
else:
if t in used:
print("No")
return
mapping[s] = t
used.add(t)
print("Yes")
if name == "main":
solve()
But wait, need to consider that mapping from s to t must be consistent. But also we need to ensure that if two different s map to same t, we catch that via used set. That's correct.
But what about if mapping is consistent but there is a scenario where a letter in S maps to itself, but later used? That's fine.
Edge case: What if the mapping is consistent, but the used set restricts something? Consider S="ab", T="ba". Steps: i=0: s='a', t='b', mapping[a]='b', used={'b'}; i=1: s='b', t='a', mapping[b] not set, used has 'b' but t='a' not used, so mapping[b]='a', used add 'a'. Done. Yes.
What about S="ab", T="ab": mapping[a]='a', used{'a'}; mapping[b]='b', used already has 'a', but t='b' not in used, so allowed. Yes.
Works.
Thus solution is done.
But wait, the problem description: "Choose two distinct lowercase English letters c_1 and c_2, then replace every occurrence of c_1 with c_2, and every occurrence of c_2 with c_1." This operation is like swapping letters. However, is it allowed to perform swaps multiple times? Yes.
But some might think that the operation is applied to S only, and we can't use intermediate letters that are not present? Actually we can always swap any two letters, even if one of them is not present, that operation does nothing. So no issue.
Thus final answer.
But hold on: There is a known AtCoder problem "abc110_b" (1-SAT?) Not exactly. Another similar problem is from ABC 229 C? Not sure. However, I recall a problem "Make Them Equal" where the operation is swapping two characters in the string? That's different.
Thus answer.
But double-check sample 1: S="azzel", T="apple". Implementation: i0: a->a mapping{a:a}, used{a}. i1: z->p, mapping{z}=p, used{a,p}. i2: z->p, mapping[z] exists and equals p, ok. i3: e->l, mapping{e}=l, used add l -> used {a,p,l}. i4: l->e, mapping{l} not set, t=e not in used? used has a,p,l but not e, so mapping[l]=e, used add e. End. Prints Yes. Good.
Sample 2: S="chokudai", T="redcoder". Let's test with code: i0: c->r, mapping{c:r}, used{r}. i1: h->e, mapping{h:e}, used{r,e}. i2: o->d, mapping{o:d}, used{r,e,d}. i3: k->c, mapping{k:c}, used{r,e,d,c}. i4: u->o, mapping{u:o}, used add o -> used{r,e,d,c,o}. i5: d->d, mapping{d:?} new? d not mapped, t=d, is d in used? Yes, used has d, so conflict -> print No. So works.
Thus answer.
But I want to consider if there is any subtle condition regarding that we can swap letters only if both appear? The problem statement: "Choose two distinct lowercase English letters c_1 and c_2, then replace every occurrence of c_1 with c_2, and every occurrence of c_2 with c_1." It doesn't say they must appear in S. So can choose any letters. So fine.
Thus solution final.
I'll output the code.
But wait, there is a nuance: The operation is global and swaps all occurrences. This means that after a sequence of swaps, the mapping from original letters to final letters is a permutation. However, we need to consider that the mapping might be not well-defined if the same original letter maps to multiple final letters because of inconsistent swaps? But we enforce consistency.
Thus answer.
Alright, final.
Now, produce final output.
But as ChatGPT, we need to produce answer with explanation? The prompt says "You are ChatGPT... Output final answer". So I need to output the solution code? Usually we output the solution in Python.
But the instruction: "If S and T can be made equal, print Yes; otherwise, print No." That's the output for the judge. But here the user is asking me to produce a solution? The system is like "You are given... Determine if... Output ...". It looks like a typical programming problem. I should respond with a solution description and code? The typical ChatGPT answer for such tasks is to provide the code. However, the meta-instruction says: "You are ChatGPT... you should output the final answer." Possibly they expect just the code? In many cases, we output the Python code.
Given it's a problem statement, I'll assume we need to output the solution code. I'll produce a Python solution with explanation? But the final answer should be the code? The user didn't specify; they gave a problem statement. Possibly they want a solution. I'll provide a clear solution explanation and then the code.
But the prompt says "You are given strings S and T... Determine if... Output ..." This is a copy of a programming problem. As ChatGPT, I should produce the answer, i.e., the code that reads input and prints Yes/No. So I'll output the code.
Thus final. ```python
import sys
def solve() -> None:
S = sys.stdin.readline().strip()
T = sys.stdin.readline().strip()
n = len(S)
mapping = {} # mapping from a character in S to its target in T
used = set() # target characters already used
for i in range(n):
s = S[i]
t = T[i]
if s in mapping:
if mapping[s] != t:
print("No")
return
else:
if t in used:
print("No")
return
mapping[s] = t
used.add(t)
print("Yes")
if name == "main":
solve()