Skip to content

Commit 915adea

Browse files
authored
Create 3992. Rearrange String to Avoid Character Pair.py
1 parent ee3a43e commit 915adea

1 file changed

Lines changed: 24 additions & 0 deletions

File tree

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution:
2+
def rearrangeString(self, s: str, x: str, y: str) -> str:
3+
result = []
4+
x_count = s.count(x)#O(n)
5+
y_count = s.count(y)#O(n)
6+
7+
if s.find(x) ==-1 or s.find(y) ==-1: #O(n)
8+
result.append(s)
9+
else:
10+
for s1 in s:
11+
if s1 == y:
12+
result.insert(0,s1) #O(n^2)
13+
elif s1 != x:
14+
result.append(s1)
15+
result.insert(y_count,x*x_count)
16+
return ''.join(result)
17+
18+
s = "zaodvxbsvqstlrbn"
19+
x = "t"
20+
y = "s"
21+
solution = Solution()
22+
print(solution.rearrangeString(s,x,y))
23+
24+

0 commit comments

Comments
 (0)