**Leetle** [2025][] · **[2026][]**  > "Leetle is a daily coding game, in the spirit of LeetCode & Wordle. > The challenges are meant to be quick and easy, but help reinforce Python > syntax and methods. The challenges are created by a mix of LLMs, mostly > Claude 3.5 Sonnet, and hand-tested. --[Ron Kiehn][]" [Ron Kiehn]: https://ronkiehn.dev/ [2025]: /leetle/doc/ckout/2025/ [2026]: /leetle/doc/ckout/2026/ # [Longest Unique Character Substring](https://leetle.app/?date=2025-01-01) Given a string `s`, find the length of the longest substring that contains no repeating characters. ## Solution ``` Python def solve(s): chars = {} left = longest = 0 for right, c in enumerate(s): if c in chars and chars[c] >= left: left = chars[c] + 1 chars[c] = right longest = max(longest, right - left + 1) return longest ``` ## Test Cases ``` Python assert (got := solve("abcabcbb")) == 3, f"{got=}" assert (got := solve("bbbbb")) == 1, f"{got=}" assert (got := solve("pwwkew")) == 3, f"{got=}" assert (got := solve("")) == 0, f"{got=}" assert (got := solve("abcdef")) == 6, f"{got=}" assert (got := solve("aab")) == 2, f"{got=}" ``` # [Simple Bracket Sequence Validator](https://leetle.app/?date=2025-01-02) Given a string `s` consisting only of opening '(' and closing ')' parentheses, determine if the input string is valid. An input string is valid if: 1. Open brackets must be closed by the same type of brackets. 2. Open brackets must be closed in the correct order. This problem is simplified to only use one type of bracket. ## Solution ``` Python def solve(s): depth = 0 for c in s: depth += 1 if c == "(" else -1 if depth < 0: return False return depth == 0 ``` ## Test Cases ``` Python assert (got := solve("()()")) is True, f"{got=}" assert (got := solve("(()())")) is True, f"{got=}" assert (got := solve(")(")) is False, f"{got=}" assert (got := solve("(((")) is False, f"{got=}" assert (got := solve("")) is True, f"{got=}" assert (got := solve("())(()")) is False, f"{got=}" ``` # [Unique Character Frequency Count](https://leetle.app/?date=2025-01-03) Given a string `s`, return the number of characters that appear exactly once in the string. If the string is empty, return 0. ## Solution ``` Python from collections import Counter def solve(s): return sum(1 for k, v in Counter(s).items() if v == 1) ``` ## Test Cases ``` Python assert (got := solve("leetcode")) == 5, f"{got=}" assert (got := solve("aabbcc")) == 0, f"{got=}" assert (got := solve("abcde")) == 5, f"{got=}" assert (got := solve("")) == 0, f"{got=}" assert (got := solve("swiss")) == 2, f"{got=}" assert (got := solve("racecar")) == 1, f"{got=}" ``` # [Path Sum to Leaf](https://leetle.app/?date=2025-01-04) Given a binary tree represented as an array in level-order (where null represents an empty node), determine if there exists a root-to-leaf path such that the sum of the values along the path equals a given target. A leaf is a node with no children. ## Solution ``` Python def solve(tree, target): def dfs(i, s): if i >= len(tree) or tree[i] is None: return False s += tree[i] left, right = 2 * i + 1, 2 * i + 2 if (left >= len(tree) or tree[left] is None) and ( right >= len(tree) or tree[right] is None ): return s == target return dfs(left, s) or dfs(right, s) if not tree: return False return dfs(0, 0) ``` ## Test Cases ``` Python assert ( got := solve([5, 4, 8, 11, None, 13, 4, 7, 2, None, None, None, 1], 22) ) is True, f"{got=}" assert (got := solve([1, 2, 3], 5)) is False, f"{got=}" assert (got := solve([1, 2], 3)) is True, f"{got=}" assert (got := solve([], 0)) is False, f"{got=}" assert (got := solve([1, None, 2], 3)) is True, f"{got=}" assert (got := solve([-2, None, -3], -5)) is True, f"{got=}" ``` # [String Array Element Sum](https://leetle.app/?date=2025-01-05) Given an array of strings, where each string represents an integer, return the sum of all these integers. If any string cannot be parsed as an integer, treat it as 0 for the summation. The input array will only contain strings composed of digits or potentially invalid characters that should result in a 0 parse. ## Solution ``` Python def solve(arr): result = 0 for i in arr: try: result += int(i) except ValueError: pass return result ``` ## Test Cases ``` Python assert (got := solve(["10", "5", "abc", "2"])) == 17, f"{got=}" assert (got := solve(["100", "200", "300"])) == 600, f"{got=}" assert (got := solve(["invalid", "data", "test"])) == 0, f"{got=}" assert (got := solve(["1", "1", "1", "1", "1"])) == 5, f"{got=}" assert (got := solve([])) == 0, f"{got=}" assert (got := solve(["999", "0", "1"])) == 1000, f"{got=}" ``` # [Maximum Sum of Non-Overlapping Subarrays](https://leetle.app/?date=2025-01-06) Given an array of positive integers `nums` and an integer `k`, find the maximum sum you can obtain by selecting two non-overlapping subarrays, each of length exactly `k`. The two subarrays must not share any elements. ## Solution ``` Python def solve(nums, k): result = left = 0 n = len(nums) s = [0] * (n + 1) for i in range(n): s[i + 1] = s[i] + nums[i] for i in range(2 * k - 1, n): left = max(left, s[i - k + 1] - s[i - 2 * k + 1]) result = max(result, left + s[i + 1] - s[i - k + 1]) return result ``` ## Test Cases ``` Python assert (got := solve([1, 2, 1, 2, 6, 7, 5, 1], 2)) == 20, f"{got=}" assert (got := solve([4, 3, 2, 6, 2, 3, 4], 1)) == 10, f"{got=}" assert (got := solve([10, 1, 1, 10, 1, 1, 10], 3)) == 24, f"{got=}" assert (got := solve([1, 1, 1, 1, 1, 1], 2)) == 4, f"{got=}" assert (got := solve([5, 5, 5, 5, 5, 5, 5], 3)) == 30, f"{got=}" assert (got := solve([1, 5, 2, 8, 3, 4], 2)) == 18, f"{got=}" ``` # [Stack Based Bracket Depth Sum](https://leetle.app/?date=2025-01-07) Given a string `s` consisting only of parentheses '(', ')', '{', '}', '[' and ']', determine the maximum nesting depth of validly matched brackets. The depth of an empty string is 0. The depth of a string containing only non-bracket characters is 0. The depth of a string like `(A)` or `[A]` or `{A}` is `1 + (A)`, where `A` is the content inside the brackets. The depth of a concatenation `AB` is `max(depth(A), depth(B))`. You must use a stack-like approach to track the current depth. ## Solution ``` Python def solve(s): opens, closes = "([{", ")]}" depth = result = 0 for c in s: depth += 1 if c in opens else -1 if c in closes else 0 result = max(result, depth) return result ``` ## Test Cases ``` Python assert (got := solve("{([])}")) == 3, f"{got=}" assert (got := solve("()[]{}")) == 1, f"{got=}" assert (got := solve("((()))")) == 3, f"{got=}" assert (got := solve("")) == 0, f"{got=}" assert (got := solve("([{}])()(())")) == 3, f"{got=}" assert (got := solve("a(b{c[d]e}f)g")) == 3, f"{got=}" ``` # [Count Unique Element Frequencies](https://leetle.app/?date=2025-01-08) Given an array of integers `arr`, return an array where each element represents the frequency of that element in the input array. However, a frequency value should only be outputted the first time it is encountered during a linear scan of the array. If the frequency of the current element has already been outputted for a previous element, return 0 for that position. ## Solution ``` Python from collections import Counter def solve(arr): counts, result = Counter(arr), [] for i in arr: result.append(0 if counts[i] in result else counts[i]) return result ``` ## Test Cases ``` Python assert (got := solve([1, 2, 2, 3, 3, 3])) == [1, 2, 0, 3, 0, 0], f"{got=}" assert (got := solve([10, 10, 20, 20])) == [2, 0, 0, 0], f"{got=}" assert (got := solve([5, 5, 5, 5, 5])) == [5, 0, 0, 0, 0], f"{got=}" assert (got := solve([1, 1, 2, 2, 3, 3, 4, 4])) == [2, 0, 0, 0, 0, 0, 0, 0], f"{got=}" assert (got := solve([7])) == [1], f"{got=}" assert (got := solve([])) == [], f"{got=}" ``` # [Smallest Path Sum in Directed Acyclic Graph](https://leetle.app/?date=2025-01-09) Given a directed acyclic graph (DAG) represented by an adjacency list where each edge has a weight, find the path from a specified starting node to a specified ending node that yields the minimum total weight sum. If no path exists, return -1. Assume nodes are labeled from 0 to N-1. The input graph structure is an adjacency list where `graph[u]` is a list of `[v, weight]` pairs, representing an edge from `u` to `v` with the given weight. ## Solution ``` Python import heapq def solve(N, graph, start, end): ds = [float("inf") for i in range(N)] pq = [(0, start)] while pq: d, u = heapq.heappop(pq) if d > ds[u]: continue if u == end: return d for v, weight in graph[u]: if d + weight < ds[v]: ds[v] = d + weight heapq.heappush(pq, (d + weight, v)) return -1 if ds[end] == float("inf") else ds[end] ``` ## Test Cases ``` Python assert (got := solve(4, [[[1, 3], [2, 1]], [[3, 2]], [[3, 5]], []], 0, 3)) == 5, ( f"{got=}" ) assert ( got := solve(5, [[[1, 10]], [[2, 1]], [[3, 1], [4, 5]], [[4, 1]], []], 0, 4) ) == 13, f"{got=}" assert (got := solve(3, [[[1, 100]], [], [[1, 1]]], 0, 1)) == 100, f"{got=}" assert (got := solve(4, [[[1, 1]], [[2, 1]], [[3, 1]], []], 3, 0)) == -1, f"{got=}" assert (got := solve(2, [[[1, 5]], []], 0, 0)) == 0, f"{got=}" assert ( got := solve( 6, [[[1, 1], [2, 10]], [[3, 1]], [[4, 1]], [[5, 1]], [[5, 10]], []], 0, 5 ) ) == 3, f"{got=}" ``` # [String Array Character Sum](https://leetle.app/?date=2025-01-10) Given an array of strings, calculate the total number of characters across all strings in the array. ## Solution ``` Python def solve(words): return sum(len(i) for i in words) ``` ## Test Cases ``` Python assert (got := solve(["apple", "banana", "kiwi"])) == 15, f"{got=}" assert (got := solve(["a", "b", "c", "d"])) == 4, f"{got=}" assert (got := solve([])) == 0, f"{got=}" assert (got := solve(["one", ""])) == 3, f"{got=}" assert (got := solve(["longstring", "short"])) == 15, f"{got=}" assert (got := solve(["x"])) == 1, f"{got=}" ``` # [Minimum Subarray Sum Length](https://leetle.app/?date=2025-01-11) Given an array of positive integers `nums` and a positive integer `target`, find the minimal length of a contiguous subarray whose sum is greater than or equal to `target`. If no such subarray exists, return 0. ## Solution ``` Python def solve(target, nums): result = float("inf") left = current = 0 for right in range(len(nums)): current += nums[right] while current >= target: result = min(result, right - left + 1) current -= nums[left] left += 1 return 0 if result == float("inf") else result ``` ## Test Cases ``` Python assert (got := solve(7, [2, 3, 1, 2, 4, 3])) == 2, f"{got=}" assert (got := solve(4, [1, 4, 4])) == 1, f"{got=}" assert (got := solve(11, [1, 1, 1, 1, 1, 1, 1, 1])) == 0, f"{got=}" assert (got := solve(15, [5, 1, 3, 5, 10, 7, 4, 9, 2, 8])) == 2, f"{got=}" assert (got := solve(1, [100])) == 1, f"{got=}" assert (got := solve(10, [1, 2, 3, 4, 5])) == 3, f"{got=}" ``` # [Valid Stack Sequence Operations](https://leetle.app/?date=2025-01-12) Given a sequence of pushed elements `pushed` and a sequence of popped elements `popped`, determine if `popped` could have been the result of a sequence of push and pop operations on an initially empty stack using only the elements from `pushed` in order. The elements in `pushed` are distinct. ## Solution ``` Python def solve(pushed, popped): stack, pop = [], 0 for push in pushed: stack.append(push) while stack and stack[-1] == popped[pop]: stack.pop() pop += 1 return pop == len(popped) ``` ## Test Cases ``` Python assert (got := solve([1, 2, 3, 4, 5], [4, 5, 3, 2, 1])) is True, f"{got=}" assert (got := solve([1, 2, 3, 4, 5], [4, 3, 5, 1, 2])) is False, f"{got=}" assert (got := solve([1, 0], [1, 0])) is True, f"{got=}" assert (got := solve([1, 0], [0, 1])) is True, f"{got=}" assert (got := solve([10, 20, 30], [30, 20, 10])) is True, f"{got=}" assert (got := solve([1, 2, 3], [3, 1, 2])) is False, f"{got=}" ``` # [Unique Element Sum](https://leetle.app/?date=2025-01-13) Given an array of integers `nums`, return the sum of all elements that appear exactly once in the array. If no elements appear exactly once, return 0. ## Solution ``` Python from collections import Counter def solve(nums): return sum(k for k, v in Counter(nums).items() if v == 1) ``` ## Test Cases ``` Python assert (got := solve([1, 2, 3, 2])) == 4, f"{got=}" assert (got := solve([1, 1, 1, 1])) == 0, f"{got=}" assert (got := solve([1, 2, 3, 4, 5])) == 15, f"{got=}" assert (got := solve([10, 20, 10, 30, 40, 30])) == 60, f"{got=}" assert (got := solve([])) == 0, f"{got=}" assert (got := solve([5, 7, 5])) == 7, f"{got=}" ``` # [Find Root in Binary Tree](https://leetle.app/?date=2025-01-14) Given the root of a binary tree, determine if a specific target node value exists within the tree. The tree nodes are defined by a `val` attribute for the node's value, and `left` and `right` attributes for child pointers. Return `True` if the target is found, and `False` otherwise. Assume the tree structure is provided as a dictionary representation where `None` indicates no child. ## Solution ``` Python def solve(root, target): if not root: return False if root["val"] == target: return True return solve(root["left"], target) or solve(root["right"], target) ``` ## Test Cases ``` Python assert ( got := solve( { "left": {"left": None, "right": None, "val": 2}, "right": {"left": None, "right": None, "val": 3}, "val": 1, }, 2, ) ) is True, f"{got=}" assert ( got := solve( { "left": { "left": {"left": None, "right": None, "val": 1}, "right": {"left": None, "right": None, "val": 7}, "val": 5, }, "right": {"left": None, "right": None, "val": 15}, "val": 10, }, 15, ) ) is True, f"{got=}" assert ( got := solve( { "left": { "left": {"left": None, "right": None, "val": 1}, "right": {"left": None, "right": None, "val": 7}, "val": 5, }, "right": {"left": None, "right": None, "val": 15}, "val": 10, }, 99, ) ) is False, f"{got=}" assert (got := solve({"left": None, "right": None, "val": 50}, 50)) is True, f"{got=}" assert (got := solve(None, 1)) is False, f"{got=}" assert ( got := solve( { "left": { "left": {"left": None, "right": None, "val": 4}, "right": {"left": None, "right": None, "val": 5}, "val": 2, }, "right": { "left": {"left": None, "right": None, "val": 6}, "right": {"left": None, "right": None, "val": 7}, "val": 3, }, "val": 1, }, 6, ) ) is True, f"{got=}" ``` # [Count Words with Prefix](https://leetle.app/?date=2025-01-15) Given an array of strings `words` and a string `pref`, return the number of strings in `words` that contain `pref` as a prefix. A prefix of a string `s` is any leading contiguous substring of `s`. ## Solution ``` Python def solve(words, pref): return sum(word.startswith(pref) for word in words) ``` ## Test Cases ``` Python assert (got := solve(["pay", "attention", "practice", "attend"], "at")) == 2, f"{got=}" assert (got := solve(["leetcode", "win", "loops", "success"], "code")) == 0, f"{got=}" assert (got := solve(["apple", "app", "apricot", "apply"], "app")) == 3, f"{got=}" assert (got := solve(["a", "a", "a"], "a")) == 3, f"{got=}" assert (got := solve(["hello", "world"], "h")) == 1, f"{got=}" assert (got := solve(["blue", "black", "blank", "bright"], "bl")) == 3, f"{got=}" ``` # [Max Sum Subarray With Fixed Window](https://leetle.app/?date=2025-01-16) Given an array of integers `nums` and an integer `k`, find the maximum sum of a contiguous subarray of size exactly `k`. You must use the two-pointers or sliding window technique. ## Solution ``` Python def solve(nums, k): current = result = sum(nums[:k]) for i in range(k, len(nums)): current += -nums[i - k] + nums[i] result = max(result, current) return result ``` ## Test Cases ``` Python assert (got := solve([1, 4, 2, 10, 2, 3, 1, 0, 20], 3)) == 21, f"{got=}" assert (got := solve([100, 200, 300, 400], 2)) == 700, f"{got=}" assert (got := solve([-1, -2, -3, -4, -5], 1)) == -1, f"{got=}" assert (got := solve([1, 1, 1, 1, 1, 1], 4)) == 4, f"{got=}" assert (got := solve([5, -2, 7, 1, -9, 3], 4)) == 11, f"{got=}" assert (got := solve([10], 1)) == 10, f"{got=}" ``` # [Simple Stack Bracket Depth](https://leetle.app/?date=2025-01-17) Given a string `s` consisting only of parentheses '(', ')', '{', '}', '[' and ']', determine the maximum nesting depth of the valid parentheses structure. The input string is guaranteed to represent a valid sequence of brackets. For example, in "(()(()))", the maximum depth is 3. In "{}{[]}", the maximum depth is 2 (from the inner '[]' within '{}'). ## Solution ``` Python def solve(s): opens, closes = "([{", ")]}" depth = result = 0 for c in s: depth += 1 if c in opens else -1 if c in closes else 0 result = max(result, depth) return result ``` ## Test Cases ``` Python assert (got := solve("(()(()))")) == 3, f"{got=}" assert (got := solve("()()()")) == 1, f"{got=}" assert (got := solve("{}{[]}")) == 2, f"{got=}" assert (got := solve("((()))[[]]{}")) == 3, f"{got=}" assert (got := solve("")) == 0, f"{got=}" assert (got := solve("([{}])")) == 3, f"{got=}" ``` # [Unique Character Frequency Check](https://leetle.app/?date=2025-01-18) Given a string `s`, determine if all characters in the string appear the same number of times. Return `True` if the frequencies are uniform, otherwise return `False`. An empty string should return `True`. ## Solution ``` Python from collections import Counter def solve(s): return not s or len(set(Counter(s).values())) == 1 ``` ## Test Cases ``` Python assert (got := solve("aabb")) is True, f"{got=}" assert (got := solve("abc")) is True, f"{got=}" assert (got := solve("aabbccddeeffg")) is False, f"{got=}" assert (got := solve("aaabbbccc")) is True, f"{got=}" assert (got := solve("")) is True, f"{got=}" assert (got := solve("racecar")) is False, f"{got=}" ``` # [Find Rooted Tree Diameter Path](https://leetle.app/?date=2025-01-19) Given a tree represented by an adjacency list where each edge has a weight of 1 (unweighted graph), find the length of the longest path between any two nodes (the diameter). The input is an array of edges. Assume the graph is connected and acyclic (a tree). ## Solution ``` Python def solve(n, edges): def dfs(node, previous, distance): for neighbor in graph[node]: if neighbor != previous: dfs(neighbor, node, distance + 1) nonlocal max_distance, farthest_node if max_distance < distance: max_distance = distance farthest_node = node graph = {i: [] for i in range(n)} for f, t in edges: graph[f].append(t) graph[t].append(f) max_distance = farthest_node = 0 dfs(0, -1, 0) max_distance = 0 dfs(farthest_node, -1, 0) return max_distance ``` ## Test Cases ``` Python assert (got := solve(6, [[0, 1], [1, 2], [1, 3], [3, 4]])) == 3, f"{got=}" assert (got := solve(1, [])) == 0, f"{got=}" assert (got := solve(2, [[0, 1]])) == 1, f"{got=}" assert (got := solve(7, [[0, 1], [1, 2], [2, 3], [3, 4], [4, 5], [5, 6]])) == 6, ( f"{got=}" ) assert (got := solve(6, [[0, 1], [0, 2], [1, 3], [1, 4], [2, 5]])) == 4, f"{got=}" assert ( got := solve(8, [[0, 1], [0, 2], [1, 3], [1, 4], [2, 5], [5, 6], [6, 7]]) ) == 6, f"{got=}" ``` # [Count Words Ending with S](https://leetle.app/?date=2025-01-20) Given an array of strings `words`, return the number of strings that end with the character 's' (case-insensitive). ## Solution ``` Python def solve(words): return sum(w.lower().endswith("s") for w in words) ``` ## Test Cases ``` Python assert (got := solve(["Dogs", "cat", "Buses"])) == 2, f"{got=}" assert (got := solve(["apple", "banana", "cherry"])) == 0, f"{got=}" assert (got := solve(["S", "is", "chess"])) == 3, f"{got=}" assert (got := solve(["YES", "no", "MaybeS"])) == 2, f"{got=}" assert (got := solve([])) == 0, f"{got=}" assert (got := solve(["pass", "fail", "glass", "wood"])) == 2, f"{got=}" ``` # [Maximum Sum Subarray of Size K](https://leetle.app/?date=2025-01-21) Given an array of integers `nums` and an integer `k`, find the maximum sum of any contiguous subarray of size exactly `k`. You must use the two-pointers or sliding window technique. ## Solution ``` Python def solve(nums, k): current = result = sum(nums[:k]) for i in range(k, len(nums)): current += -nums[i - k] + nums[i] result = max(result, current) return result ``` ## Test Cases ``` Python assert (got := solve([2, 1, 5, 1, 3, 2], 3)) == 9, f"{got=}" assert (got := solve([2, 3, 4, 1, 5], 2)) == 7, f"{got=}" assert (got := solve([1, 1, 1, 1, 1, 1], 4)) == 4, f"{got=}" assert (got := solve([10, -5, 20, 1, 5], 1)) == 20, f"{got=}" assert (got := solve([1, 5, 2, 8, 1, 9], 4)) == 20, f"{got=}" assert (got := solve([100, 200, 300, 400], 4)) == 1000, f"{got=}" ``` # [Find First Unique Character Index](https://leetle.app/?date=2025-01-22) Given a string `s`, find the first non-repeating character in it and return its index. If it does not exist, return -1. ## Solution ``` Python def solve(s): for i, c in enumerate(s): if c not in s[:i] + s[i + 1 :]: return i return -1 ``` ## Test Cases ``` Python assert (got := solve("leetcode")) == 0, f"{got=}" assert (got := solve("loveleetcode")) == 2, f"{got=}" assert (got := solve("aabb")) == -1, f"{got=}" assert (got := solve("z")) == 0, f"{got=}" assert (got := solve("ccbaabd")) == 6, f"{got=}" assert (got := solve("")) == -1, f"{got=}" ``` # [Count Unique Elements Frequency](https://leetle.app/?date=2025-01-23) Given an array of integers `nums`, return the count of how many distinct numbers appear exactly the same number of times as their value. For example, if the number 3 appears exactly 3 times, it contributes to the final count. If the number 5 appears 2 times, it does not contribute. ## Solution ``` Python from collections import Counter def solve(nums): return sum(n == c for n, c in Counter(nums).items()) ``` ## Test Cases ``` Python assert (got := solve([2, 2, 3, 3, 3, 4])) == 2, f"{got=}" assert (got := solve([1, 1, 1, 2, 2, 5])) == 1, f"{got=}" assert (got := solve([5, 5, 5, 5, 5])) == 1, f"{got=}" assert (got := solve([10, 10, 10, 10, 10, 10, 10, 10, 10, 10])) == 1, f"{got=}" assert (got := solve([1, 2, 3, 4, 5])) == 1, f"{got=}" assert (got := solve([7, 7, 7, 7, 7, 7, 7, 1, 2, 2])) == 3, f"{got=}" ``` # [Find Root of Binary Tree](https://leetle.app/?date=2025-01-24) Given the root of a binary tree, determine if it is a valid Binary Search Tree (BST). A valid BST is defined as follows: The left subtree of a node contains only nodes with keys less than the node's key. The right subtree of a node contains only nodes with keys greater than the node's key. Both the left and right subtrees must also be binary search trees. Assume the input tree structure uses nodes with `val`, `left`, and `right` attributes. Return `True` if it is a valid BST, and `False` otherwise. The input will be the root node object or `None` if the tree is empty. ## Solution ``` Python class TreeNode: def __init__(self, val=0, left=None, right=None): self.val = val self.left = left self.right = right def solve(root): def build(data): if data: return TreeNode(data["val"], build(data["left"]), build(data["right"])) def validate(node, lo, hi): if not node or not (lo < node.val < hi): return not node return validate(node.left, lo, node.val) and validate(node.right, node.val, hi) return validate(build(root), float("-inf"), float("inf")) ``` ## Test Cases ``` Python assert ( got := solve( { "left": {"left": None, "right": None, "val": 1}, "right": {"left": None, "right": None, "val": 3}, "val": 2, } ) ) is True, f"{got=}" assert ( got := solve( { "left": {"left": None, "right": None, "val": 1}, "right": { "left": {"left": None, "right": None, "val": 3}, "right": {"left": None, "right": None, "val": 6}, "val": 4, }, "val": 5, } ) ) is False, f"{got=}" assert (got := solve(None)) is True, f"{got=}" assert ( got := solve( {"left": {"left": None, "right": None, "val": 1}, "right": None, "val": 1} ) ) is False, f"{got=}" assert ( got := solve( { "left": { "left": {"left": None, "right": None, "val": 2}, "right": {"left": None, "right": None, "val": 7}, "val": 5, }, "right": { "left": {"left": None, "right": None, "val": 12}, "right": {"left": None, "right": None, "val": 20}, "val": 15, }, "val": 10, } ) ) is True, f"{got=}" assert ( got := solve( { "left": {"left": None, "right": None, "val": 4}, "right": { "left": {"left": None, "right": None, "val": 3}, "right": None, "val": 6, }, "val": 5, } ) ) is False, f"{got=}" ``` # [Count Alternating Parity Bits](https://leetle.app/?date=2025-01-25) Given an array of integers `nums`, count how many elements have a different parity (even/odd) than the element immediately preceding them. The first element is never counted as it has no predecessor. ## Solution ``` Python def solve(nums): return sum((nums[i - 1] ^ nums[i]) & 1 for i in range(1, len(nums))) ``` ## Test Cases ``` Python assert (got := solve([1, 2, 4, 5])) == 2, f"{got=}" assert (got := solve([2, 4, 6, 8])) == 0, f"{got=}" assert (got := solve([1, 3, 5])) == 0, f"{got=}" assert (got := solve([1, 2, 1, 2, 1])) == 4, f"{got=}" assert (got := solve([10])) == 0, f"{got=}" assert (got := solve([10, 11, 13, 14, 16])) == 2, f"{got=}" ``` # [Longest Balanced Binary Substring](https://leetle.app/?date=2025-01-26) Given a binary string `s` consisting only of '0's and '1's, find the length of the longest substring where all '0's come before all '1's, and the number of '0's is equal to the number of '1's. ## Solution ``` Python def solve(s): cs, c, result = [], 1, 0 for i in range(1, len(s)): if s[i] == s[i - 1]: c += 1 else: cs.append((s[i - 1], c)) c = 1 cs.append((s[-1], c)) for i in range(len(cs) - 1): (xc, xl), (yc, yl) = cs[i], cs[i + 1] if xc == "0" and yc == "1": result = max(result, min(xl, yl) * 2) return result ``` ## Test Cases ``` Python assert (got := solve("010011")) == 4, f"{got=}" assert (got := solve("00111")) == 4, f"{got=}" assert (got := solve("111")) == 0, f"{got=}" assert (got := solve("00011")) == 4, f"{got=}" assert (got := solve("001")) == 2, f"{got=}" assert (got := solve("111000")) == 0, f"{got=}" ``` # [Simple Stack Sequence Validator](https://leetle.app/?date=2025-01-27) Given a sequence of numbers `pushed` and a sequence `popped`, determine if `popped` could have been the result of a sequence of push and pop operations on an initially empty stack using the elements from `pushed` in order. The input arrays will contain unique elements. ## Solution ``` Python def solve(pushed, popped): stack, pop = [], 0 for push in pushed: stack.append(push) while stack and stack[-1] == popped[pop]: stack.pop() pop += 1 return pop == len(popped) ``` ## Test Cases ``` Python assert (got := solve([1, 2, 3, 4, 5], [4, 5, 3, 2, 1])) is True, f"{got=}" assert (got := solve([1, 2, 3, 4, 5], [4, 3, 5, 1, 2])) is False, f"{got=}" assert (got := solve([1, 0], [1, 0])) is True, f"{got=}" assert (got := solve([1, 0], [0, 1])) is True, f"{got=}" assert (got := solve([1, 2, 3], [3, 2, 1])) is True, f"{got=}" assert (got := solve([10, 20, 30, 40, 50], [50, 40, 30, 20, 10])) is True, f"{got=}" ``` # [Unique Character Frequency Sum](https://leetle.app/?date=2025-01-28) Given a string `s`, calculate the sum of the frequencies of all characters that appear exactly once in the string. If no character appears exactly once, return 0. ## Solution ``` Python from collections import Counter def solve(s): return sum(1 for k, v in Counter(s).items() if v == 1) ``` ## Test Cases ``` Python assert (got := solve("aabbc")) == 1, f"{got=}" assert (got := solve("abcde")) == 5, f"{got=}" assert (got := solve("aabbcc")) == 0, f"{got=}" assert (got := solve("")) == 0, f"{got=}" assert (got := solve("apple")) == 3, f"{got=}" assert (got := solve("racecar")) == 1, f"{got=}" ``` # [Binary Tree Path Sums](https://leetle.app/?date=2025-01-29) Given the root of a binary tree represented as a list in level-order (where `None` represents a missing node), return the sum of all values in the tree. If the tree is empty, return 0. ## Solution ``` Python def solve(root): return sum(i for i in root if i) ``` ## Test Cases ``` Python assert (got := solve([1, 2, 3])) == 6, f"{got=}" assert (got := solve([10, 5, 15, None, None, 20])) == 50, f"{got=}" assert (got := solve([])) == 0, f"{got=}" assert (got := solve([5, None, 10])) == 15, f"{got=}" assert (got := solve([-1, -2, -3])) == -6, f"{got=}" assert (got := solve([1, 2, None, 3, None, None, None, 4])) == 10, f"{got=}" ``` # [Filter Digits from String](https://leetle.app/?date=2025-01-30) Given a string `s`, return a new string containing only the numeric digits (0-9) found in the original string, in the order they appear. If no digits are found, return an empty string. ## Solution ``` Python def solve(s): return "".join(filter(str.isdigit, s)) ``` ## Test Cases ``` Python assert (got := solve("abc123def")) == "123", f"{got=}" assert (got := solve("hello")) == "", f"{got=}" assert (got := solve("1a2b3c")) == "123", f"{got=}" assert (got := solve("9876543210")) == "9876543210", f"{got=}" assert (got := solve("No digits here!")) == "", f"{got=}" assert (got := solve("Room 404")) == "404", f"{got=}" ``` # [Max Sum Subarray Fixed Size](https://leetle.app/?date=2025-01-31) Given an array of integers `nums` and an integer `k`, find the maximum sum of any contiguous subarray of size exactly `k`. You must use the two-pointers or sliding window technique. ## Solution ``` Python def solve(nums, k): current = result = sum(nums[:k]) for i in range(k, len(nums)): current += -nums[i - k] + nums[i] result = max(result, current) return 0 if k > len(nums) else result ``` ## Test Cases ``` Python assert (got := solve([2, 1, 5, 1, 3, 2], 3)) == 9, f"{got=}" assert (got := solve([1, 4, 2, 10, 23, 3, 1, 0, 20], 4)) == 39, f"{got=}" assert (got := solve([-1, -4, -2, -10], 2)) == -5, f"{got=}" assert (got := solve([100, 200, 300, 400], 1)) == 400, f"{got=}" assert (got := solve([5, 5, 5, 5, 5], 5)) == 25, f"{got=}" assert (got := solve([1, 2, 3, 4, 5], 6)) == 0, f"{got=}" ``` # [Sum of Leaf Nodes](https://leetle.app/?date=2025-02-01) Given a binary tree represented as a list in level-order format (where `None` represents a missing node), calculate the sum of all leaf nodes. A leaf node is a node that has no children. ## Solution ``` Python def solve(root): kids = root[1:][::-1] total = 0 for node in root: if node: if not kids: total += node else: kids = kids[:-2] return total ``` ## Test Cases ``` Python assert (got := solve([1, 2, 3, 4, None, None, 5])) == 9, f"{got=}" assert (got := solve([10, 5, 15])) == 20, f"{got=}" assert (got := solve([1, None, 2, None, 3])) == 3, f"{got=}" assert (got := solve([5])) == 5, f"{got=}" assert (got := solve([])) == 0, f"{got=}" assert (got := solve([1, 2, None, 4, 5])) == 9, f"{got=}" ``` # [Stack Balanced Bracket Depth](https://leetle.app/?date=2025-02-02) Given a string `s` containing only parentheses '(', ')', '{', '}', '[' and ']', determine the maximum nesting depth of the validly matched brackets. If the string is not validly formed (e.g., unmatched brackets), return -1. For example, in `"{([])}"`, the depth is 3 (curly, then square, then round). ## Solution ``` Python def solve(s): result = 0 stack = [] brackets = dict(zip(")]}", "([{")) for c in s: if c in brackets.values(): stack.append(c) result = max(result, len(stack)) elif stack and brackets.get(c) == stack[-1]: stack.pop() else: return -1 return -1 if stack else result ``` ## Test Cases ``` Python assert (got := solve("{([()])}")) == 4, f"{got=}" assert (got := solve("()[]{}")) == 1, f"{got=}" assert (got := solve("((()))")) == 3, f"{got=}" assert (got := solve("([{}])")) == 3, f"{got=}" assert (got := solve("(((")) == -1, f"{got=}" assert (got := solve("([)]")) == -1, f"{got=}" ``` # [Unique Sum of First Occurrences](https://leetle.app/?date=2025-02-03) Given an array of integers `nums`, return the sum of all numbers that appear exactly once in the array. Use a hash map to count frequencies. ## Solution ``` Python from collections import Counter def solve(nums): return sum(k for k, v in Counter(nums).items() if v == 1) ``` ## Test Cases ``` Python assert (got := solve([1, 2, 3, 2])) == 4, f"{got=}" assert (got := solve([1, 1, 1, 1, 1])) == 0, f"{got=}" assert (got := solve([4, 5, 6, 4, 7])) == 18, f"{got=}" assert (got := solve([10])) == 10, f"{got=}" assert (got := solve([])) == 0, f"{got=}" assert (got := solve([5, 5, 8, 9, 8, 1])) == 10, f"{got=}" ``` # [Find the Longest Word](https://leetle.app/?date=2025-02-04) Given a string `s` containing words separated by single spaces, return the length of the longest word. If the string is empty or contains no words, return 0. ## Solution ``` Python def solve(s): return max(len(i) for i in s.split()) if s.strip() else 0 ``` ## Test Cases ``` Python assert (got := solve("the quick brown fox")) == 5, f"{got=}" assert (got := solve("hello")) == 5, f"{got=}" assert (got := solve("")) == 0, f"{got=}" assert (got := solve("a bb ccc dddd")) == 4, f"{got=}" assert (got := solve("coding is fun")) == 6, f"{got=}" assert (got := solve(" ")) == 0, f"{got=}" ``` # [Longest Subarray With Sum K](https://leetle.app/?date=2025-02-05) Given an array of positive integers `nums` and an integer `k`, find the length of the longest contiguous subarray whose sum is equal to `k`. If no such subarray exists, return 0. ## Solution ``` Python def solve(nums, k): current = left = result = 0 for right in range(len(nums)): current += nums[right] while current > k: current -= nums[left] left += 1 if current == k: result = max(result, right - left + 1) return result ``` ## Test Cases ``` Python assert (got := solve([1, 2, 1, 1, 1], 3)) == 3, f"{got=}" assert (got := solve([1, 2, 3], 3)) == 2, f"{got=}" assert (got := solve([4, 5, 6], 3)) == 0, f"{got=}" assert (got := solve([1, 1, 1, 1, 1], 5)) == 5, f"{got=}" assert (got := solve([10, 2, 3], 5)) == 2, f"{got=}" assert (got := solve([1, 2, 1, 3], 4)) == 3, f"{got=}" ``` # [Count Leaf Nodes](https://leetle.app/?date=2025-02-06) Given a representation of a binary tree as a list where each element is a list `[value, left_child, right_child]`, count the number of leaf nodes. A leaf node is a node that has no children (both `left_child` and `right_child` are null). ## Solution ``` Python def solve(tree): if not tree: return 0 if not tree[1] and not tree[2]: return 1 return solve(tree[1]) + solve(tree[2]) ``` ## Test Cases ``` Python assert (got := solve([1, [2, None, None], [3, None, None]])) == 2, f"{got=}" assert (got := solve([1, None, None])) == 1, f"{got=}" assert (got := solve([1, [2, [4, None, None], None], [3, None, None]])) == 2, f"{got=}" assert (got := solve(None)) == 0, f"{got=}" assert (got := solve([1, [2, [3, [4, None, None], None], None], None])) == 1, f"{got=}" assert ( got := solve([1, [2, None, None], [3, [4, None, None], [5, None, None]]]) ) == 3, f"{got=}" ``` # [Simple Bracket Sequence Depth](https://leetle.app/?date=2025-02-07) Given a string `s` consisting only of opening '(' and closing ')' parentheses, find the maximum nesting depth of the parentheses. The depth is the maximum number of open brackets encountered before a matching closing bracket is found. Assume the input string is a valid sequence (i.e., it is balanced). ## Solution ``` Python def solve(s): result = current = 0 for c in s: current += 1 if c == "(" else -1 result = max(result, current) return result ``` ## Test Cases ``` Python assert (got := solve("()(())")) == 2, f"{got=}" assert (got := solve("((()))")) == 3, f"{got=}" assert (got := solve("()()()")) == 1, f"{got=}" assert (got := solve("")) == 0, f"{got=}" assert (got := solve("(()(()))")) == 3, f"{got=}" assert (got := solve("((()()))")) == 3, f"{got=}" ``` # [Find Most Frequent Even Element](https://leetle.app/?date=2025-02-08) Given an array of integers `nums`, return the smallest even integer that appears most frequently in the array. If there is a tie, return the smallest one. If no even element exists, return -1. ## Solution ``` Python def solve(nums): d = {} for i in {i for i in nums if i % 2 == 0}: d.setdefault(nums.count(i), []).append(i) return min(d[max(d)]) if d else -1 ``` ## Test Cases ``` Python assert (got := solve([0, 1, 2, 2, 4, 4, 1])) == 2, f"{got=}" assert (got := solve([4, 4, 4, 9, 2, 4])) == 4, f"{got=}" assert (got := solve([29, 47, 21])) == -1, f"{got=}" assert (got := solve([10000, 10000, 2, 2, 3])) == 2, f"{got=}" assert (got := solve([1, 2, 1, 2, 1, 2])) == 2, f"{got=}" assert (got := solve([8, 8, 10, 10, 12, 12])) == 8, f"{got=}" ``` # [Sum of Positive Array Elements](https://leetle.app/?date=2025-02-09) Given an array of integers, return the sum of all positive numbers in the array. If the array is empty or contains no positive numbers, return 0. ## Solution ``` Python def solve(arr): return sum(i for i in arr if i > 0) ``` ## Test Cases ``` Python assert (got := solve([1, -4, 7, 12])) == 20, f"{got=}" assert (got := solve([-1, -2, -3, -4])) == 0, f"{got=}" assert (got := solve([10, 5, 0, 2])) == 17, f"{got=}" assert (got := solve([])) == 0, f"{got=}" assert (got := solve([5])) == 5, f"{got=}" assert (got := solve([-100, 1, 100, -1])) == 101, f"{got=}" ``` # [Longest Subarray of Ones](https://leetle.app/?date=2025-02-10) Given a binary array `nums`, you should delete one element from it. Return the size of the longest non-empty subarray containing only 1's in the resulting array. Return 0 if there is no such subarray. ## Solution ``` Python def solve(nums): left = deleted = answer = 0 for right, num in enumerate(nums): deleted += num == 0 if deleted > 1: deleted -= nums[left] == 0 left += 1 answer = max(answer, right - left) return answer ``` ## Test Cases ``` Python assert (got := solve([1, 1, 0, 1])) == 3, f"{got=}" assert (got := solve([0, 1, 1, 1, 0, 1, 1, 0])) == 5, f"{got=}" assert (got := solve([1, 1, 1])) == 2, f"{got=}" assert (got := solve([0, 0, 0])) == 0, f"{got=}" assert (got := solve([1, 0, 0, 1])) == 1, f"{got=}" assert (got := solve([1, 1, 0, 0, 1, 1, 1, 0, 1])) == 4, f"{got=}" ``` # [Simple Tree Node Value Sum](https://leetle.app/?date=2025-02-11) Given the root of a binary tree, return the sum of the values of all its nodes. Assume the tree nodes have an integer attribute `val`. ## Solution ``` Python def solve(root): return root["val"] + solve(root["left"]) + solve(root["right"]) if root else 0 ``` ## Test Cases ``` Python assert ( got := solve( { "left": {"left": None, "right": None, "val": 2}, "right": {"left": None, "right": None, "val": 3}, "val": 1, } ) ) == 6, f"{got=}" assert ( got := solve( { "left": { "left": {"left": None, "right": None, "val": 1}, "right": {"left": None, "right": None, "val": 7}, "val": 5, }, "right": {"left": None, "right": None, "val": 15}, "val": 10, } ) ) == 38, f"{got=}" assert (got := solve(None)) == 0, f"{got=}" assert ( got := solve( {"left": {"left": None, "right": None, "val": 5}, "right": None, "val": -5} ) ) == 0, f"{got=}" assert (got := solve({"left": None, "right": None, "val": 100})) == 100, f"{got=}" assert ( got := solve( { "left": { "left": {"left": None, "right": None, "val": 1}, "right": None, "val": 1, }, "right": { "left": None, "right": {"left": None, "right": None, "val": 1}, "val": 1, }, "val": 1, } ) ) == 5, f"{got=}" ``` # [Clear the Backspace Strings](https://leetle.app/?date=2025-02-12) Given a string `s` containing lowercase letters and `'#'` characters, where `'#'` represents a backspace, return the final string after all backspaces have been applied. If a backspace is applied to an empty string, the string remains empty. ## Solution ``` Python def solve(s): answer = [] for c in s: if c == "#": answer.pop() if answer else None else: answer.append(c) return "".join(answer) ``` ## Test Cases ``` Python assert (got := solve("ab#c")) == "ac", f"{got=}" assert (got := solve("ad#c")) == "ac", f"{got=}" assert (got := solve("a##c")) == "c", f"{got=}" assert (got := solve("a ``` # [Distinct Element Frequency Counter](https://leetle.app/?date=2025-02-13) Given an array of integers `nums`, return the frequency of the least common non-zero element. If all elements are zero or the array is empty, return 0. Use a hash map to count frequencies. ## Solution ``` Python from collections import Counter def solve(nums): nonzero = [i for i in nums if i] return Counter(nonzero).most_common()[-1][1] if nonzero else 0 ``` ## Test Cases ``` Python assert (got := solve([1, 2, 2, 3, 3, 3])) == 1, f"{got=}" assert (got := solve([5, 5, 5, 10, 10, 10, 1, 1])) == 2, f"{got=}" assert (got := solve([7, 7, 7, 7])) == 4, f"{got=}" assert (got := solve([0, 0, 0])) == 0, f"{got=}" assert (got := solve([4, 4, 2, 2, 1])) == 1, f"{got=}" assert (got := solve([])) == 0, f"{got=}" ``` # [Sum of Adjacent Character Codes](https://leetle.app/?date=2025-02-14) Given a string `s`, return a new string where each character is replaced by the sum of its ASCII value and the ASCII value of the character immediately following it. The last character should wrap around and use the first character for its sum calculation. The output should be a string where each position contains the character corresponding to the calculated sum (using modulo 128). ## Solution ``` Python def solve(s): w = s + s[0] if s else "" return "".join(chr((ord(w[i]) + ord(w[i + 1])) % 128) for i in range(len(s))) ``` ## Test Cases ``` Python assert (got := solve("abc")) == "CED", f"{got=}" assert (got := solve("Hello")) == "-QX[7", f"{got=}" assert (got := solve("Z")) == "4", f"{got=}" assert (got := solve("a")) == "B", f"{got=}" assert (got := solve("xyz")) == "qsr", f"{got=}" assert (got := solve("")) == "", f"{got=}" ``` # [Longest Substring With Two Distinct](https://leetle.app/?date=2025-02-15) Given a string `s`, find the length of the longest substring that contains at most two distinct characters. ## Solution ``` Python def solve(s): answer = left = 0 chars = {} for right, char in enumerate(s): chars[char] = chars.get(char, 0) + 1 while len(chars) > 2: chars[s[left]] -= 1 if chars[s[left]] == 0: del chars[s[left]] left += 1 answer = max(answer, right - left + 1) return answer ``` ## Test Cases ``` Python assert (got := solve("eceba")) == 3, f"{got=}" assert (got := solve("ccaabbb")) == 5, f"{got=}" assert (got := solve("abcabcabc")) == 2, f"{got=}" assert (got := solve("aaaaa")) == 5, f"{got=}" assert (got := solve("")) == 0, f"{got=}" assert (got := solve("abaccc")) == 4, f"{got=}" ``` # [Smallest Node Value in Tree](https://leetle.app/?date=2025-02-16) Given the root of a binary tree, find the smallest value among all the nodes in the tree. Assume the tree contains at least one node. ## Solution ``` Python def solve(root): answer = root["val"] stack = [root] while stack: node = stack.pop() answer = min(answer, node["val"]) left, right = node["left"], node["right"] if left: stack.append(left) if right: stack.append(right) return answer ``` ## Test Cases ``` Python assert ( got := solve( { "left": { "left": {"left": None, "right": None, "val": 1}, "right": {"left": None, "right": None, "val": 4}, "val": 3, }, "right": { "left": {"left": None, "right": None, "val": 7}, "right": {"left": None, "right": None, "val": 9}, "val": 8, }, "val": 5, } ) ) == 1, f"{got=}" assert (got := solve({"left": None, "right": None, "val": 100})) == 100, f"{got=}" assert ( got := solve( { "left": {"left": None, "right": None, "val": -10}, "right": {"left": None, "right": None, "val": 0}, "val": -5, } ) ) == -10, f"{got=}" assert ( got := solve( { "left": { "left": {"left": None, "right": None, "val": 30}, "right": None, "val": 20, }, "right": {"left": None, "right": None, "val": 5}, "val": 10, } ) ) == 5, f"{got=}" assert ( got := solve( { "left": { "left": {"left": None, "right": None, "val": 42}, "right": None, "val": 42, }, "right": {"left": None, "right": None, "val": 42}, "val": 42, } ) ) == 42, f"{got=}" assert ( got := solve( { "left": { "left": {"left": None, "right": None, "val": 3}, "right": {"left": None, "right": None, "val": 4}, "val": 2, }, "right": {"left": None, "right": None, "val": 0}, "val": 1, } ) ) == 0, f"{got=}" ``` # [Sum of Consecutive Stack Elements](https://leetle.app/?date=2025-02-17) Given an array of positive integers `nums`, calculate the sum of all elements that are immediately preceded by a larger element in the array. If an element has no preceding element (it's the first element), or if the preceding element is smaller or equal, it does not contribute to the sum based on this rule. The final result should be the total sum accumulated. ## Solution ``` Python def solve(nums): result = 0 for i in range(len(nums) - 1): result += nums[i + 1] if nums[i] > nums[i + 1] else 0 return result ``` ## Test Cases ``` Python assert (got := solve([5, 2, 8, 3, 9])) == 5, f"{got=}" assert (got := solve([10, 5, 1, 20, 15])) == 21, f"{got=}" assert (got := solve([1, 2, 3, 4, 5])) == 0, f"{got=}" assert (got := solve([5, 5, 5, 5])) == 0, f"{got=}" assert (got := solve([100, 10, 50, 5, 1])) == 16, f"{got=}" assert (got := solve([7])) == 0, f"{got=}" ``` # [Find Most Frequent Element Count](https://leetle.app/?date=2025-02-18) Given an array of integers `nums`, return the frequency (count) of the element that appears most often in the array. If there is a tie for the most frequent element, return the count of any of those elements. ## Solution ``` Python from collections import Counter def solve(nums): return Counter(nums).most_common(1)[0][1] ``` ## Test Cases ``` Python assert (got := solve([1, 3, 2, 1, 4, 1])) == 3, f"{got=}" assert (got := solve([5, 5, 2, 2, 9])) == 2, f"{got=}" assert (got := solve([10])) == 1, f"{got=}" assert (got := solve([7, 7, 7, 7, 7])) == 5, f"{got=}" assert (got := solve([-1, 0, -1, 5, 0, -1])) == 3, f"{got=}" assert (got := solve([100, 200, 100, 300, 200, 400])) == 2, f"{got=}" ``` # [Filter Odd Length Strings](https://leetle.app/?date=2025-02-19) Given a list of strings `words`, return a new list containing only the strings that have an odd number of characters. The order of the strings should be preserved. ## Solution ``` Python def solve(words): return [i for i in words if len(i) % 2] ``` ## Test Cases ``` Python assert (got := solve(["apple", "pie", "kiwi"])) == ["apple", "pie"], f"{got=}" assert (got := solve(["a", "bb", "ccc", "dddd"])) == ["a", "ccc"], f"{got=}" assert (got := solve(["even", "four"])) == [], f"{got=}" assert (got := solve(["hello", "world"])) == ["hello", "world"], f"{got=}" assert (got := solve([])) == [], f"{got=}" assert (got := solve(["", " ", " "])) == [" "], f"{got=}" ``` !!!BWL: THE BEER-WARE LICENSE (Revision 42) Carlo~~@~~Miron~~.IT~~ wrote this file. As long as you retain this notice you can do whatever you want with this stuff. If we meet some day, and you think this stuff is worth it, you can buy me a beer in return. —㎝