Headline: 🧠 Thinking Mathematically: Finding the GCD of Strings The Content: Continuing my JavaScript challenge on LeetCode! Today I tackled the "Greatest Common Divisor of Strings" problem. This one is fascinating because it blends string logic with classic number theory. The Challenge: Given two strings, find the largest string x such that x divides both str1 and str2. My Approach: The Concatenation Test: A brilliant trick for this problem is checking if str1 + str2 === str2 + str1. If they don't match, a common divisor string literally cannot exist. The GCD Logic: If they pass the test, the length of the greatest common divisor string must be the GCD of the lengths of the two strings. The Solution: I implemented a recursive helper function using the Euclidean Algorithm to find the gcdLength, then simply sliced the prefix! Technical Efficiency: Time Complexity: $O(n + m)$ due to string concatenation and comparison. Space Complexity: $O(n + m)$ to store the concatenated strings. Key Reflection: It’s amazing how concepts from basic math (like GCD) find their way into modern software engineering problems. It’s all about finding the pattern before writing the first line of code. Onward to the next challenge! 📈 #LeetCode #JavaScript #CodingChallenge #ProblemSolving #SoftwareEngineering #Algorithms #MathInCode #WebDevelopment
Finding GCD of Strings with JavaScript LeetCode Challenge
More Relevant Posts
-
JavaScript’s sort() can silently break your algorithms. I was solving LeetCode #350 (Intersection of Two Arrays II) using the two-pointer approach and hit an unexpected issue. I sorted the arrays like this: nums.sort() It looked fine… until it wasn’t. Example: [1, 2, 10, 5].sort() // → [1, 10, 2, 5] Why? Because JavaScript sorts lexicographically (as strings) by default. So the engine actually compares: "1", "10", "2", "5" Correct numeric sorting requires a comparator: nums.sort((a, b) => a - b) Without this, algorithms that rely on sorted arrays (binary search, two-pointer techniques, etc.) can produce incorrect results. #JavaScript #WebDevelopment #Programming #CodingTips #LeetCode #Algorithms #SoftwareEngineering #FrontendDevelopment
To view or add a comment, sign in
-
-
Day 20: Property Descriptors & The "Sugar" of Classes 🏗️💎 Object-oriented JavaScript isn't just about syntax; it's about control. Today was the final deep-dive into the "Meta" layer of JS objects—understanding why Math.PI is immutable and how to build bulletproof class structures. The "Crack-It Kit" Checklist: Day 20 📑 🔹 Class Hierarchy: Mastering extends and the super() bridge. Understanding why the parent must initialize before the child can exist. 🏛️ 🔹 Static Utilities: Learning to define methods that belong to the "Blueprint" (Class) rather than the "House" (Instance). ⚙️ 🔹 The .bind() Marriage: Locking context permanently. Moving beyond immediate execution to creating reusable, context-safe functions. 🔗 🔹 Under the Hood (Descriptors): Breaking down writable, enumerable, and configurable. Solving the mystery of why built-in JS properties are unchangeable. 💎 🔹 Property Shielding: Using Object.defineProperty to hide data from loops and prevent unauthorized overwrites. 🛡️ 🔹 Object Iteration: Mastering Object.entries() and Factory Functions for cleaner, more modern data handling. 🧪 The foundation is complete. We’ve moved from basic literals to professional meta-programming. 🏗️ #JavaScript #WebDevelopment #CrackItKit #OOP #WebEngineering #CodingJourney #SoftwareArchitecture #TechInterviews #120DayChallenge
To view or add a comment, sign in
-
-
Day 21: The Final Logic – Closures & The Magic of Property Access 🔒✨ Today marks the grand finale of the JavaScript deep-dive. We didn't just look at the code; we looked at the Memory and the Engine logic that governs how variables live and die. 🧠 The "Crack-It Kit" Checklist: Day 21 📑 🔹 The "Stack Overflow" Trap: Understanding why a setter that calls itself triggers a recursion loop, and the "Underscore Logic" (_variable) used to fix it. 🛡️ 🔹 Getters & Setters: Moving beyond simple data storage to "Smart Properties." Whether using Class syntax or Object.defineProperty, it's about intercepting and validating every piece of data. ⚙️ 🔹 The .length Mystery: Breaking down how arr.length actually works. It’s not just a counter—it’s an exotic property managed by the engine with a setter that can physically truncate memory. 🧪 🔹 Lexical Scoping: Mastering the "Hierarchy of Access." Understanding that where you write your code determines what your functions can see. 🏛️ 🔹 Closures (The Memory Lock): The ultimate interview topic. Learning how JS "locks" parent variables in memory to keep them alive for inner functions, even after the parent has finished executing. 🔒 The JavaScript foundation is now 100% complete. From the TCP 3-way handshake to the internal mechanics of Closures, the logic is locked in. 🏗️ #JavaScript #WebDevelopment #CrackItKit #Closures #WebEngineering #CodingJourney #SoftwareArchitecture #TechInterviews #MERNStack #WanderlustProject
To view or add a comment, sign in
-
-
🚀 LeetCode Problem Solved – Sort Characters by Frequency Today I solved the “Sort Characters By Frequency” problem on LeetCode. 🔎 Problem Statement: Given a string, sort its characters in decreasing order based on their frequency. 🧠 Key Learning: Instead of directly sorting the string (which isn’t possible since strings are immutable in JavaScript), I: ✔ Counted the frequency of each character using an object ✔ Extracted keys using Object.keys() ✔ Sorted them based on frequency (descending order) ✔ Rebuilt the final string using nested loops 💡 Concepts Used: HashMap / Object for frequency counting Custom sorting with comparator function String building using loops Time Complexity optimization thinking ✨ Example: Input: "tree" Output: "eert" This problem strengthened my understanding of: Frequency-based sorting Custom comparator logic Clean DSA implementation in JavaScript Consistency in solving DSA problems daily 🚀 #LeetCode #DSA #JavaScript #ProblemSolving #CodingJourney #FrontendDeveloper #100DaysOfCode
To view or add a comment, sign in
-
-
🚀 𝗡𝗲𝘄 𝗣𝗥 𝗠𝗲𝗿𝗴𝗲𝗱 I recently tackled the challenge of reversing an array in-place. The core 🧩 𝗣𝗿𝗼𝗯𝗹𝗲𝗺 was to modify the original array directly without creating a new one, which requires careful index management. To solve this, I implemented the two-pointer technique in JavaScript. I used one pointer starting at the beginning of the array and another at the end. I then swapped the elements pointed to by these pointers and moved them towards the center until they met or crossed. This ⚙️ 𝗔𝗽𝗽𝗿𝗼𝗮𝗰𝗵 ensures O(1) space complexity. My 🐞 𝗗𝗲𝗯𝘂𝗴𝗴𝗶𝗻𝗴 𝗣𝗿𝗼𝗰𝗲𝘀𝘀 involved dry runs to trace the pointer movements and element swaps. Visualizing the array's state at each step was crucial. I also occasionally used the debugger to step through the code and confirm my understanding of the logic. A 📚 𝗞𝗲𝘆 𝗟𝗲𝗮𝗿𝗻𝗶𝗻𝗴 from this exercise was the power of in-place algorithms for optimizing memory usage, especially when dealing with large datasets. You can see the implementation in my latest PR: https://lnkd.in/d5rAw_CN What are your favorite in-place algorithms or techniques? 📦 Repo: https://lnkd.in/d5rAw_CN #DataStructures #Algorithms #JavaScript #TwoPointerTechnique #ProblemSolving #InPlaceAlgorithm #CodingChallenge #SoftwareDevelopment #ArrayManipulation
To view or add a comment, sign in
-
-
🚀 Cracked a tricky DSA concept today — Search in Rotated Sorted Array (with duplicates) At first glance the array looks unsorted… But actually it’s two sorted arrays joined together 🔍 Example: [2,5,6,0,0,1,2] The challenge ❓ Find a target efficiently (not O(n)) → Use Modified Binary Search (O(log n)) 💡 Key Learning: 1️⃣ One half of the array is always sorted 2️⃣ Check if target lies in the sorted half 3️⃣ If duplicates confuse the search → shrink the window (left++, right--) Core logic: • All equal → ignore edges • Left sorted → search there • Else → search right side 📈 What I learned: Understanding the logic is more important than memorizing code. Today I didn’t just solve a problem — I understood how to think like an algorithm. #DSA #BinarySearch #JavaScript #CodingJourney #100DaysOfCode #ProblemSolving
To view or add a comment, sign in
-
-
Data Structures & String Magic Today was all about how we store and manipulate data in JavaScript. I moved beyond simple variables and explored the power of Strings and Objects. It’s fascinating to see how similar Strings and Arrays can be, yet how unique they are in their behavior! My Learning Milestones Today: String Mastery: Explored slice, join, concat, and the importance of case normalization (toLowerCase/toUpperCase) for comparisons. The "Reverse" Challenge: Learned three different ways to reverse a string—a classic interview favorite! Object Essentials: Introduction to Properties and Values. I practiced multiple ways to "get" and "set" data using dot notation and bracket notation. Advanced Object Ops: Working with nested objects, using Object.keys() and Object.values(), and learning how to safely delete properties. Iteration: Mastering how to loop through objects to pull out the data I need. Objects are truly the backbone of JavaScript, and I'm feeling much more confident in how to structure my code. 🚀 #JavaScript #WebDevelopment #CodingJourney #StringsAndObjects #FrontendDev #TechGrowth
To view or add a comment, sign in
-
-
Today I practiced the "Middle of the Linked List" problem while working on Data Structures in JavaScript. The challenge was to return the middle node of a singly linked list. If the list has two middle nodes, the second one should be returned. Instead of counting the length of the list first, I implemented the Fast and Slow Pointer technique: Slow pointer moves one step at a time Fast pointer moves two steps at a time When the fast pointer reaches the end, the slow pointer naturally lands at the middle of the list. This approach solves the problem in O(n) time and O(1) space, which is much more efficient than calculating the list length separately. Working through these problems is helping me strengthen my understanding of pointers, traversal patterns, and algorithmic thinking in JavaScript. #javascript #dsa #datastructures #algorithms #leetcode #programming #softwareengineering
To view or add a comment, sign in
-
-
✅ Solved LeetCode: Same Tree (100) — Recursive DFS Approach Implemented a clean recursive solution in JavaScript using a structural comparison strategy. Approach: - If both nodes p and q are null, return true. - If only one of them is null, return false. - Otherwise: - Check if p.val === q.val. - Recursively compare: - p.left with q.left - p.right with q.right - Return the combined result of value equality and both subtree comparisons. This approach ensures both structure and node values are identical at every level of the tree. Time Complexity: O(n) — each node is visited once Space Complexity: O(h) — recursion stack space (where h is tree height) A straightforward and elegant recursive pattern for tree comparison problems!
To view or add a comment, sign in
-
-
✅ Solved LeetCode: Invert Binary Tree (226) — Recursive DFS Approach Implemented a clean recursive solution in JavaScript using a simple swap-and-recurse strategy. Approach: - If the node is null, return it immediately. - Swap the left and right children: - Store root.left in a temporary variable. - Assign root.right to root.left. - Assign the temporary value to root.right. - Recursively call the function on: - root.left - root.right - Return the root after inversion. This approach flips the tree at every node and naturally propagates the inversion down to all subtrees using recursion. Time Complexity: O(n) — each node is visited once Space Complexity: O(h) — recursion stack space (where h is tree height) A classic tree problem solved with a simple and elegant recursive swap!
To view or add a comment, sign in
-
Explore content categories
- Career
- Productivity
- Finance
- Soft Skills & Emotional Intelligence
- Project Management
- Education
- Technology
- Leadership
- Ecommerce
- User Experience
- Recruitment & HR
- Customer Experience
- Real Estate
- Marketing
- Sales
- Retail & Merchandising
- Science
- Supply Chain Management
- Future Of Work
- Consulting
- Writing
- Economics
- Artificial Intelligence
- Employee Experience
- Workplace Trends
- Fundraising
- Networking
- Corporate Social Responsibility
- Negotiation
- Communication
- Engineering
- Hospitality & Tourism
- Business Strategy
- Change Management
- Organizational Culture
- Design
- Innovation
- Event Planning
- Training & Development