Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added python code for stack permutation #1755

Merged
merged 1 commit into from
Nov 3, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions docs/Stack/Stack-permutation.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,33 @@ int main() {

```

### Python Implementation:
```python
def is_stack_permutation(input, output):
stack = []
j = 0
n = len(input)

for i in range(n):
# Push the current element of the input array to the stack
stack.append(input[i])

# Check if the top of the stack matches the output array
while stack and stack[-1] == output[j]:
stack.pop()
j += 1

# If j has reached n, then output is a valid permutation
return j == n

if __name__ == "__main__":
input = [1, 2, 3]
output = [2, 1, 3]

if is_stack_permutation(input, output):
print("Yes, it is a stack permutation")
else:
print("No, it is not a stack permutation")
```


Loading