Skip to content

Commit

Permalink
Merge pull request #987 from shuvojitss/pr-4
Browse files Browse the repository at this point in the history
Added Stack Permutation program
  • Loading branch information
UTSAVS26 authored Nov 1, 2024
2 parents dce2c01 + 0a8c5d2 commit 97c3cba
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
21 changes: 21 additions & 0 deletions Algorithms_and_Data_Structures/Stack/Stack_permutation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
def is_stack_permutation(input, output):
stack = [] # Use list as a stack
j = 0 # Pointer for the output sequence
for i in range(len(input)):
# Push the current element of the input array onto 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 # Move to the next element in output
# If j has reached the length of output, then output is a valid permutation
return j == len(output)

# Example usage
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")
1 change: 1 addition & 0 deletions Project-Structure.md
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@
* [Postfixtoprefix](Algorithms_and_Data_Structures/Stack/PostfixToPrefix.py)
* [Prefixtoinfix](Algorithms_and_Data_Structures/Stack/PrefixToInfix.py)
* [Prefixtopostfix](Algorithms_and_Data_Structures/Stack/PrefixToPostfix.py)
* [Stack Permutation](Algorithms_and_Data_Structures/Stack/Stack_permutation.py)
* [Stack](Algorithms_and_Data_Structures/Stack/stack.py)
* Strings
* [Longest Palindromic Substring](Algorithms_and_Data_Structures/Strings/longest_palindromic_substring.py)
Expand Down

0 comments on commit 97c3cba

Please sign in to comment.