From 0f5ed52469a16466d2c5f780ab4c7a886011c444 Mon Sep 17 00:00:00 2001 From: "codeflash-ai[bot]" <148906541+codeflash-ai[bot]@users.noreply.github.com> Date: Mon, 22 Dec 2025 08:24:28 +0000 Subject: [PATCH] Optimize find_common_tags **Key optimizations:** - Pre-extract all tag lists upfront (avoids repeated `.get("tags", [])` lookups in the critical loop). - Early exit if any tag list is empty (intersection must be empty if any is empty). - Sort tag lists by size before intersection to reduce intermediate set size as quickly as possible (much faster for many articles, especially when tag set sizes vary). - All comments and code behavior are preserved. The return values, exceptions, and input/output remain exactly as in the original code. --- src/algorithms/string.py | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/src/algorithms/string.py b/src/algorithms/string.py index 658439e..6074b42 100644 --- a/src/algorithms/string.py +++ b/src/algorithms/string.py @@ -41,9 +41,18 @@ def find_common_tags(articles: list[dict[str, list[str]]]) -> set[str]: if not articles: return set() - common_tags = set(articles[0].get("tags", [])) - for article in articles[1:]: - common_tags.intersection_update(article.get("tags", [])) + # Pre-extract all tag lists for faster access and avoid repeated .get lookups + tag_lists = [article.get("tags", []) for article in articles] + + # If any tag list is empty, the intersection will be empty + if any(len(tags) == 0 for tags in tag_lists): + return set() + + # Sort tag_lists by length to start intersection from smallest, reducing work early + tag_lists.sort(key=len) + common_tags = set(tag_lists[0]) + for tags in tag_lists[1:]: + common_tags.intersection_update(tags) if not common_tags: break return common_tags