-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2840.py
More file actions
45 lines (32 loc) · 1.51 KB
/
2840.py
File metadata and controls
45 lines (32 loc) · 1.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
"""2840. Check if Strings Can be Made Equal With Operations II
You are given two strings s1 and s2, both of length n, consisting of lowercase English letters.
You can apply the following operation on any of the two strings any number of times:
Choose any two indices i and j such that i < j and the difference j - i is even, then swap the two characters at those indices in the string.
Return true if you can make the strings s1 and s2 equal, and false otherwise.
Example 1:
Input: s1 = "abcdba", s2 = "cabdab"
Output: true
Explanation: We can apply the following operations on s1:
- Choose the indices i = 0, j = 2. The resulting string is s1 = "cbadba".
- Choose the indices i = 2, j = 4. The resulting string is s1 = "cbbdaa".
- Choose the indices i = 1, j = 5. The resulting string is s1 = "cabdab" = s2.
Example 2:
Input: s1 = "abe", s2 = "bea"
Output: false
Explanation: It is not possible to make the two strings equal.
"""
from collections import Counter
class Solution:
def checkStrings(self, s1: str, s2: str) -> bool:
even1 = Counter(s1[i] for i in range(0, len(s1), 2))
even2 = Counter(s2[i] for i in range(0, len(s2), 2))
odd1 = Counter(s1[i] for i in range(1, len(s1), 2))
odd2 = Counter(s2[i] for i in range(1, len(s2), 2))
return even1 == even2 and odd1 == odd2
# ---- Run in VS Code ----
if __name__ == "__main__":
s1 = input("Enter s1: ")
s2 = input("Enter s2: ")
sol = Solution()
result = sol.checkStrings(s1, s2)
print(result)