Course Hive
Search

Welcome

Sign in or create your account

Continue with Google
or
1071 Greatest Common Divisor of Strings (C++) | String GCD Trick | LeetCode 75 #02
Play lesson

LeetCode 75 C++ | Step-by-Step Solutions (LAN Academy) - 1071 Greatest Common Divisor of Strings (C++) | String GCD Trick | LeetCode 75 #02

4.0 (1)
12 learners

What you'll learn

This course includes

  • 5.5 hours of video
  • Certificate of completion
  • Access on mobile and TV

Summary

Keywords

Full Transcript

LeetCode 75 C++ playlist: https://www.youtube.com/playlist?list=PLF0G-7pZcOza_jyAxwMpa5KnK3vST5smK Comment β€œNEXT” for the next problem link. Most people overthink this problem. The fastest way to solve it is a simple β€œstring GCD” test that instantly tells you whether a common base pattern exists β€” then one gcd on lengths gives the answer. In this video, I solve **LeetCode 1071: Greatest Common Divisor of Strings** in **C++** with a clean, interview-ready approach: - First, we verify both strings are built from the same repeating pattern using one powerful check: (str1 + str2) == (str2 + str1) - If true, the length of the greatest divisor string is: gcd(str1.length(), str2.length()) - The answer is the prefix: str1.substr(0, gcdLen) Why this works (quick intuition): If two strings come from the same repeating base, swapping their concatenation order doesn’t change the result. If the concatenations differ, there is no string that can divide both β€” return "". What you’ll learn (and what interviewers want to see): - How to detect a shared repeating pattern in O(n) - How numeric gcd translates directly to strings - A minimal, robust C++ solution (no brute force, no extra loops) - Edge cases that break naive solutions (e.g., "LEET" vs "CODE", "AAAAAB" vs "AAA") Examples: "ABCABC" + "ABC" β†’ "ABC" "ABABAB" + "ABAB" β†’ "AB" "LEET" + "CODE" β†’ "" "AAAAAB" + "AAA" β†’ "" Problem link: https://leetcode.com/problems/greatest-common-divisor-of-strings/ More clear C++ LeetCode explanations (LeetCode 75): https://lanacademy-school.thinkific.com/courses/leetcode75 Did you first think β€œbrute force all divisors” or did you see the gcd pattern immediately? Comment your first instinct. Follow LAN Academy: Website: https://lanacademy-school.thinkific.com YouTube: @lanacademy9521 X (Twitter): https://x.com/SolutionsLan #LeetCode #cpp #BitManipulation#leetcode #leetcodecoding #leetcodesolution #leetcodechallenge #faang #fang #computerscience #cs #codingtiktok #coding #codinglife #boostofhope #fyp #codinginterview #softwareengineer #swe #tech #foryou #python #learntocode #google #techtok #programming #algorithms #learntocode #1071 #bac #blink #blind @reper #foryou #viral #reper #urmaritori #for

Course Hive

Continue this lesson in the app

Install CourseHive on Android or iOS to keep learning while you move.

Related Courses

FAQs

Course Hive
Download CourseHive
Keep learning anywhere