diff --git a/src/my_project/interviews/top_150_questions_round_22/ex_23_find_index_first_occurence.py b/src/my_project/interviews/top_150_questions_round_22/ex_23_find_index_first_occurence.py new file mode 100644 index 00000000..f986c5fb --- /dev/null +++ b/src/my_project/interviews/top_150_questions_round_22/ex_23_find_index_first_occurence.py @@ -0,0 +1,14 @@ +from typing import List, Union, Collection, Mapping, Optional +from abc import ABC, abstractmethod + +class Solution: + def strStr(self, haystack: str, needle: str) -> int: + + len_needle = len(needle) + len_haystack = len(haystack) + + for i in range(len_haystack - len_needle + 1): + if needle == haystack[i:i+len_needle]: + return i + + return -1 \ No newline at end of file diff --git a/src/my_project/interviews/top_150_questions_round_22/ex_24_text_justification.py b/src/my_project/interviews/top_150_questions_round_22/ex_24_text_justification.py new file mode 100644 index 00000000..c9f1dc4d --- /dev/null +++ b/src/my_project/interviews/top_150_questions_round_22/ex_24_text_justification.py @@ -0,0 +1,51 @@ +from typing import List, Union, Collection, Mapping, Optional +from abc import ABC, abstractmethod + +class Solution: + def fullJustify(self, words: List[str], maxWidth: int) -> List[str]: + """ + Performs text justification by distributing words and spaces across lines of fixed width. + + Strategy: Build lines word by word until adding the next word would exceed maxWidth. + When a line is full, distribute extra spaces evenly using round-robin. + """ + result = [] # Final list of justified lines + current_list = [] # Words being accumulated for the current line + num_of_letters = 0 # Total character count (excluding spaces) in current_list + + for word in words: + # Check if adding this word exceeds the line width + # Formula: letters + new_word_length + minimum_spaces_needed + # Minimum spaces = one space between each word = len(current_list) + if num_of_letters + len(word) + len(current_list) > maxWidth: + # Justify and add the current line to results + + # Calculate number of gaps between words (n words = n-1 gaps) + # Use max(1, ...) to handle single-word lines and avoid division by zero + num_gaps = max(1, len(current_list) - 1) + + # Distribute extra spaces using round-robin + # Total spaces needed = maxWidth - num_of_letters + extra_spaces = maxWidth - num_of_letters + for i in range(extra_spaces): + # Use modulo to cycle through gaps repeatedly + # This ensures spaces are distributed as evenly as possible + gap_index = i % num_gaps + current_list[gap_index] += ' ' + + # Join words into a single justified line and add to result + result.append("".join(current_list)) + + # Reset for next line + current_list = [] + num_of_letters = 0 + + # Add current word to the line being built + current_list.append(word) + num_of_letters += len(word) + + # form last line by join with space and left justify to maxWidth using ljust (python method) + # that means pad additional spaces to the right to make string length equal to maxWidth + result.append(" ".join(current_list).ljust(maxWidth)) + + return result \ No newline at end of file diff --git a/tests/test_150_questions_round_22/test_23_find_index_first_occurence_round_22.py b/tests/test_150_questions_round_22/test_23_find_index_first_occurence_round_22.py new file mode 100644 index 00000000..c5be5cfc --- /dev/null +++ b/tests/test_150_questions_round_22/test_23_find_index_first_occurence_round_22.py @@ -0,0 +1,17 @@ +import unittest +from src.my_project.interviews.top_150_questions_round_22\ +.ex_23_find_index_first_occurence import Solution + +class FirstOccurrenceIndexTestCase(unittest.TestCase): + + def test_first_occurrence_index(self): + solution = Solution() + output = solution.strStr(haystack="sadbutsad", needle="sad") + target = 0 + self.assertEqual(output, target) + + def test_no_first_occurrence_index(self): + solution = Solution() + output = solution.strStr(haystack="leetcode", needle="leeto") + target = -1 + self.assertEqual(output, target) \ No newline at end of file diff --git a/tests/test_150_questions_round_22/test_24_text_justification_round_22.py b/tests/test_150_questions_round_22/test_24_text_justification_round_22.py new file mode 100644 index 00000000..27367a22 --- /dev/null +++ b/tests/test_150_questions_round_22/test_24_text_justification_round_22.py @@ -0,0 +1,26 @@ +import unittest +from src.my_project.interviews.top_150_questions_round_22\ +.ex_24_text_justification import Solution + +class TextJustificationTestCase(unittest.TestCase): + + def test_first_pattern(self): + solution = Solution() + output = solution.fullJustify(words = ["This", "is", "an", "example", "of", "text", "justification."], maxWidth = 16) + target = [ + "This is an", + "example of text", + "justification. " +] + + self.assertEqual(output, target) + + def test_second_pattern(self): + solution = Solution() + output = solution.fullJustify(words = ["What","must","be","acknowledgment","shall","be"], maxWidth = 16) + target = [ + "What must be", + "acknowledgment ", + "shall be " +] + self.assertEqual(output, target) \ No newline at end of file