We need to solve the problem: Given two strings s and t, we need to check if we can transform s into t by a series of operations where we can take any two adjacent characters and swap them if they are both consonants or both vowels, but cannot swap a consonant with a vowel. Essentially, we can reorder characters within the set of vowels and within the set of consonants arbitrarily (since adjacent swaps of same type allow any permutation?), but we cannot interchange a vowel and a consonant relative positions. However, are there constraints that the relative order between vowels and consonants is fixed? Let's analyze.
We have a string s of length n. Operation: select i (1 <= i <= n-1) such that either both s[i] and s[i+1] are vowels, or both are consonants. Then swap them. So we can swap adjacent same-type characters. This is essentially allowing us to reorder the characters within each type (vowel and consonant) arbitrarily because adjacent swaps generate the full symmetric group on positions of the same type? But careful: The positions of vowels and consonants are interleaved. If we swap adjacent same-type characters, that essentially allows us to bubble sort within each type, but the positions of the types themselves are not swapped. Actually, if we only swap adjacent characters when they are same type, then the sequence of types (i.e., whether each position is vowel or consonant) remains fixed. Because swapping two adjacent same-type characters doesn't change the pattern of vowel/consonant positions: after swapping, the two positions still contain the same type as before. So the type pattern (like a binary string where V=1, C=0) is invariant.
Therefore, we can rearrange the vowels arbitrarily among the positions that are originally vowels, and similarly consonants among the consonant positions. So the condition for s to be transformable into t is:
The type pattern of s must equal the type pattern of t (i.e., for each i, s[i] is vowel iff t[i] is vowel). Because swapping same-type doesn't change pattern.
After matching pattern, we need to check whether the multiset of vowels in s equals the multiset of vowels in t (when considering only vowel positions) and similarly for consonants.
But is that sufficient? Let's reason: If the patterns match, then we have a permutation of positions of same type. Since we can swap adjacent same-type characters arbitrarily, we can achieve any permutation of the characters among positions of that type? Let's verify: Within the set of indices that are vowels, we can treat them as a subsequence. Since we can swap adjacent vowels, we can reorder the vowels as we like as long as we only move within vowel positions. However, there may be consonants in between. Swapping adjacent vowels that are not directly adjacent because of consonants? Actually, two vowels are not necessarily adjacent if there is a consonant between them. To swap two vowels that are separated by consonants, we need to move them past consonants. But we cannot swap a vowel with a consonant. So a vowel cannot pass through a consonant. That means the relative order of vowel positions is fixed? Wait, consider string: V C V. The two vowels are separated by a consonant. Can we swap the two vowels? Let's attempt: positions 1=V1, 2=C, 3=V2. We want to swap V1 and V2. Since we cannot directly swap V1 and C (different types), we cannot move V1 to position 3 because that would require crossing the consonant. However, we could potentially move V2 to position 1? But to move V2 left, it would need to swap with C (position2) which is consonant, but that's disallowed because V2 is vowel and C is consonant. So no, we cannot exchange two vowels that are separated by at least one consonant. So the order of vowels relative to each other among the vowel positions is not fully permutable; they are confined to their original positions among the type sequence. Actually, consider the type pattern: Suppose pattern is V C V. The vowel positions are indices 1 and 3. They cannot be swapped because they are not adjacent and crossing a consonant is not allowed. So the only possible swaps are between adjacent vowels. That implies that within a contiguous block of consecutive vowels, you can reorder them arbitrarily (since adjacent swaps within block allow any permutation). But vowels separated by at least one consonant cannot be exchanged because to cross the consonant would require swapping a vowel with a consonant, which is not allowed. However, could we use a series of swaps to move a vowel past a consonant indirectly? For example, consider V C V. Could we first swap the consonant with something? But we have no other characters. The only allowed swaps are between same-type adjacent. So V and C cannot swap. So no. What about longer strings: V1 C1 V2 C2 V3? Could V1 move to the right? To move right, it would need to swap with C1, which is disallowed. So it's stuck. So indeed, the type pattern is invariant, and also the relative order of characters within each type is not fully changeable; only within each contiguous segment of the same type can we reorder arbitrarily. Actually, let's analyze the effect of allowed swaps.
We have a string of characters, each categorized as V or C. Allowed operation: swap adjacent positions i and i+1 if they have same type. This is analogous to a sorting network where only adjacent same-type swaps allowed. This is known as a "restricted swap" system. The invariant is not just the pattern of types, but also the sequence of types itself, and also the order of the characters within each type when you read them from left to right, but only if they are in separate blocks? Let's think in terms of "stable" labeling: Since a vowel cannot cross a consonant, the relative order of vowels that are in different blocks (separated by consonants) is fixed. However, within a block of consecutive vowels, we can reorder them arbitrarily. Similarly for consonants.
But is it possible for a vowel to move from one vowel block to another? To go from one block to another, it would have to cross a consonant, which is disallowed. So the block structure is invariant: the positions of vowels that are contiguous in the pattern remain contiguous, but the composition of each block can change only within the block. However, the pattern may have multiple vowel blocks separated by consonant blocks. Since vowels cannot cross consonants, the multiset of vowels in each block must match? Actually, the overall set of vowels can be redistributed among vowel blocks only if they can cross consonants, which they cannot. So the only thing we can do is: within each maximal consecutive segment of vowels, we can arbitrarily permute the vowels that originally were in that segment (but they cannot leave the segment). But wait, could a vowel from one block move into an adjacent block by swapping with a consonant? No, because swapping vowel and consonant is not allowed. So indeed, a vowel stays within its block of vowels. However, what about the scenario where there is a block of vowels, then a block of consonants, then another block of vowels. If we swap adjacent vowels within the first block, they stay there. But could a vowel from the first block become part of the second vowel block by moving through the consonant block? That would require crossing the consonant block, which is disallowed. So the blocks are isolated.
Thus the invariant is more refined: The string can be partitioned into alternating blocks of consecutive same-type characters. The allowed swaps only allow reordering within each block. Therefore, the final string t must have exactly the same type pattern, and for each block (maximal consecutive run of vowels or consonants), the multiset of characters in that block must be the same as the multiset in the corresponding block of s. Because characters cannot move between blocks.
But is it necessary that blocks correspond exactly? Let's check: The type pattern is invariant, so the positions of vowels and consonants are fixed. So the blocks (runs) are determined by the pattern. If we have pattern V V C V, that's blocks: first block: positions 1-2 (vowels), second block: position 3 (consonant), third block: position 4 (vowel). So indeed, each block's characters can only be permuted among themselves. Therefore, for s to be transformable to t, for each block index i, the multiset of characters in block i of s must equal the multiset in block i of t.
This seems plausible. Let's test with examples.
Example 1: s="abc", t="acb". Check types: a (vowel? Actually a is vowel), b is consonant, c is consonant? c is consonant. So s: V C C? Actually a is vowel, b consonant, c consonant => pattern V C C. t: a (vowel), c (consonant), b (consonant) => pattern V C C. Blocks: block1 (positions1) vowel 'a'? Actually careful: positions: 1 vowel, 2 consonant, 3 consonant. Blocks: first block of vowels is just position1, block of consonants positions2-3. In s, vowel block has {'a'}, consonant block has {'b','c'}. In t, vowel block has {'a'}, consonant block has {'c','b'} which is same multiset. Within the consonant block, we can swap adjacent consonants? positions2 and3 are both consonants, so we can swap them. So indeed s->t by swapping b and c. So condition holds.
Example 2: s="ab", t="ba". s: a(vowel), b(consonant) => pattern V C. t: b(consonant), a(vowel) => pattern C V. Patterns differ, so impossible. Output No. That matches the sample? Actually sample1? There is sample: acmicpc and tsukuba. Let's check. But we need to check both conditions.
However, there might be nuance: What about swapping adjacent same-type characters, but can we also use multiple swaps to effectively move a character across a block of opposite type if we have a "bridge" of same-type? For example, consider pattern: V C V. Could we move the left vowel to the right vowel position if we temporarily change the type of the middle character? No, we cannot change types. So no.
What about a longer pattern: V V C V V. The vowel blocks are positions 1-2 and 4-5. Could a vowel from block1 move to block2? It would have to cross the C at position3. To cross, it would need to swap with the C, which is disallowed. So cannot.
Thus the block-wise multiset condition is necessary and sufficient. Also note that adjacent same-type swaps generate the full symmetric group on each block because the block is a contiguous segment of same-type characters, and we can bubble sort within that segment using adjacent swaps. So any permutation within the block is achievable.
Therefore, problem reduces to: Check if s and t have the same pattern (vowel/consonant classification at each position). And for each maximal contiguous segment of same type (i.e., runs), the multiset of characters in s's segment equals that in t's segment.
We can implement by scanning both strings simultaneously, comparing type per position. If any position type mismatch, answer No. Otherwise, we need to verify per run.
Simplify: Since the pattern must match, we can just group runs of same type. For each run, we can extract the substring from s and t for that run, sort them (or count characters), and compare. If all runs match, answer Yes; else No.
But caution: The pattern matching ensures that the runs are aligned (same start and end positions). So we can iterate through the string and for each run, collect characters from s and t and compare.
Time complexity: O(n log n) sorting per run, or O(n) with counting if we use frequency arrays (since alphabet is lowercase English, 26 letters). n <= 2e5, so sorting each run could be overall O(n log n) worst-case if we sort each run individually; total characters sum over runs = n, so total sorting cost if we sort each run is sum(len_i log len_i) which could be up to n log n if there is one large run (like all vowels or all consonants). That's acceptable for n=2e5.
Alternatively, we can compare frequencies per run using arrays to avoid sorting.
Implementation steps:
Define a helper function is_vowel(c): return c in set('aeiou').
Read s and t.
n = len(s). If len(t) != n, output No (but problem states both strings have same length? Not explicitly but likely yes; we can check to be safe).
Check for each i from 0 to n-1: if is_vowel(s[i]) != is_vowel(t[i]) -> print("No") and return.
If all types match, then we need to check runs. We'll iterate i from 0 to n-1, maintaining start of current run. While i < n: determine type (vowel or consonant) of current position (since both s and t have same type pattern, we can use s or t). Let typ = is_vowel(s[i]). Then find end of run: j = i while j < n and is_vowel(s[j]) == typ: j++. Now we have run from i to j-1. Extract substring s_run = s[i:j], t_run = t[i:j]. Sort both substrings or compare character counts. If they are not equal as multisets, print No and return. Then set i = j.
If all runs match, print Yes.
Edge cases: Strings of length 1. Run single character; compare s[0] and t[0] after type check? Actually if types match, then we need to check within run: the multiset of s_run must equal t_run. For length 1, it's just equality of characters. So that works.
Let's test with given sample.
Sample Input:
acmicpc
tsukuba
First, let's classify characters.
s = "acmicpc"
a: vowel
c: consonant
m: consonant
i: vowel
c: consonant
p: consonant
c: consonant? Actually c is consonant. Wait string "acmicpc": a c m i c p c? Actually "acmicpc" spelled: a c m i c p c? Let's write: a (v), c (c), m (c), i (v), c (c), p (c), c (c). So pattern: V C C V C C C.
t = "tsukuba"
t: t (c)
s: s (c)
u: vowel
k: consonant? k is consonant.
u: vowel? Actually "tsukuba": t s u k u b a? Wait "tsukuba" is likely "tsukuba": t (c), s (c), u (v), k (c), u (v), b (c), a (v). So pattern: C C V C V C V. That's C C V C V C V. Compare with s pattern: V C C V C C C. They differ at first position (V vs C). So type mismatch at index0. Thus answer No. Sample output is No. Good.
Thus our reasoning matches sample.
Now need to be careful: Is there any scenario where we can change the type pattern? The operation never changes type, because swapping two same-type characters leaves type at each position unchanged? Actually if we swap two vowels, each position still contains a vowel, so the type pattern remains identical. So pattern invariant.
Thus solution is straightforward.
But wait, is there any nuance with the definition of vowel? The problem says: vowels are 'a', 'i', 'u', 'e', 'o'. Others are consonants. Yes.
Now we must consider if there is any other operation possible indirectly that could allow swapping of different types? The problem statement: "You can swap two adjacent characters if they are both vowels or both consonants." So only same-type swaps allowed. So indeed no cross-type swaps.
Thus solution stands.
Double-check with some custom cases.
Case: s="abc", t="cba". s: a(v), b(c), c(c) pattern V C C. t: c(c), b(c), a(v) pattern C C V. Pattern mismatch -> No. Indeed, cannot because a vowel cannot move from position1 to position3.
Case: s="abca", t="acba". s: a(v), b(c), c(c), a(v) pattern V C C V. t: a(v), c(c), b(c), a(v) pattern V C C V. Blocks: vowel block1: [a] (pos1); consonant block: positions2-3 (b,c); vowel block2: position4 (a). For block1 vowel: s has 'a', t has 'a' ok. consonant block: s "bc", t "cb" -> multiset same {b,c} ok. vowel block2: s 'a', t 'a' ok. So should be Yes. Can we transform? We can swap b and c (adjacent consonants) to get "acba"? Actually s="abca" -> swap positions2 and3 (b and c are both consonants) yields "acba". That's t. So Yes.
Case: s="abcde", t="aecbd". s: a(v), b(c), c(c), d(c), e(v) pattern V C C C V. t: a(v), e(v), c(c), b(c), d(c) pattern V V C C C. Pattern mismatch at pos2? Actually at position2, s has b(consonant), t has e(vowel) -> type mismatch. So No.
Case: s="abce", t="aecb". length 4. s: a(v), b(c), c(c), e(v) pattern V C C V. t: a(v), e(v), c(c), b(c) pattern V V C C. mismatch. So No.
Case: s="ab", t="ba" we did.
Case: s="ab", t="ab" Yes.
Case with more complex runs: s="baced", t="badec". Let's compute: s: b(c), a(v), c(c), e(v), d(c) pattern C V C V C. t: b(c), a(v), d(c), e(v), c(c) pattern C V C V C. Pattern matches. Runs: block1 consonant positions1: b vs b -> ok. block2 vowel positions2: a vs a -> ok. block3 consonant positions3: c vs d? Actually block3 is position3 only (since pattern C then V then C, so consecutive same types? Pattern: positions: 1(C),2(V),3(C),4(V),5(C) => runs: run1: [1] C, run2: [2] V, run3: [3] C, run4: [4] V, run5: [5] C. Each run length 1, so we compare characters: position3: s c, t d -> not equal, so No. But can we transform? Since positions are isolated, cannot swap because adjacent positions are different types. So indeed impossible.
Case: s="aebc", t="abec"? s: a(v), e(v), b(c), c(c) pattern V V C C. t: a(v), b(c), e(v), c(c) pattern V C V C -> mismatch? Actually at pos2: s e(v), t b(c) mismatch. So No.
Case: s="ae bc"? Actually consider swapping within a vowel block: s="aei", all vowels. Pattern V V V. t="iea". Since whole string is one vowel block, we can arbitrarily reorder because adjacent swaps allowed (all vowels). So should be Yes. Our algorithm: pattern matches, one run of length 3, multisets equal if s and t have same characters. If they do, Yes. So correct.
Similarly for consonants.
Now consider edge: Are there any tricks where two blocks of same type are adjacent? Actually runs are maximal, so they are separated by opposite type. So no adjacent same-type runs.
Thus solution is correct.
But wait, we need to confirm the statement: "You can swap two adjacent characters if they are both vowels or both consonants." It says "swap two adjacent characters" meaning we can pick any i and swap s[i] and s[i+1] if condition holds. This is exactly what we used.
Now, is there any scenario where we can effectively exchange a vowel and a consonant by a sequence of swaps? For example, consider three characters: V C V. Could we turn it into C V V? That would require moving the consonant left. To move consonant left, it would need to swap with the vowel on its left, but that's disallowed. So cannot. So our reasoning holds.
Thus answer: Compare types per position, then compare per run.
Implementation details:
Use a function is_vowel(c) that returns true if c in 'aeiou'.
If lengths differ, No (though problem may guarantee same length, but safe to check).
For i in range(n): if is_vowel(s[i]) != is_vowel(t[i]): print("No") and exit.
Then i=0; while i<n: typ = is_vowel(s[i]); j=i; while j<n and is_vowel(s[j])==typ: j+=1. Now we have segment [i,j). Compare sorted(s[i:j]) and sorted(t[i:j]) or use Counter. If they differ, print("No") and exit. i=j.
After loop, print("Yes").
Complexities: O(n log n) worst case due to sorting runs.
We can avoid sorting by using a frequency array for each run. But since runs could be many, we can simply compare characters after sorting (Python's sort is fast). n=2e5, sorting all characters overall is O(n log n) if we sort each run separately? Actually total sorting time if we sort each run individually: sum(len_i log len_i) <= n log n (since log len_i <= log n). But worst case if we have many small runs, the overhead of many sorts may be high but still acceptable? Suppose n=200000, with alternating single-character runs (like V C V C ...). Then we have about n/2 runs of length 1, sorting each length1 is trivial (constant). For runs length>1, the cost is low. In worst case, one run of length n, sorting O(n log n). That's fine.
Alternative: We could directly compare the whole string after type check by noting that we can reorder within each type block. Another viewpoint: If pattern matches, then for each i, the i-th vowel in order from left must be the same in s and t? Actually not, because vowels can be reordered within each vowel block but not across blocks. However, the ordering of vowels across blocks is fixed relative to blocks. But could we describe condition as: The sequence of vowels when reading left to right must be the same up to permutation within each block? That is, the multiset per block must match. So sorting per block is simplest.
Let's test a case: s = "aaabbb", t = "bbbaaa". Suppose pattern: all consonants? Actually if they are all same type? If all vowels, then one block. Sorting per block: s sorted "aaabbb", t sorted "aaabbb"? Actually t = "bbbaaa", sorted yields "aaabbb". But s block is whole string, so we compare sorted(s) vs sorted(t) which are equal. So algorithm says Yes. Indeed, we can reorder arbitrarily because all vowels and adjacent swaps allowed throughout (since all are same type, we can bubble sort). So correct.
If pattern is like V C V, then we have two vowel blocks separated by a consonant. s vowel block1 must have same chars as t vowel block1, and block2 must match. So the order of vowels overall may differ if the multisets differ between blocks. Example: s = "abaca"? Actually s: a (v), b (c), a (v) -> vowel blocks: [a], [a] both 'a'. t: a (v), b (c), a (v) works. But if s vowel block1 = a, block2 = e, and t block1 = e, block2 = a, then they would not match because block1 multiset differs. So algorithm catches that.
Thus solution.
Now, I'll code it.
But before finalizing, let's think about if there is any hidden complexity: The operation is allowed repeatedly. Are there any constraints like we can only swap if both are vowels or both are consonants, but we cannot swap if they are not adjacent? No.
Is there any possibility to change the type pattern indirectly by swapping two same-type characters that are not adjacent? No.
Thus answer.
We should also consider that the problem may have multiple test cases? The description says "The input consists of two strings S and T." So single case.
Thus final answer: Implement as described.
Now produce final solution in Python.
Edge Cases:
Empty string? n>=1? Probably n >=1.
Now, I'll write code.
Implementation:
def is_vowel(c):
return c in 'aeiou'
def solve():
import sys
input = sys.stdin.readline
s = input().strip()
t = input().strip()
n = len(s)
if len(t) != n:
print("No")
return
# check type per position
for i in range(n):
if is_vowel(s[i]) != is_vowel(t[i]):
print("No")
return
i = 0
while i < n:
typ = is_vowel(s[i])
j = i
while j < n and is_vowel(s[j]) == typ:
j += 1
# compare substrings s[i:j] and t[i:j] as multisets
if sorted(s[i:j]) != sorted(t[i:j]):
print("No")
return
i = j
print("Yes")
That should work.
But is it necessary to sort each run? Could there be a scenario where the pattern matches but the multiset per run matches but still cannot transform due to ordering constraints beyond multiset? For a single block, if multisets match, we can permute to any arrangement because the block consists entirely of same-type characters, and adjacent swaps within block allow any permutation (like bubble sort). So yes, sufficient.
Thus solution.
Double-check with a more complex block scenario: block of vowels with length L, we can rearrange arbitrarily because all are vowels and we can swap any adjacent vowels. Since the block is contiguous, we can use adjacent swaps to achieve any permutation; it's a sorting network with adjacent swaps which can generate any permutation (the symmetric group) because the restriction only that we can swap adjacent elements, which is enough to sort any list (bubble sort). So yes.
Thus done.
Now we can also implement with counting using arrays to be efficient, but sorting is fine.
I'll produce final answer with explanation.
But note: The problem name is "Same or not?" from AtCoder? Actually it's from AtCoder Beginner Contest 366? Possibly. But anyway.
Make sure to handle input with newline.
I'll write final solution.
Now write final answer in "final" channel.
We can transform the string only by swapping adjacent characters that are both vowels or both consonants. Such swaps never change whether a position contains a vowel or a consonant, so the pattern of vowels and consonants must be the same in both strings. Moreover, a character cannot move past a character of the opposite type because that would require a disallowed swap. Therefore, within each maximal contiguous block of the same type, the characters are confined to that block, and we can reorder them arbitrarily using allowed swaps. Thus, for each block, the multiset of characters in that block must be identical in the two strings.
We check these conditions:
For every index, the type (vowel/consonant) of S and T matches.
For every maximal run of equal type, the sorted sequence (or the character counts) of S and T over that run are the same.
If both hold, answer Yes; otherwise No.
python
import sys
def is_vowel(c: str) -> bool:
return c in 'aeiou'
def solve() -> None:
input = sys.stdin.readline
s = input().strip()
t = input().strip()
n = len(s)
if len(t) != n:
print("No")
return
# 1. type check
for i in range(n):
if is_vowel(s[i]) != is_vowel(t[i]):
print("No")
return
# 2. block-wise multiset check
i = 0
while i < n:
typ = is_vowel(s[i])
j = i
while j < n and is_vowel(s[j]) == typ:
j += 1
if sorted(s[i:j]) != sorted(t[i:j]):
print("No")
return
i = j
print("Yes")
if __name__ == "__main__":
solve()