-
-
Notifications
You must be signed in to change notification settings - Fork 46.6k
added algorithm to remove duplicates from a linked list #9395
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
Closed
Closed
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
50fd067
added algorithm to remove duplicates from a linked list
pa-kh039 6385383
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 3290352
update
pa-kh039 6fcc91c
updates
pa-kh039 55a02a1
added dp algorithm for counting distinct subsequences
pa-kh039 bbb717a
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,109 @@ | ||
from __future__ import annotations | ||
|
||
from dataclasses import dataclass | ||
|
||
|
||
@dataclass | ||
class Node: | ||
data: int | ||
next_node: Node | None = None | ||
|
||
|
||
def print_linked_list(head: Node | None) -> None: | ||
""" | ||
Print the entire linked list iteratively. | ||
|
||
>>> head = insert_node(None, 0) | ||
>>> head = insert_node(head, 2) | ||
>>> head = insert_node(head, 1) | ||
>>> print_linked_list(head) | ||
0->2->1 | ||
>>> head = insert_node(head, 4) | ||
>>> head = insert_node(head, 5) | ||
>>> print_linked_list(head) | ||
0->2->1->4->5 | ||
""" | ||
if head is None: | ||
return | ||
while head.next_node is not None: | ||
print(head.data, end="->") | ||
head = head.next_node | ||
print(head.data) | ||
|
||
|
||
def insert_node(head: Node | None, data: int) -> Node | None: | ||
""" | ||
Insert a new node at the end of a linked list | ||
and return the new head. | ||
|
||
>>> head = insert_node(None, 10) | ||
>>> head = insert_node(head, 9) | ||
>>> head = insert_node(head, 8) | ||
>>> print_linked_list(head) | ||
10->9->8 | ||
""" | ||
new_node = Node(data) | ||
if head is None: | ||
return new_node | ||
|
||
temp_node = head | ||
while temp_node.next_node: | ||
temp_node = temp_node.next_node | ||
temp_node.next_node = new_node | ||
return head | ||
|
||
|
||
def remove_duplicates(head: Node | None) -> Node | None: | ||
""" | ||
Remove nodes with duplicate data | ||
|
||
>>> head=insert_node(None,1) | ||
>>> head=insert_node(head,1) | ||
>>> head=insert_node(head,2) | ||
>>> head=insert_node(head,3) | ||
>>> head=insert_node(head,3) | ||
>>> head=insert_node(head,4) | ||
>>> head=insert_node(head,5) | ||
>>> head=insert_node(head,5) | ||
>>> head=insert_node(head,5) | ||
>>> new_head= remove_duplicates(head) | ||
>>> print_linked_list(new_head) | ||
1->2->3->4->5 | ||
""" | ||
if head is None or head.next_node is None: | ||
return head | ||
|
||
has_occurred = {} | ||
|
||
new_head = head | ||
last_node = head | ||
has_occurred[head.data] = True | ||
current_node = None | ||
if head.next_node: | ||
current_node = head.next_node | ||
while current_node is not None: | ||
if current_node.data not in has_occurred: | ||
last_node.next_node = current_node | ||
last_node = current_node | ||
has_occurred[current_node.data] = True | ||
current_node = current_node.next_node | ||
last_node.next_node = None | ||
return new_head | ||
|
||
|
||
if __name__ == "__main__": | ||
import doctest | ||
|
||
doctest.testmod() | ||
|
||
head = insert_node(None, 1) | ||
head = insert_node(head, 1) | ||
head = insert_node(head, 2) | ||
head = insert_node(head, 3) | ||
head = insert_node(head, 3) | ||
head = insert_node(head, 4) | ||
head = insert_node(head, 5) | ||
head = insert_node(head, 5) | ||
|
||
new_head = remove_duplicates(head) | ||
print_linked_list(new_head) |
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,51 @@ | ||
from future import __annotations__ | ||
|
||
|
||
def subsequenceCounting(s1: str, s2: str, n: int, m: int) -> int: | ||
""" | ||
Uses bottom-up dynamic programming/tabulation | ||
to count the number of distinct | ||
subsequences of string s2 in string s1 | ||
|
||
>>> s1 = "babgbag" | ||
>>> s2 = "bag" | ||
>>> subsequenceCounting(s1, s2, len(s1), len(s2)) | ||
""" | ||
# Initialize a DP table to store the count of distinct subsequences | ||
dp = [[0 for i in range(m + 1)] for j in range(n + 1)] | ||
|
||
# Base case: There is exactly one subsequence of an empty string s2 in s1 | ||
for i in range(n + 1): | ||
dp[i][0] = 1 | ||
|
||
# Initialize dp[0][i] to 0 for i > 0 since an empty s1 cannot have a non-empty subsequence of s2 | ||
for i in range(1, m + 1): | ||
dp[0][i] = 0 | ||
|
||
# Fill in the DP table using dynamic programming | ||
for i in range(1, n + 1): | ||
for j in range(1, m + 1): | ||
# If the current characters match, we have two choices: | ||
# 1. Include the current character in both s1 and s2 (dp[i-1][j-1]) | ||
# 2. Skip the current character in s1 (dp[i-1][j]) | ||
if s1[i - 1] == s2[j - 1]: | ||
dp[i][j] = dp[i - 1][j - 1] + dp[i - 1][j] | ||
else: | ||
dp[i - 1][j] | ||
|
||
# The final value in dp[n][m] is the count of distinct subsequences | ||
return dp[n][m] | ||
|
||
|
||
if __name__ == "__main__": | ||
import doctest | ||
|
||
doctest.testmod() | ||
s1 = "babgbag" | ||
s2 = "bag" | ||
|
||
# Find the number of distinct subsequences of string s2 in string s1 | ||
print( | ||
"The Count of Distinct Subsequences is", | ||
subsequenceCounting(s1, s2, len(s1), len(s2)), | ||
) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Variable and function names should follow the
snake_case
naming convention. Please update the following name accordingly:subsequenceCounting
Please provide descriptive name for the parameter:
n
Please provide descriptive name for the parameter:
m