Skip to content

Commit f42e7c6

Browse files
authored
Merge pull request #1481 from ivan1016017/december10
adding algo
2 parents 8759cd7 + 65df3b3 commit f42e7c6

File tree

4 files changed

+108
-0
lines changed

4 files changed

+108
-0
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
from typing import List, Union, Collection, Mapping, Optional
2+
from abc import ABC, abstractmethod
3+
4+
class Solution:
5+
def strStr(self, haystack: str, needle: str) -> int:
6+
7+
len_needle = len(needle)
8+
len_haystack = len(haystack)
9+
10+
for i in range(len_haystack - len_needle + 1):
11+
if needle == haystack[i:i+len_needle]:
12+
return i
13+
14+
return -1
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
from typing import List, Union, Collection, Mapping, Optional
2+
from abc import ABC, abstractmethod
3+
4+
class Solution:
5+
def fullJustify(self, words: List[str], maxWidth: int) -> List[str]:
6+
"""
7+
Performs text justification by distributing words and spaces across lines of fixed width.
8+
9+
Strategy: Build lines word by word until adding the next word would exceed maxWidth.
10+
When a line is full, distribute extra spaces evenly using round-robin.
11+
"""
12+
result = [] # Final list of justified lines
13+
current_list = [] # Words being accumulated for the current line
14+
num_of_letters = 0 # Total character count (excluding spaces) in current_list
15+
16+
for word in words:
17+
# Check if adding this word exceeds the line width
18+
# Formula: letters + new_word_length + minimum_spaces_needed
19+
# Minimum spaces = one space between each word = len(current_list)
20+
if num_of_letters + len(word) + len(current_list) > maxWidth:
21+
# Justify and add the current line to results
22+
23+
# Calculate number of gaps between words (n words = n-1 gaps)
24+
# Use max(1, ...) to handle single-word lines and avoid division by zero
25+
num_gaps = max(1, len(current_list) - 1)
26+
27+
# Distribute extra spaces using round-robin
28+
# Total spaces needed = maxWidth - num_of_letters
29+
extra_spaces = maxWidth - num_of_letters
30+
for i in range(extra_spaces):
31+
# Use modulo to cycle through gaps repeatedly
32+
# This ensures spaces are distributed as evenly as possible
33+
gap_index = i % num_gaps
34+
current_list[gap_index] += ' '
35+
36+
# Join words into a single justified line and add to result
37+
result.append("".join(current_list))
38+
39+
# Reset for next line
40+
current_list = []
41+
num_of_letters = 0
42+
43+
# Add current word to the line being built
44+
current_list.append(word)
45+
num_of_letters += len(word)
46+
47+
# form last line by join with space and left justify to maxWidth using ljust (python method)
48+
# that means pad additional spaces to the right to make string length equal to maxWidth
49+
result.append(" ".join(current_list).ljust(maxWidth))
50+
51+
return result
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import unittest
2+
from src.my_project.interviews.top_150_questions_round_22\
3+
.ex_23_find_index_first_occurence import Solution
4+
5+
class FirstOccurrenceIndexTestCase(unittest.TestCase):
6+
7+
def test_first_occurrence_index(self):
8+
solution = Solution()
9+
output = solution.strStr(haystack="sadbutsad", needle="sad")
10+
target = 0
11+
self.assertEqual(output, target)
12+
13+
def test_no_first_occurrence_index(self):
14+
solution = Solution()
15+
output = solution.strStr(haystack="leetcode", needle="leeto")
16+
target = -1
17+
self.assertEqual(output, target)
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import unittest
2+
from src.my_project.interviews.top_150_questions_round_22\
3+
.ex_24_text_justification import Solution
4+
5+
class TextJustificationTestCase(unittest.TestCase):
6+
7+
def test_first_pattern(self):
8+
solution = Solution()
9+
output = solution.fullJustify(words = ["This", "is", "an", "example", "of", "text", "justification."], maxWidth = 16)
10+
target = [
11+
"This is an",
12+
"example of text",
13+
"justification. "
14+
]
15+
16+
self.assertEqual(output, target)
17+
18+
def test_second_pattern(self):
19+
solution = Solution()
20+
output = solution.fullJustify(words = ["What","must","be","acknowledgment","shall","be"], maxWidth = 16)
21+
target = [
22+
"What must be",
23+
"acknowledgment ",
24+
"shall be "
25+
]
26+
self.assertEqual(output, target)

0 commit comments

Comments
 (0)