LeetCode 217: Contains duplicate ⁇ Suppose you have a list of numbers, and you want to know if any number appears more than once. It’s like making sure everyone in class has a unique roll number! Approach 1: Set - Go through each number. - If it’s not in your set, add it. - If you ever see a number already in your set, you’ve got a duplicate! Approach 2: Hash Table (Counter) - Use a Counter to count how often each number appears in the list. - If any number appears more than once, return True (there’s a duplicate). Complexity: - Time Complexity: O(n) — One pass through all numbers. - Space Complexity: O(n) — Storing seen numbers or counts. Both approaches are fast and effective for catching duplicates quickly! Let’s check our numbers and make sure everyone’s unique! Check out the problem here: https://lnkd.in/g-JgRTEn Keep going, keep revising, and keep building confidence! 💪🔥 #DSA #Coding #ProblemSolving #Learning
More Relevant Posts
-
🎯 Day 83 of #100DaysOfCode 🔹 Problem: Count the Digits That Divide a Number – LeetCode ✨ Approach: A simple yet elegant digit-based problem! I extracted each digit of the number using modulo and division, then checked whether the digit cleanly divides the original number. Every valid digit increases the count — a perfect use-case for number breakdown and modular arithmetic. 🔢⚡ 📊 Complexity Analysis: ⏱ Time Complexity: O(d) — where d is the number of digits 💾 Space Complexity: O(1) — no extra data structures used ✅ Runtime: 0 ms (Beats 100% 🚀) ✅ Memory: 42.29 MB 🔑 Key Insight: Even basic problems sharpen precision — breaking numbers digit-by-digit reinforces strong logical thinking and clean coding habits. Sometimes the simplest loops teach the most. ✨ #LeetCode #100DaysOfCode #DSA #NumberTheory #ModularArithmetic #CleanCode #EfficientCoding #LogicBuilding #TechJourney #ProgrammingChallenge #CodingDaily
To view or add a comment, sign in
-
-
LeetCode 136: Single number 🔂 Approach 1: Hash Map - We use a dictionary to count occurrences of each number. - Go through the list and update counts. - Finally, look for the number with a count of 1—that’s our answer! Complexity: - Time Complexity: O(n) — Check all items. - Space Complexity: O(n) — Dictionary to track counts. Approach 2: XOR (Bit Manipulation) - Initialize a variable ans = 0. - XOR every number in the list with ans. - Pairs cancel out (because a ^ a = 0 and 0 ^ b = b), so only the single number remains at the end. Complexity: - Time Complexity: O(n) — Just one pass through all numbers. - Space Complexity: O(1) — No extras, just ans. The XOR trick is super efficient and works like magic due to properties of binary operations! Check out the problem here: https://lnkd.in/giUbz53v Keep going, keep revising, and keep building confidence! 💪🔥 #DSA #Coding #ProblemSolving #Learning
To view or add a comment, sign in
-
Don’t dive into LeetCode before you know this. It can save 40% of your time… 👇 I wasted weeks because I didn’t know this. What changed? 𝗜 𝗹𝗲𝗮𝗿𝗻𝗲𝗱 𝗖++ 𝗦𝗧𝗟 (𝗦𝘁𝗮𝗻𝗱𝗮𝗿𝗱 𝗧𝗲𝗺𝗽𝗹𝗮𝘁𝗲 𝗟𝗶𝗯𝗿𝗮𝗿𝘆). 🧠 Why it matters: STL saves you from writing complex data structures from scratch. Everything — from sorting to searching — is already optimized and tested. 𝗜𝘁 𝗵𝗮𝘀 𝟰 𝗺𝗮𝗶𝗻 𝗽𝗮𝗿𝘁𝘀: ➝ Containers (the data structures) ➝ Algorithms (the actions, like sort()) ➝ Iterators (the “pointers” that move through them) ➝ Functions (we’ll skip this for now) ⚡ 𝗟𝗲𝘁’𝘀 𝗱𝗶𝘃𝗲 𝗶𝗻𝘁𝗼 𝗖𝗼𝗻𝘁𝗮𝗶𝗻𝗲𝗿𝘀 — 𝗠𝗼𝘀𝘁 𝗨𝘀𝗲𝗱 (𝗶𝗻 𝗼𝗿𝗱𝗲𝗿): ➝ vector – dynamic array, most common → vector<int> v = {1,2,3}; ➝ unordered_map – fastest key-value lookups → unordered_map<string,int> mp; ➝ map – sorted key-value pairs ➝ set – unique sorted elements ➝ unordered_set – unique, unsorted, faster ➝ deque – insert/remove from both ends ➝ list – doubly linked list ➝ stack / queue / priority_queue – specific use (LIFO/FIFO) ➝ array – fixed size, static 💡 𝗤𝘂𝗶𝗰𝗸 𝗧𝗶𝗽𝘀: ➝ Use vector by default unless you have a reason not to. ➝ Use unordered_map when you need speed, map when you need order. ➝ set and unordered_set are perfect for unique elements. 📌 Save this post — you’ll need it every time you start a DSA problem 🚀 #cplusplus #stl #dsa #programming #codingjourney #developers #techstudents #leetcode #students #engineeringlife #competitiveprogramming
To view or add a comment, sign in
-
-
🚀 How I Solved LeetCode’s Word Search Problem Without DFS or Recursion When I came across Word Search (LeetCode 79), I noticed that almost every solution used DFS and backtracking. But I wanted to solve it my own way — using index tracking and adjacency logic instead of recursion. My intuition was simple: If a word exists in the grid, every next letter must lie right next to the previous one — either up, down, left, or right. That means their coordinate difference should satisfy: abs(x1 - x2) + abs(y1 - y2) == 1. This small observation became the foundation of my entire approach. 💡 My Thought Process 1️⃣ I mapped every letter in the grid to its coordinates. 2️⃣ I started from all positions of the first letter in the word. 3️⃣ For every next letter, I checked which positions are adjacent and not already used. 4️⃣ I extended paths step by step — no recursion, only iteration. 5️⃣ If any path reached the last letter → the word exists. ⚙️ My Implementation I used a BFS-style iterative search, where each state holds: the current cell (x, y) the current index in the word and a bitmask representing already-used cells. This replaced recursion and heavy visited[][][] arrays with a compact integer mask. Each move just extended to adjacent cells if the next character matched. It handled tricky cases like "ABCB" perfectly → returned false, which even some DFS implementations fail on. ✅ Why It Worked ✔️ No recursion = no stack overflow. ✔️ Bitmask = minimal memory use. ✔️ Adjacency rule = exactly matches grid movement. ✔️ Iterative BFS = simple, efficient, and logical. 🎯 What I Learned You don’t always need to follow the standard algorithms. Sometimes, just trusting your intuition and building around your idea leads to something even better. This problem taught me that creativity and logic go hand in hand. And when you believe in your approach — it truly works. 💪 #Coding #LeetCode #ProblemSolving #Cplusplus #Innovation #DSA #Learning #DeveloperJourney
To view or add a comment, sign in
-
-
🌟 Day 93 of My LeetCode Journey — Problem 686: Repeated String Match 💡 Problem Insight: Today’s problem explored how many times string A must be repeated so that string B becomes a substring of it. It’s a fun mix of string repetition and pattern matching — testing both logic and edge-case awareness. 🧠 Concept Highlight: The core idea is to keep repeating A until it’s at least as long as B, then check if B exists within it (or one more repetition). This problem reinforces concepts of string concatenation, searching, and boundary conditions in text processing. 💪 Key Takeaway: Sometimes, solutions aren’t about complexity but about knowing when to stop repeating — in both coding and persistence! ✨ Daily Reflection: Simple problems like these highlight the beauty of algorithmic reasoning — transforming trial-and-error into structured logic. #Day93 #LeetCode #100DaysOfCode #StringMatching #ProblemSolving #DSA #CodingJourney #LearnByDoing #SoftwareEngineering #Persistence
To view or add a comment, sign in
-
-
💻 Day 3 of My #120DaysOfLeetCode Challenge 🚀 🧩 Problem: Longest Substring Without Repeating Characters Difficulty: Medium | Language Used: C 🔍 What’s the Problem About? The task is to find the length of the longest substring in a given string that contains no repeating characters. For example: Input: "abcabcbb" → Output: 3 (Longest substring: "abc") Input: "pwwkew" → Output: 3 (Longest substring: "wke") Input: "bbbbb" → Output: 1 (Longest substring: "b") This problem is a classic example of how we can optimize brute force logic using sliding window and two-pointer techniques. ⚙️ Approach I Used – Sliding Window Technique Instead of checking every possible substring (which would be very slow), I used a sliding window approach with two pointers (left and right) and a frequency array to track visited characters. Here’s the step-by-step thought process: Start both pointers at the beginning of the string. Move the right pointer one step at a time, adding characters to the window. If a character repeats, move the left pointer ahead until all characters in the window are unique again. Keep track of the maximum window size during the process. This ensures each character is processed only once → giving an efficient O(n) solution. 💡 Key Takeaways Learned how two-pointer and sliding window techniques can simplify substring problems. Understood how to use frequency arrays to track duplicates efficiently. Strengthened logical reasoning and window-based problem-solving skills in C. Every new problem is an opportunity to refine logic, enhance efficiency, and get one step closer to mastering Data Structures and Algorithms. Day 3 completed ✅ | 117 more to go 💪 #LeetCode #CProgramming #DSA #ProblemSolving #CodingChallenge #100DaysOfCode #LearningJourney #SlidingWindow
To view or add a comment, sign in
-
-
🔹 DSA Daily | Check for Balanced Parentheses (C++) Balanced parentheses problems are a classic way to test your understanding of **stacks** and **logical thinking**. Today, I solved one such problem — checking if a string of parentheses, braces, and brackets is balanced. 💡 Problem Statement: Given a string containing '(', ')', '{', '}', '[' and ']', determine if the string is **balanced**. A string is balanced if every opening bracket has a corresponding closing bracket in the correct order. Approach: I used a **stack** to keep track of opening brackets. - Push every opening bracket onto the stack. - For closing brackets, check if the top of the stack has the matching opening bracket. - If it does, pop it; otherwise, the string is unbalanced. - At the end, if the stack is empty, the string is balanced. Time Complexity: O(n) — traversing the string once Space Complexity: O(n) — for the stack This problem highlights how **stacks make bracket-matching elegant and efficient**. 🚀 #DSA #CPlusPlus #Coding #ProblemSolving #Stack #BalancedParentheses #CodeEveryday #GeeksforGeeks #LeetCode #ProgrammingJourney #DataStructures #Algorithms #InterviewPrep #CodingCommunity #LogicBuilding #EfficientCode #LearnToCode #TechJourney
To view or add a comment, sign in
-
-
🚀 Day 23/30 – 30DaysOfDSAChallenge 🚀 🧠 Topic: Subsets II (Handling Duplicates) 📘 Concepts Covered: Recursion | Backtracking | Duplicate Handling Today I solved the Subsets II problem — a tricky variant of the power set problem where the array can contain duplicate elements. The challenge was to ensure that no duplicate subsets appear in the final output. 🔹 Approach – Recursive Backtracking with Skip Logic 💡 Logic: Sort the array first to group duplicate elements together. While generating subsets recursively, skip consecutive duplicates using a simple condition. This ensures unique subsets without extra space for duplicate checks. ⚙️ Time Complexity: O(2ⁿ) ⚙️ Space Complexity: O(2ⁿ * n) ✅ Result: Accepted ✅ | Runtime: 0 ms 🟢 | Beats: 100% 🚀 ✨ Key Takeaway: Understanding how to prune recursion trees helps eliminate redundancy and boosts performance — a crucial concept in advanced DSA problems. #Day23 #30DaysOfDSAChallenge #LeetCode #Backtracking #Recursion #SubsetsII #ProblemSolving #CPlusPlus #CodingChallenge #DSA #CodeEveryday #DeveloperJourney #Algorithms
To view or add a comment, sign in
-
-
LeetCode 1512: Number of good pairs 1️⃣1️⃣ The problem: For a list of numbers, count how many pairs of indices (i,j) have the same value, with i<j. In other words, how many pairs are "identical"? How does the code work? - First, we use a Counter to count how many times each number appears. - For any number that appears more than once, the number of unique pairs is given by the combination formula: ( pairs=n×(n−1)/2 ), where n is how often the number appears. - Add up pairs for each number and return the total. Complexity: - Time Complexity: O(n) — One scan to count, then a quick calculation for each different value. - Space Complexity: O(n) — Storing counts of each number. Check out the problem here: https://lnkd.in/gYsrZBxu Keep going, keep revising, and keep building confidence! 💪🔥 #DSA #Coding #ProblemSolving #Learning
To view or add a comment, sign in
-
LeetCode 219: Contains duplicate II ⁇ Suppose you have a list of numbers, and you want to check if any number appears at least twice close together—within k positions apart. How does the code work? - We use a dictionary to track the last index where each number appeared. - As we go through the list: - If the current number has been seen before, - Check the gap between indices. - If the gap (i−j)is less than or equal to k, we found a nearby duplicate! - Otherwise, update the index to the current position. - If the number is new, just store this index. - If we finish the list with no matches, return False. Complexity: - Time Complexity: O(n) — Scan through all numbers in a single pass. - Space Complexity: O(n) — Need to remember each distinct number’s last position. Check out the problem here: https://lnkd.in/gUtKpsHf Keep going, keep revising, and keep building confidence! 💪🔥 #DSA #Coding #ProblemSolving #Learning
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