🚀 Day 9 of 100 Days of LeetCode 🧩 Problem: #242. Valid Anagram 💻 Difficulty: Easy 🔍 Concept: Check whether two strings are anagrams — i.e., they contain the same characters in the same frequency, just rearranged. ⚙️ Approach Used: Used a frequency array of size 26 to count occurrences of each character in the first string. Decreased counts while scanning the second string. If all frequencies end up zero ➜ both strings are anagrams ✅ 🧠 Key Learnings: Practiced frequency array patterns for string problems. Learned to avoid sorting-based solutions for better time complexity (O(n) instead of O(n log n)). 💻 Code (Java): class Solution { public boolean isAnagram(String s, String t) { int[] freq = new int[26]; for (char c : s.toCharArray()) freq[c - 'a']++; for (char c : t.toCharArray()) freq[c - 'a']--; for (int f : freq) if (f != 0) return false; return true; } } 💬 Reflection: Small wins count too! Even the simplest problems help in strengthening your logical foundation and pattern recognition. #100DaysOfLeetCode #Day9 #ProblemSolving #Java #LeetCode #DSA #CodingChallenge #LearningEveryday
"Day 9: Valid Anagram solution in Java"
More Relevant Posts
-
#100DaysOfCode – Day 73 String Manipulation Problem: Valid Anagram (LeetCode #242) Task: Given two strings s and t, return true if t is an anagram of s, otherwise false. Example: Input: s = "anagram", t = "nagaram" → Output: true Input: s = "rat", t = "car" → Output: false My Approach:- Method 1 – Sorting Converted both strings into character arrays. Sorted them and compared if equal, they’re anagrams! Time Complexity: O(n log n) Method 2 – Frequency Count (Optimized) Counted occurrences of each character in s and t. If every count matches, it’s a valid anagram. Time Complexity: O(n) | Space: O(1) String problems may look simple, but optimizing from sorting to counting makes a huge difference. Small logic shifts often lead to big performance gains! #100DaysOfCode #Java #ProblemSolving #LeetCode #CodingJourney #takeUforward #DataStructures #Algorithms #StringManipulation #GeeksForGeeks #CodeNewbie
To view or add a comment, sign in
-
-
🚀 Just Solved LeetCode #124 — Binary Tree Maximum Path Sum 📘 Problem: Given the root of a binary tree, return the maximum path sum of any non-empty path. A path can start and end at any node, and it’s the sum of all node values along that path. Example: Input → [1,2,3] Output → 6 Explanation → The optimal path is 2 → 1 → 3 with a sum of 6. 🧠 My Approach: I used a recursive DFS (Depth First Search) to calculate the maximum gain from each node: 1️⃣ For each node, calculate the maximum path sum including its left and right subtrees. 2️⃣ Compare the sum of the current path with the global maximum (`maxSum`). 3️⃣ Return the max gain that can be extended to the parent node (node value + max of left/right gain). 4️⃣ Used `Math.max()` to ensure negative paths don’t reduce the result. 💡 What I Learned: ✅ Importance of returning gain vs total path sum in recursive tree problems ✅ How to handle global state (`maxSum`) in recursion ✅ Deepened understanding of DFS-based tree traversal and dynamic recursion logic #LeetCode #Java #DSA #BinaryTree #CodingJourney
To view or add a comment, sign in
-
-
🔹 Day 47: Find Pivot Index (LeetCode #724) 📌 Problem Statement: Given an array of integers nums, the pivot index is the index where the sum of all numbers to the left is equal to the sum of all numbers to the right. If no such index exists, return -1. If multiple exist, return the leftmost one. ✅ My Approach: I first calculated the total sum of the array, then iterated through each element keeping track of the left sum. At each index, I checked whether left sum == total sum - left sum - current element. If true, that index is the pivot index. 📊 Complexity: Time Complexity: O(n) Space Complexity: O(1) ⚡ Submission Stats: Runtime: 0 ms (Beats 100%) Memory: 45.41 MB (Beats 66.27%) 💡 Reflection: This problem strengthened my understanding of prefix sums and efficient single-pass array traversal. A clean and optimized logic! 💪 #LeetCode #Java #Arrays #PrefixSum #100DaysOfCode #Day47
To view or add a comment, sign in
-
-
💻 Day 53 of #100DaysOfCode – LeetCode #206: Reverse Linked List Today’s challenge was one of the most classic problems in data structures — reversing a linked list. This problem is a great way to strengthen your understanding of pointers and linked list manipulation. 🧩 Problem: Given the head of a singly linked list, reverse it and return the new head. ⚙ Approach: I used an iterative approach with three pointers: prev, curr, and next. Store the next node Reverse the current node’s pointer Move forward until the list is reversed 🧠 Key Learning: This problem really helped me visualize how pointers work in linked structures — a great exercise for understanding memory links and iterative logic. ✅ Time Complexity: O(n) ✅ Space Complexity: O(1) Here’s my simple Java solution: class Solution { public ListNode reverseList(ListNode head) { ListNode prev = null, curr = head; while (curr != null) { ListNode next = curr.next; curr.next = prev; prev = curr; curr = next; } return prev; } } 🔥 Every day, one step closer to mastering data structures! #Day53 #LeetCode #Java #100DaysOfCode #CodingJourney #LinkedList #DSA #Programming #ReverseLinkedList
To view or add a comment, sign in
-
-
💻 Day 03/10 of Linked List Series: Inserting a Node at the Beginning of a Linked List Let’s understand how we can add a new node at the start of a linked list 👇 ⚙️ Algorithm: Insert at Beginning Create a new node with the given data (for example, 'A'). Set the next pointer of this new node to point to the current head of the list. Now, make this new node the new head of the linked list. 💡 In simple terms: you’re placing a new link before the first link in the chain. 🧩 Example Explanation: Initial list: B → C → D We want to insert 'A' at the beginning. Steps: Create a new node 'A'. Point 'A' → 'B'. Return node 'A'. ✅ Final list becomes: A → B → C → D ✨ This operation is one of the most common and easiest insert operations in linked lists — it just needs a few pointer adjustments! 📚 Stay tuned for the next part in this #LinkedListSeries! Follow my Brand 👉 #CodeWithLakkojuEswaraSai #CodeWithLakkojuEswaraSai_LinkedList #DSA #Java #10000coders
To view or add a comment, sign in
-
-
Day 16: Solving “Find Pivot Index” – LeetCode (Java) Today, I explored an interesting problem called “Find Pivot Index” from LeetCode. It really helped me understand how to balance sums on both sides of an array element. Problem Statement: Given an array, find the index where the sum of all elements on the left is equal to the sum of all elements on the right. If no such index exists, return -1. Key Logic: For each element: Calculate leftSum = sum of elements before the index. Calculate rightSum = sum of elements after the index. Compare both. If they match → that’s your pivot index! 🔍 Example: Input: [1, 7, 3, 6, 5, 6] Output: 3 Explanation: Left sum = 11, Right sum = 11 Learning Moment: Concepts Used: Nested loops Prefix sums Edge case handling Logical debugging Next Step: I’ll now optimize this logic to a more efficient approach (using total sum & prefix sum) to make it run in O(n) time. #Java #LeetCode #DSA #ProblemSolving #LearningJourney #90DaysOfCode #BCA #CareerGrowth
To view or add a comment, sign in
-
-
🔹 Day 38 – LeetCode Practice Problem: Missing Number (LeetCode #268) 📌 Problem Statement: You are given an array nums containing n distinct numbers taken from the range [0, n]. Find the one number that is missing from the array. ✅ My Approach (Java): Calculated the expected sum using the formula total = \frac{n \times (n + 1)}{2} The missing number = total - actual sum. This method avoids sorting or extra space, keeping it optimal. 📊 Complexity: Time Complexity: O(n) Space Complexity: O(1) ⚡ Submission Results: Accepted ✅ Runtime: 0 ms (Beats 100%) 🚀 Memory: 45.51 MB (Beats 32.44%) 💡 Reflection: A great reminder that sometimes the most efficient solutions come from simple mathematical reasoning. Clean, elegant, and lightning-fast! #LeetCode #ProblemSolving #Java #DSA #CodingPractice #Learning
To view or add a comment, sign in
-
-
🗓 Day 7 / 100 – #100DaysOfLeetCode 🔢 Problem 2536: Increment Submatrices by One Today’s challenge involved processing multiple submatrix increment queries on an n x n matrix, initially filled with zeros. 🧠 My Approach Instead of updating every cell inside each submatrix (which would be too slow for up to 10⁴ queries), I used a row-wise difference array technique. For each query [r1, c1, r2, c2]: Increment prefix[row][c1] Decrement prefix[row][c2+1] (if within bounds) This allows efficient marking of increments. Later, prefix-summing each row reconstructs the final matrix. ⏱ Time Complexity O(q × n) where q = number of queries (We touch r2 - r1 + 1 rows per query) 💾 Space Complexity O(n²) for the prefix matrix #LeetCode #Java #ProblemSolving #CodingChallenge #100DaysOfCode #DSA #LearningEveryday
To view or add a comment, sign in
-
-
💻 Day 53 of #100DaysOfCode – LeetCode #206: Reverse Linked List Today’s challenge was one of the most classic problems in data structures — reversing a linked list. This problem is a great way to strengthen your understanding of pointers and linked list manipulation. 🧩 Problem: Given the head of a singly linked list, reverse it and return the new head. ⚙ Approach: I used an iterative approach with three pointers: prev, curr, and next. Store the next node Reverse the current node’s pointer Move forward until the list is reversed 🧠 Key Learning: This problem really helped me visualize how pointers work in linked structures — a great exercise for understanding memory links and iterative logic. ✅ Time Complexity: O(n) ✅ Space Complexity: O(1) Here’s my simple Java solution: class Solution { public ListNode reverseList(ListNode head) { ListNode prev = null, curr = head; while (curr != null) { ListNode next = curr.next; curr.next = prev; prev = curr; curr = next; } return prev; } } 🔥 Every day, one step closer to mastering data structures! #Day53 #LeetCode #Java #100DaysOfCode #CodingJourney #LinkedList #DSA #Programming #ReverseLinkedList
To view or add a comment, sign in
-
-
💻 Day 52 of #100DaysOfCodeChallenge Today’s problem: LeetCode 203 – Remove Linked List Elements 🧩 I worked on a Linked List manipulation problem where the goal is to remove all nodes that match a given value. This problem helped strengthen my understanding of pointer handling and dummy node usage in linked lists. 🧠 What I Learned: Handling edge cases where the head itself might need to be removed. Using a dummy node before the head to simplify pointer operations. How current and next pointers work together to modify a linked list safely. 🧩 Java Solution: class Solution { public ListNode removeElements(ListNode head, int val) { ListNode dummy = new ListNode(0); dummy.next = head; ListNode current = dummy; while (current.next != null) { if (current.next.val == val) current.next = current.next.next; else current = current.next; } return dummy.next; } } ⏱️ Complexity: Time: O(n) Space: O(1) Every day, I’m getting more confident with data structures and linked list traversal logic. On to the next challenge 🚀 #Day52 #LeetCode #Java #100DaysOfCode #CodingChallenge #LinkedList #ProblemSolving
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