🔹 Day 32 – LeetCode Practice Problem: Reverse Words in a String (LeetCode #151) 📌 Problem Statement: Given an input string s, reverse the order of the words. A word is defined as a sequence of non-space characters separated by at least one space. Return the words in reverse order, joined by a single space — no extra spaces allowed. ✅ My Approach (Java): Used split() to separate words by spaces. Ignored extra spaces using \\s+ in regex. Reversed the array of words and joined them with a single space. Ensured no leading or trailing spaces appear in the final output. 📊 Complexity: Time Complexity: O(n) Space Complexity: O(n) ⚡ Submission Results: Accepted ✅ Runtime: 12 ms (Beats 13.87%) Memory: 45.26 MB (Beats 16.33%) 💡 Reflection: This problem helped me refine my understanding of string manipulation and space handling in Java. Even though performance can be optimized, the logic was clean and easy to follow. #LeetCode #ProblemSolving #Java #DSA
Reversing Words in a String with Java on LeetCode
More Relevant Posts
-
✅ Day 35 of #100DaysOfCode Challenge 📘 LeetCode Problem 112: Path Sum 🧩 Problem Statement: Given the root of a binary tree and an integer targetSum, return true if there exists a root-to-leaf path whose sum of node values equals targetSum. Example: Input: root = [5,4,8,11,null,13,4,7,2,null,null,null,1], targetSum = 22 Output: true Explanation: Path 5 → 4 → 11 → 2 gives the sum = 22 ✅ 💡 Simple Approach: If the tree is empty → return false If it’s a leaf node → check if its value equals targetSum Otherwise → subtract node value and check left and right recursively 💻 Easiest Java Code: class Solution { public boolean hasPathSum(TreeNode root, int sum) { if (root == null) return false; if (root.left == null && root.right == null && root.val == sum) return true; sum = sum - root.val; return hasPathSum(root.left, sum) || hasPathSum(root.right, sum); } } ⚙ Complexity: ⏱ Time: O(n) → visit each node once 💾 Space: O(h) → recursion stack (h = height of tree) 🌿 Small code, big concept — recursion makes trees easy 🌱 #Day35 #100DaysOfCode #LeetCode #Java #DSA #BinaryTree #Recursion #CodingChallenge
To view or add a comment, sign in
-
-
✅ Day 35 of #100DaysOfCode Challenge 📘 LeetCode Problem 112: Path Sum 🧩 Problem Statement: Given the root of a binary tree and an integer targetSum, return true if there exists a root-to-leaf path whose sum of node values equals targetSum. Example: Input: root = [5,4,8,11,null,13,4,7,2,null,null,null,1], targetSum = 22 Output: true Explanation: Path 5 → 4 → 11 → 2 gives the sum = 22 ✅ 💡 Simple Approach: If the tree is empty → return false If it’s a leaf node → check if its value equals targetSum Otherwise → subtract node value and check left and right recursively 💻 Easiest Java Code: class Solution { public boolean hasPathSum(TreeNode root, int sum) { if (root == null) return false; if (root.left == null && root.right == null && root.val == sum) return true; sum = sum - root.val; return hasPathSum(root.left, sum) || hasPathSum(root.right, sum); } } ⚙️ Complexity: ⏱️ Time: O(n) → visit each node once 💾 Space: O(h) → recursion stack (h = height of tree) 🌿 Small code, big concept — recursion makes trees easy 🌱 #Day35 #100DaysOfCode #LeetCode #Java #DSA #BinaryTree #Recursion #CodingChallenge
To view or add a comment, sign in
-
-
✅ Day 35 of #100DaysOfCode Challenge 📘 LeetCode Problem 112: Path Sum 🧩 Problem Statement: Given the root of a binary tree and an integer targetSum, return true if there exists a root-to-leaf path whose sum of node values equals targetSum. Example: Input: root = [5,4,8,11,null,13,4,7,2,null,null,null,1], targetSum = 22 Output: true Explanation: Path 5 → 4 → 11 → 2 gives the sum = 22 ✅ 💡 Simple Approach: If the tree is empty → return false If it’s a leaf node → check if its value equals targetSum Otherwise → subtract node value and check left and right recursively 💻 Easiest Java Code: class Solution { public boolean hasPathSum(TreeNode root, int sum) { if (root == null) return false; if (root.left == null && root.right == null && root.val == sum) return true; sum = sum - root.val; return hasPathSum(root.left, sum) || hasPathSum(root.right, sum); } } ⚙️ Complexity: ⏱️ Time: O(n) → visit each node once 💾 Space: O(h) → recursion stack (h = height of tree) 🌿 Small code, big concept — recursion makes trees easy 🌱 #Day35 #100DaysOfCode #LeetCode #Java #DSA #BinaryTree #Recursion #CodingChallenge
To view or add a comment, sign in
-
-
𝗕𝗲𝗵𝗶𝗻𝗱 𝘁𝗵𝗲 𝗦𝗰𝗲𝗻𝗲𝘀 𝗼𝗳 𝗝𝗮𝘃𝗮 𝗔𝗿𝗿𝗮𝘆𝗟𝗶𝘀𝘁! Today, I implemented a 𝗗𝘆𝗻𝗮𝗺𝗶𝗰 𝗔𝗿𝗿𝗮𝘆 in Java — building the logic from scratch to understand how ArrayList actually works under the hood. ⚙️ This exercise gave me hands-on exposure to how resizing, shifting, and accessing elements are efficiently handled in dynamic data structures. 𝗛𝗲𝗿𝗲’𝘀 𝘄𝗵𝗮𝘁 𝗜 𝗶𝗺𝗽𝗹𝗲𝗺𝗲𝗻𝘁𝗲𝗱 👇 🔹 add(element) — to append new items dynamically 🔹 add(index, element) — to insert at a specific position 🔹 get(index) — to fetch values efficiently 🔹 set(index, element) — to replace existing elements 🔹 remove(index/element) — to delete items and shift elements 🔹 contains(element) — to check for existence 🔹 isEmpty() & clear() — to manage and reset the structure 𝗜𝗺𝗽𝗹𝗲𝗺𝗲𝗻𝘁𝗮𝘁𝗶𝗼𝗻 𝗖𝗼𝗱𝗲 : https://lnkd.in/gXEfwXvf 📘 𝗞𝗲𝘆 𝗧𝗮𝗸𝗲𝗮𝘄𝗮𝘆: Recreating ArrayList from scratch deepened my understanding of how memory reallocation, indexing, and dynamic resizing make it one of the most powerful data structures in Java’s Collection Framework. Learning the “how” behind the “what” truly builds stronger foundations. 💪 #Java #DataStructures #ArrayList #CodingJourney #LearningByDoing #SoftwareEngineering #DSA #100DaysOfCode
To view or add a comment, sign in
-
-
✅ Day 33 of #100DaysOfCode Challenge 📘 LeetCode Problem 110: Balanced Binary Tree 🧩 Problem Statement: Given a binary tree, determine if it is height-balanced. A height-balanced binary tree is a binary tree in which the depth of the two subtrees of every node never differs by more than 1. Example: Input: root = [3,9,20,null,null,15,7] Output: true Explanation: Both left and right subtrees of every node differ in height by at most 1. 💡 Approach (Simple & Efficient): Use a recursive function to calculate the height of each subtree. If any subtree is unbalanced, return -1. Otherwise, return the height of the tree. If the final result is -1 → tree is not balanced. 💻 Java Code: class Solution { public boolean isBalanced(TreeNode root) { return checkHeight(root) != -1; } private int checkHeight(TreeNode node) { if (node == null) return 0; int leftHeight = checkHeight(node.left); if (leftHeight == -1) return -1; int rightHeight = checkHeight(node.right); if (rightHeight == -1) return -1; if (Math.abs(leftHeight - rightHeight) > 1) return -1; return 1 + Math.max(leftHeight, rightHeight); } } ⚙️ Complexity: ⏱️ Time: O(n) → Each node visited once 💾 Space: O(h) → Recursion stack (h = tree height) 🌱 Another step towards mastering Binary Trees! #Day33 #100DaysOfCode #LeetCode #Java #DSA #BinaryTree #BalancedTree #Recursion #CodingChallenge
To view or add a comment, sign in
-
-
✅ Day 33 of #100DaysOfCode Challenge 📘 LeetCode Problem 110: Balanced Binary Tree 🧩 Problem Statement: Given a binary tree, determine if it is height-balanced. A height-balanced binary tree is a binary tree in which the depth of the two subtrees of every node never differs by more than 1. Example: Input: root = [3,9,20,null,null,15,7] Output: true Explanation: Both left and right subtrees of every node differ in height by at most 1. 💡 Approach (Simple & Efficient): Use a recursive function to calculate the height of each subtree. If any subtree is unbalanced, return -1. Otherwise, return the height of the tree. If the final result is -1 → tree is not balanced. 💻 Java Code: class Solution { public boolean isBalanced(TreeNode root) { return checkHeight(root) != -1; } private int checkHeight(TreeNode node) { if (node == null) return 0; int leftHeight = checkHeight(node.left); if (leftHeight == -1) return -1; int rightHeight = checkHeight(node.right); if (rightHeight == -1) return -1; if (Math.abs(leftHeight - rightHeight) > 1) return -1; return 1 + Math.max(leftHeight, rightHeight); } } ⚙️ Complexity: ⏱️ Time: O(n) → Each node visited once 💾 Space: O(h) → Recursion stack (h = tree height) 🌱 Another step towards mastering Binary Trees! #Day33 #100DaysOfCode #LeetCode #Java #DSA #BinaryTree #BalancedTree #Recursion #CodingChallenge
To view or add a comment, sign in
-
-
Day 80 of #100DaysOfCode Solved Frequency Sort in Java 🔠 Approach Today's problem was to sort an array based on the frequency of its numbers. My initial solution was accepted, but it exposed a major efficiency gap! My strategy involved: Sorting the array first. Using nested loops to count the frequency of each number and store it in a HashMap. (Implied) Using the counts to perform the final custom sort. The core issue was the counting method: running a full loop inside another full loop to get the frequency. This quadratic $O(N^2)$ counting completely tanked the performance. ✅ Runtime: 29 ms (Beats 12.02%) ✅ Memory: 45.26 MB (Beats 5.86%) Another great lesson in algorithmic complexity! The difference between $O(N^2)$ and the optimal $O(N \log N)$ for this problem is massive. Time to refactor and implement the fast, single-pass $O(N)$ frequency count using getOrDefault! 💪 #Java #100DaysOfCode #LeetCode #CodingChallenge #Algorithms #Arrays #HashMap #Optimization #ProblemSolving
To view or add a comment, sign in
-
-
✅ Day 33 of #100DaysOfCode Challenge 📘 LeetCode Problem 110: Balanced Binary Tree 🧩 Problem Statement: Given a binary tree, determine if it is height-balanced. A height-balanced binary tree is a binary tree in which the depth of the two subtrees of every node never differs by more than 1. Example: Input: root = [3,9,20,null,null,15,7] Output: true Explanation: Both left and right subtrees of every node differ in height by at most 1. 💡 Approach (Simple & Efficient): Use a recursive function to calculate the height of each subtree. If any subtree is unbalanced, return -1. Otherwise, return the height of the tree. If the final result is -1 → tree is not balanced. 💻 Java Code: class Solution { public boolean isBalanced(TreeNode root) { return checkHeight(root) != -1; } private int checkHeight(TreeNode node) { if (node == null) return 0; int leftHeight = checkHeight(node.left); if (leftHeight == -1) return -1; int rightHeight = checkHeight(node.right); if (rightHeight == -1) return -1; if (Math.abs(leftHeight - rightHeight) > 1) return -1; return 1 + Math.max(leftHeight, rightHeight); } } ⚙ Complexity: ⏱ Time: O(n) → Each node visited once 💾 Space: O(h) → Recursion stack (h = tree height) 🌱 Another step towards mastering Binary Trees! #Day33 #100DaysOfCode #LeetCode #Java #DSA #BinaryTree #BalancedTree #Recursion #CodingChallenge
To view or add a comment, sign in
-
-
✅ Day 31 of #100DaysOfCode Challenge 📘 LeetCode Problem 104: Maximum Depth of Binary Tree 🧩 Problem Statement: Given the root of a binary tree, return its maximum depth. A binary tree's maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node. Example: Input: root = [3,9,20,null,null,15,7] Output: 3 Explanation: The longest path is 3 → 20 → 7, so the depth = 3. --- 💡 Approach: We can solve this using Recursion — If the tree is empty → depth = 0 Recursively find the depth of the left and right subtrees The maximum depth = 1 + max(leftDepth, rightDepth) --- 💻 Java Code: class Solution { public int maxDepth(TreeNode root) { if (root == null) return 0; int leftDepth = maxDepth(root.left); int rightDepth = maxDepth(root.right); return 1 + Math.max(leftDepth, rightDepth); } } --- ⚙ Complexity Analysis: ⏱ Time Complexity: O(n) → Each node is visited once 💾 Space Complexity: O(h) → Call stack height (h = height of tree) --- 🌱 Another small step in the 100 Days of Code journey! #100DaysOfCode #Day31 #LeetCode #Java #DSA #CodingChallenge #BinaryTree #Recursion
To view or add a comment, sign in
-
-
✅ Day 31 of #100DaysOfCode Challenge 📘 LeetCode Problem 104: Maximum Depth of Binary Tree 🧩 Problem Statement: Given the root of a binary tree, return its maximum depth. A binary tree's maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node. Example: Input: root = [3,9,20,null,null,15,7] Output: 3 Explanation: The longest path is 3 → 20 → 7, so the depth = 3. --- 💡 Approach: We can solve this using Recursion — If the tree is empty → depth = 0 Recursively find the depth of the left and right subtrees The maximum depth = 1 + max(leftDepth, rightDepth) --- 💻 Java Code: class Solution { public int maxDepth(TreeNode root) { if (root == null) return 0; int leftDepth = maxDepth(root.left); int rightDepth = maxDepth(root.right); return 1 + Math.max(leftDepth, rightDepth); } } --- ⚙ Complexity Analysis: ⏱ Time Complexity: O(n) → Each node is visited once 💾 Space Complexity: O(h) → Call stack height (h = height of tree) --- 🌱 Another small step in the 100 Days of Code journey! #100DaysOfCode #Day31 #LeetCode #Java #DSA #CodingChallenge #BinaryTree #Recursion
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