Appearance
question:from collections import defaultdict from typing import List def group_anagrams(words: List[str]) -> List[List[str]]: Groups a list of words into anagrams. :param words: List of strings :return: List of lists of grouped anagram strings >>> group_anagrams(['bat']) [['bat']] >>> group_anagrams(['bat', 'cat', 'dog']) [['bat'], ['cat'], ['dog']] >>> group_anagrams(['bat', 'tab', 'tap', 'pat']) [['bat', 'tab'], ['tap', 'pat']] >>> group_anagrams(['ab', 'ba', 'abc', 'bca', 'cab', 'xy', 'yx']) [['ab', 'ba'], ['abc', 'bca', 'cab'], ['xy', 'yx']] >>> group_anagrams(['abc', 'bca', 'cab', 'abc']) [['abc', 'bca', 'cab', 'abc']] anagram_dict = defaultdict(list) # Implementation here return list(anagram_dict.values())
answer:from collections import defaultdict def group_anagrams(words): Groups a list of words into anagrams. :param words: List of strings :return: List of lists of grouped anagram strings anagram_dict = defaultdict(list) for word in words: sorted_word = ''.join(sorted(word)) anagram_dict[sorted_word].append(word) return list(anagram_dict.values())
question:def add_digits(num: int) -> int: Given a non-negative integer num, repeatedly add all its digits until the result has only one digit. >>> add_digits(0) 0 >>> add_digits(5) 5 >>> add_digits(38) 2 >>> add_digits(1234) 1 >>> add_digits(987654) 3 >>> add_digits(99999) 9 >>> add_digits(10000) 1
answer:def add_digits(num): Given a non-negative integer num, repeatedly add all its digits until the result has only one digit. if num == 0: return 0 else: return 1 + (num - 1) % 9
question:def max_billboards(heights, k): Returns the maximum number of billboards that can be installed such that no two billboards overlap when viewed from the front. Parameters: heights (list of int): Heights of the buildings. k (int): Height of the billboards. Returns: int: Maximum number of billboards that can be installed. def test_all_buildings_shorter_than_k(): assert max_billboards([3, 2, 1], 4) == 0 def test_all_buildings_taller_than_k(): assert max_billboards([5, 6, 7], 4) == 3 def test_some_buildings_taller_than_k(): assert max_billboards([5, 1, 6, 3, 7], 4) == 3 def test_heights_with_gaps(): assert max_billboards([1, 4, 1, 4, 1], 4) == 2 def test_mixed_heights(): assert max_billboards([2, 4, 2, 4, 4], 4) == 3 def test_no_buildings(): assert max_billboards([], 4) == 0
answer:def max_billboards(heights, k): Returns the maximum number of billboards that can be installed such that no two billboards overlap when viewed from the front. Parameters: heights (list of int): Heights of the buildings. k (int): Height of the billboards. Returns: int: Maximum number of billboards that can be installed. if not heights: return 0 max_billboards = 0 for i, height in enumerate(heights): if height >= k: max_billboards += 1 while i + 1 < len(heights) and heights[i + 1] >= k: i += 1 return max_billboards
question:def remove_invalid_parentheses(s: str) -> List[str]: Remove the minimum number of invalid parentheses to make the string valid. Return all possible results in any order. >>> remove_invalid_parentheses("()())()") ['()()()', '(())()'] >>> remove_invalid_parentheses("(a)())()") ['(a)()()', '(a())()'] >>> remove_invalid_parentheses(")(") [""] :param s: A string containing lowercase alphabets and parentheses :return: A list of strings with all possible valid combinations
answer:def remove_invalid_parentheses(s): def is_valid(string): balance = 0 for char in string: if char == '(': balance += 1 elif char == ')': balance -= 1 if balance < 0: return False return balance == 0 level = {s} while True: valid = list(filter(is_valid, level)) if valid: return valid next_level = set() for item in level: for i in range(len(item)): if item[i] in ('(', ')'): next_level.add(item[:i] + item[i+1:]) level = next_level # Example Usage # print(remove_invalid_parentheses("()())()")) # Output: ['()()()', '(())()']