-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3561_Resulting_String_After_Adjacent_Removals.py
More file actions
69 lines (44 loc) · 1.97 KB
/
3561_Resulting_String_After_Adjacent_Removals.py
File metadata and controls
69 lines (44 loc) · 1.97 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
"""
You are given a string s consisting of lowercase English letters.
You must repeatedly perform the following operation while the string s has at least two consecutive characters:
Remove the leftmost pair of adjacent characters in the string that are consecutive in the alphabet, in either order (e.g., 'a' and 'b', or 'b' and 'a').
Shift the remaining characters to the left to fill the gap.
Return the resulting string after no more operations can be performed.
Note: Consider the alphabet as circular, thus 'a' and 'z' are consecutive.
Example 1:
Input: s = "abc"
Output: "c"
Explanation:
Remove "ab" from the string, leaving "c" as the remaining string.
No further operations are possible. Thus, the resulting string after all possible removals is "c".
Example 2:
Input: s = "adcb"
Output: ""
Explanation:
Remove "dc" from the string, leaving "ab" as the remaining string.
Remove "ab" from the string, leaving "" as the remaining string.
No further operations are possible. Thus, the resulting string after all possible removals is "".
Example 3:
Input: s = "zadb"
Output: "db"
Explanation:
Remove "za" from the string, leaving "db" as the remaining string.
No further operations are possible. Thus, the resulting string after all possible removals is "db".
Constraints:
1 <= s.length <= 105
s consists only of lowercase English letters.
"""
class Solution:
def resultingString(self, s: str) -> str:
def consecutive(a,b):
#Check if characters a & b are consecutive in circular alphabet
return abs(ord(a)-ord(b)) == 1 or (a == 'a' and b == 'z') or (a =='z' and b== 'a')
stack=[] #empty stack to store data
for char in s :
#if top os stack and currrent char are consecutive pop it out from stck
if stack and consecutive (stack[-1],char):
stack.pop()
else:
stack.append(char)
#rreamining char in stack are final string as result
return ''.join(stack)