#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
Valid Anagram Problem Solved with Sorting and Counting
More Relevant Posts
-
🔹 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 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
-
-
#100DaysOfCode – Day 77 Delete in a Doubly Linked List Problem: Given a doubly linked list and an integer x, remove the node at position x (1-indexed) and return the head of the updated list. Example: Input: 1 <-> 2 <-> 3 <-> 4, x = 3 Output: 1 <-> 2 <-> 4 My Approach: 1️⃣ Handled edge cases deleting the head or an empty list. 2️⃣ Traversed to the node at position x. 3️⃣ Updated both prev and next pointers to unlink the node cleanly. Time Complexity: O(N) Space Complexity: O(1) Understanding pointer manipulation in linked lists builds the foundation for mastering advanced data structures. Every prev and next link matters! #100DaysOfCode #Java #ProblemSolving #DSA #GeeksforGeeks #LinkedList #Pointers #TakeUForward #CodeNewbie #CodingJourney
To view or add a comment, sign in
-
-
🚀#PostLog34 🔹 LeetCode 1901: Find a Peak Element II Problem was focused on efficiently finding a peak element in a 2D grid — one that’s greater than all its adjacent cells (top, bottom, left, and right). 💡 Concept learned: Instead of scanning the entire grid, a binary search on columns helps reduce time complexity to O(m log n). For each middle column, find the global maximum element, then decide which side to move based on neighboring values — simple yet powerful divide-and-conquer logic. ✅ 58/58 test cases passed ⚡ Runtime: 0ms | Beats 100% of Java submissions Each problem reinforces how algorithmic thinking and optimization go hand-in-hand — step by step toward sharper logic and cleaner solutions. #LeetCode #Java #DSA #ProblemSolving #CodingJourney
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 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
To view or add a comment, sign in
-
-
✨ Day 56 of 100: Remove Duplicates from Sorted List II ✨ Today’s challenge was LeetCode 82 – Remove Duplicates from Sorted List II 🧩 Problem: Given a sorted linked list, delete all nodes that have duplicate numbers — leaving only distinct numbers from the original list. Example: Input: 1 -> 2 -> 3 -> 3 -> 4 -> 4 -> 5 Output: 1 -> 2 -> 5 Approach: 🔹 Use a dummy node to handle edge cases easily (like when the first few nodes are duplicates). 🔹 Traverse the list using two pointers (prev and current). 🔹 If duplicates are found (i.e., current.val == current.next.val), skip all nodes with that value. 🔹 Otherwise, move prev forward. This approach ensures all duplicate sequences are fully removed, not just reduced. 💡 Key Takeaways: The dummy node trick simplifies edge case handling. Mastered pointer manipulation in linked lists. Reinforced understanding of data cleanup in sorted sequences. #100DaysOfCode #Day56 #LeetCode #Java #LinkedList #CodingChallenge #ProblemSolving
To view or add a comment, sign in
-
-
🚀 𝗗𝗮𝘆 𝟮𝟰𝟱 𝗼𝗳 𝟮𝟰𝟳 𝗟𝗲𝗲𝘁𝗖𝗼𝗱𝗲 𝗖𝗵𝗮𝗹𝗹𝗲𝗻𝗴𝗲 🔹 Problem: 📘 91. Decode Ways 🧩 Approach: We are given a string of digits, and we must determine how many ways it can be decoded to letters (using mapping "1" -> 'A' to "26" -> 'Z'). Dynamic Programming (DP) is used to count possible decodings by analyzing single and two-digit combinations. 📘 Steps to Solve: If the string starts with '0', return 0 (invalid code). Use a DP array where dp[i] represents the number of ways to decode up to index i. For each index i, check: The single digit (s[i-1]) is valid (1–9). The two-digit substring (s[i-2..i-1]) is valid (10–26). Add valid possibilities to build dp[i]. The final answer is dp[n]. ✅ Time Complexity: O(n) ✅ Space Complexity: O(n) ✨ What I Learned Today: Dynamic Programming simplifies complex recursive problems by storing partial solutions — a beautiful example of optimization in action! 💡 Let's do this! #LeetCode #Day247 #Java #DynamicProgramming #MediumProblem #247Challenge #CodingJourney
To view or add a comment, sign in
-
-
Day 46/100 – #100DaysOfCode 🚀 | #Java #BFS #Graph ✅ Problem Solved: Word Ladder (LeetCode 127) 🧩 Problem Summary: You are given a beginWord, endWord, and a list of words. You must transform beginWord → endWord by changing one character at a time, where each intermediate word must exist in the dictionary. Return the minimum number of transformations. 💡 Approach Used: Used Breadth-First Search (BFS) because: We need the shortest path in terms of transformation steps. Each word is considered a node in a graph. Key Steps: Pre-processed all words into generic pattern groups (e.g., h*t → hot, hit, etc.). Performed BFS from beginWord until reaching endWord. Counted transformation depth as solution. ⚙️ Time Complexity: O(N × L²) 📦 Space Complexity: O(N × L) ✨ Takeaway: This problem is an excellent example of how BFS can be used for shortest transformation sequences in word-graphs. #Java #LeetCode #Graph #BFS #ProblemSolving #100DaysOfCode #CodingChallenge
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