Skip to content

Commit 74b1831

Browse files
authored
Merge pull request #1397 from sounmind/main
2 parents 1618f0c + 277cc6f commit 74b1831

File tree

6 files changed

+131
-123
lines changed

6 files changed

+131
-123
lines changed

encode-and-decode-strings/sounmind.js

-58
This file was deleted.

encode-and-decode-strings/sounmind.py

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
from typing import List
2+
3+
4+
class Solution:
5+
# Special character used as delimiter between length and actual string
6+
delimiter = "$"
7+
8+
def encode(self, strs: List[str]) -> str:
9+
"""
10+
Encodes a list of strings into a single string.
11+
Format: [length]$[string][length]$[string]...
12+
"""
13+
encoded = []
14+
# Encode each word using the format: length + delimiter + word
15+
for word in strs:
16+
# Convert word length to string, add delimiter, then add the word itself
17+
encoded.append(f"{len(word)}{self.delimiter}{word}")
18+
19+
# Join all encoded elements into a single string and return
20+
return "".join(encoded)
21+
22+
def decode(self, encoded: str) -> List[str]:
23+
"""
24+
Decodes a single string back to the original list of strings.
25+
"""
26+
decoded = [] # List to store the decoded strings
27+
current_index = 0 # Pointer to track current position in the encoded string
28+
29+
# Process the encoded string until we reach the end
30+
while current_index < len(encoded):
31+
# Starting from current_index, find the position of delimiter
32+
delimiter_index = current_index
33+
34+
# Move forward until we find the delimiter character
35+
while encoded[delimiter_index] != self.delimiter:
36+
delimiter_index += 1
37+
38+
# Extract and convert the length prefix to integer
39+
word_length = int(encoded[current_index:delimiter_index])
40+
41+
# Calculate the start position of the word (after the delimiter)
42+
word_start = delimiter_index + 1
43+
44+
# Calculate the end position of the word
45+
word_end = word_start + word_length
46+
47+
# Extract the word using the calculated positions
48+
word = encoded[word_start:word_end]
49+
50+
# Add the decoded word to our result list
51+
decoded.append(word)
52+
53+
# Move the current index to the end of this word for the next iteration
54+
current_index = word_end
55+
56+
return decoded

group-anagrams/sounmind.js

-35
This file was deleted.

group-anagrams/sounmind.py

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
from collections import defaultdict
2+
from typing import List
3+
4+
5+
class Solution:
6+
def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
7+
# Dictionary to store anagram groups using character frequency as key
8+
anagram_groups = defaultdict(list)
9+
10+
for word in strs:
11+
# Create a frequency array for 26 lowercase English letters
12+
char_frequency = [0] * 26
13+
14+
# Count frequency of each character in the current word
15+
for char in word:
16+
# Convert character to 0-25 index (a=0, b=1, ..., z=25)
17+
char_index = ord(char) - ord("a")
18+
char_frequency[char_index] += 1
19+
20+
# Convert the frequency array to tuple (to make it hashable as dictionary key)
21+
# Words with identical character frequencies are anagrams
22+
freq_key = tuple(char_frequency)
23+
24+
# Add the current word to its corresponding anagram group
25+
anagram_groups[freq_key].append(word)
26+
27+
# Return all the anagram groups as a list of lists
28+
return list(anagram_groups.values())

merge-two-sorted-lists/sounmind.js

-30
This file was deleted.

merge-two-sorted-lists/sounmind.py

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
from typing import Optional
2+
3+
4+
class ListNode:
5+
def __init__(self, val=0, next=None):
6+
self.val = val
7+
self.next = next
8+
9+
10+
class Solution:
11+
def mergeTwoLists(
12+
self, list1: Optional[ListNode], list2: Optional[ListNode]
13+
) -> Optional[ListNode]:
14+
"""
15+
Merge two sorted linked lists and return the head of the merged list.
16+
17+
Args:
18+
list1: Head of the first sorted linked list
19+
list2: Head of the second sorted linked list
20+
21+
Returns:
22+
Head of the merged sorted linked list
23+
"""
24+
# Handle edge cases
25+
if not list1:
26+
return list2
27+
if not list2:
28+
return list1
29+
30+
# Create a dummy head to simplify the merging process
31+
dummy_head = ListNode()
32+
current = dummy_head
33+
34+
# Merge nodes from both lists in sorted order
35+
while list1 and list2:
36+
if list1.val < list2.val:
37+
current.next = list1
38+
list1 = list1.next
39+
else:
40+
current.next = list2
41+
list2 = list2.next
42+
current = current.next
43+
44+
# Attach remaining nodes from either list
45+
current.next = list1 if list1 else list2
46+
47+
return dummy_head.next

0 commit comments

Comments
 (0)