-
Notifications
You must be signed in to change notification settings - Fork 208
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #987 from shuvojitss/pr-4
Added Stack Permutation program
- Loading branch information
Showing
2 changed files
with
22 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters