hi connections I just tackled LeetCode #151. The task is simple: take a string like " hello world " and return "world hello". The "Easy" way in many languages is to use built-in methods like split(), reverse(), and join(). This is often the best for production because it's readable and maintainable. My approach focused on robustness: Trimming: Removing unnecessary leading and trailing whitespace. Filtering: Ensuring that multiple spaces between words are reduced to a single space. Reversing: Flipping the order of the words while keeping the characters within the words intact. Complexity: Time: O(n) Space: O(n) In an interview, I always start with the most readable solution before discussing how we could optimize for space. #CleanCode #SoftwareEngineering #Python #Java #LeetCode #CodingInterviews
Reversing String with Leading/Trailing Whitespace Removal
More Relevant Posts
-
Just published a detailed solution breakdown on LeetCode I recently wrote an explanation for the “Prime Number of Set Bits” problem, covering: • Intuition and brute-force approach • Bit counting optimization • Bitmask compression trick (storing prime checks inside a single integer) • Multi-language implementations (C++, Java, Python, C#, C) One part I enjoyed exploring was replacing repeated prime checks with a compact bitmask: (1 << bitCount) & 665772 Small problem — but great practice in thinking about constraints and constant-time optimizations. Here’s the full explanation: https://lnkd.in/gAjX6MMt Always trying to get better at writing clean explanations, not just solving problems. #Algorithms #DataStructures #BitManipulation #LeetCode #ProblemSolving
To view or add a comment, sign in
-
-
🚀 Day 10/30 | LeetCode Problem: Reverse Linked List (206) Problem: Given the head of a singly linked list, reverse the list and return the new head. 💡 Approach (Iterative – Three Pointers) To reverse a linked list, we carefully change the direction of pointers. We use three variables: prev → previous node current → current node next_node → temporarily stores next node Steps: Store next node Reverse the current node’s pointer Move prev and current one step forward Repeat until the list ends Finally, prev becomes the new head. ⏱ Complexity Time Complexity: O(n) Space Complexity: O(1) 🧠 Python Code class Solution: def reverseList(self, head): prev = None current = head while current: nxt = current.next current.next = prev prev = current current = nxt return prev 📌 Example Input: [1,2,3,4,5] Output: [5,4,3,2,1] 🎯 Key Takeaway Linked List problems are all about pointer manipulation. Understanding pointer flow is more important than memorizing code. ✅ Accepted 🔖 Hashtags #LeetCode #30DaysOfLeetCode #Day10 #Python #LinkedList #DataStructures #Algorithms #ProblemSolving #CodingJourney #SoftwareEngineering
To view or add a comment, sign in
-
-
🚀 LeetCode Practice: Problem #145 – Binary Tree Postorder Traversal I recently solved LeetCode 145, a classic tree traversal problem that strengthens understanding of Depth-First Search (DFS) and recursion. 📌 Problem Statement Given the root of a binary tree, return the postorder traversal of its nodes’ values. Postorder Traversal Order: 👉 Left → Right → Root 🧠 Technique Used: Depth-First Search (Recursive Approach) Approach Explanation: If the current node is None, return. Recursively traverse the left subtree. Recursively traverse the right subtree. Visit (append) the root node value at the end. This traversal ensures that child nodes are processed before their parent node. Time Complexity: O(n) Space Complexity: O(h) (Where h is the height of the tree due to recursion stack) #LeetCode #Python #DSA #ValidAnagram #ProblemSolving #Algorithms #DataStructures #CodingPractice #100DaysOfCode #InterviewPreparation #SoftwareEngineering #DeveloperJourney #TechCommunity #LearningByDoing
To view or add a comment, sign in
-
-
hi connections I just tackled LeetCode #8 (String to Integer). On the surface, it’s just converting a string to a number. In reality, it’s a test of how you handle "dirty" data. The Challenge: You have to parse a string while ignoring whitespace, handling signs (+/-), stopping at non-digit characters, and—most importantly—managing integer overflow. My Strategy: Sanitize: Trim leading whitespace. Determine Sign: Check for '+' or '-' and store it. Convert: Iterate through digits and build the number. Guardrails: Check against 2^{31} - 1 and -2^{31} at every step to prevent overflow. This problem isn't about being "clever"—it’s about being thorough. In production, this is the difference between a working app and a crash. #SoftwareEngineering #CleanCode #LeetCode #BackendDevelopment #Java #Python
To view or add a comment, sign in
-
-
Day 47/100 – #100DaysOfLeetCode 🚀 🧩 Problem: LeetCode 191 - Number of 1 Bits (Easy) 🧠 Approach 1: Using Brian Kernighan’s Algorithm. 💻 Solution: class Solution: def hammingWeight(self, n: int) -> int: count = 0 while n: n &= (n - 1) count += 1 return count ⏱ Time | Space: O(k) | O(1) 📌 Key Takeaway: Using n & (n - 1) efficiently removes one set bit at a time, making it faster than checking every bit individually. 🧠 Approach 2: Python’s built-in binary conversion makes counting set bits simple 💻 Solution: class Solution: def hammingWeight(self, n: int) -> int: return bin(n).count('1') #leetcode #dsa #development #problemSolving #CodingChallenge
To view or add a comment, sign in
-
-
Searching & Sorting Algorithms power almost everything we build - from search features to ranked lists. I just published a complete, practical guide covering: • Linear & Binary Search • Bubble, Merge, Quick Sort • Big-O explained simply • Real-world use cases • Code in Python, JavaScript, Java, C++ & C If you’re preparing for interviews or strengthening your DSA foundation, this will help. 👉 Read here: https://lnkd.in/g2kJhgKc #DSA #Algorithms #Programming #SoftwareDevelopment #Python #JavaScript #Coding
To view or add a comment, sign in
-
-
Today I learned about if conditions — how programs make decisions. Just like in real life, code also decides what to do based on situations. If condition = logic begins. Logic begins = backend starts making sense. Slowly understanding how real applications think behind the scenes. 💡 Learning. Improving. Building. 🚀 #Python #PythonDeveloper #FullStackDevelopment #BackendDevelopment #CodingJourney #LearningInPublic #DeveloperLife
To view or add a comment, sign in
-
-
🚀 Day 7/30 | LeetCode Problem: First Unique Character in a String (387) Problem: Given a string s, find the first non-repeating character and return its index. If it doesn't exist, return -1. Approach: Use a dictionary to count occurrences of each character Traverse the string a second time to find the first character with count 1 Return its index Time Complexity: O(n) → Two passes over the string Space Complexity: O(1) → Dictionary holds at most 26 letters (for lowercase English letters) Python Code: class Solution: def firstUniqChar(self, s): a = {} for i in s: if i in a: a[i] += 1 else: a[i] = 1 for i in range(len(s)): if a[s[i]] == 1: return i return -1 Example: Input: "leetcode" Output: 0 → 'l' is the first unique character Input: "loveleetcode" Output: 2 → 'v' is the first unique character Takeaway: Counting occurrences is a common technique for string problems and can be applied to many scenarios like finding duplicates, frequency analysis, etc. 🔖 Hashtags #LeetCode #30DaysOfLeetCode #Day7 #Python #Strings #DSA #ProblemSolving #CodingJourney #SoftwareEngineering #TechCommunity
To view or add a comment, sign in
-
-
🚀 DSA Consistency – Day 2 Today I solved the Binary Watch problem on LeetCode. The idea was simple but interesting — iterate through all valid hours (0–11) and minutes (0–59), count the number of set bits in their binary representation, and collect the times where the total equals the given number of LEDs turned on. This problem reinforced: * Bit manipulation fundamentals * Clean iteration over constrained ranges * Proper formatting of edge cases (like leading zeros in minutes) Sometimes the goal isn’t solving something extremely complex — it’s about showing up daily and sharpening fundamentals. Consistency > Intensity. Here’s my solution (Python, Java & C++): 🔗 https://lnkd.in/dArp73Cd #DSA #LeetCode #Consistency #ProblemSolving #CodingJourney #SoftwareEngineering #BitManipulation
To view or add a comment, sign in
-
-
Day 35 of 365 days of code So far, so good Qn 1) Online stock span approach: decreasing monotonic stack Inside the constructor(__init__ block): initialize an empty stack we are going to use the stack to store the prices and the span Inside the function block(next): 1) set span=1 2) while the stack is not empty and the price at the top of the stack is less than the price: i) span+=stack.pop() 3) push the price and the span, as a tuple inside the stack. 4) return span. #365daysOfCode #NeetCode #leetcode #DSA #python #LeetCode #ProblemSolving #Algorithms #365dayschallenge
To view or add a comment, sign in
-
Explore related topics
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