Keeping the Two-Pointer momentum going! Today's challenge: Valid Palindrome. After using two pointers to reverse arrays and remove duplicates, I applied the exact same pattern to strings. The goal? Check if a string reads the same forwards and backwards (like "racecar"). While you could solve this by creating a reversed copy of the string and comparing the two, that requires O(n) extra memory. The Two-Pointer approach handles it entirely in-place with O(1) space! Here is the logic: • Left Pointer: Starts at the very beginning of the string. • Right Pointer: Starts at the very end. • Compare: If the characters match, great! If they don't, it's not a palindrome. • Move Inward: Step both pointers toward the middle until they cross. It is incredibly satisfying to see how one core algorithmic pattern can be adapted to solve so many different types of problems so efficiently. #DSA #Java #LeetCode #isValidPalindrome
Valid Palindrome with Two Pointers in O(1) Space
More Relevant Posts
-
Day 72 ✅ — Longest Palindromic Substring (LC #5) This one looked simple. It wasn't. My first instinct was DP. Big table, fill diagonals, track the max. It works — but O(n²) space and honestly more code than needed. Then I remembered: every palindrome expands outward from a center. So instead of building a table, I just picked each character as a potential center and expanded left/right as long as the characters matched. Two cases to handle: → Odd length palindromes: center is one character (i, i) → Even length palindromes: center is between two characters (i, i+1) That's it. No DP. No extra space. Same time complexity, half the headache. Runtime: 0 ms 🔥 — The pattern that clicked for me: If you're thinking about palindromes and reach for DP first — pause. Ask yourself: can I just expand from each center? Nine times out of ten, that's the cleaner path. — Day 71 → Arrays done Day 72 → Strings in progress Slow and steady. 💪 #DSAChallenge #Day72of365 #LeetCode #Java #Strings #CodingJourney #BackendDeveloper
To view or add a comment, sign in
-
-
Day 86/100 🚀 | #100DaysOfDSA Solved LeetCode 125 – Valid Palindrome today. Approach: Used the two-pointer technique to check palindrome while ignoring non-alphanumeric characters. • Initialized left at start and right at end • Skipped characters that are not letters/digits using Character.isLetterOrDigit() • Compared characters in lowercase to ensure case-insensitivity • Moved pointers inward until they meet This avoids creating a new string and keeps the solution efficient. Time Complexity: O(n) Space Complexity: O(1) Key takeaway: For string problems involving filtering + comparison, prefer two pointers instead of building a new string to optimize space. #100DaysOfDSA #LeetCode #DSA #Java #TwoPointers #Strings #ProblemSolving #Consistency
To view or add a comment, sign in
-
-
The Two-Pointer streak continues! Today’s LeetCode Problem: Is Subsequence. After using multiple pointers to sort arrays and move zeroes, I applied the exact same pattern to string manipulation today. The challenge was figuring out if a shorter string (s) is a valid subsequence of a longer string (t) without disturbing the relative order of the characters. Instead of generating all possible subsequences (which would be a massive O(2ⁿ) performance drain), the Two-Pointer approach solves this beautifully in a single pass. Knocking this out in O(n) time and O(1) space is another great reminder of how incredibly versatile this algorithmic pattern is for both arrays and strings. #DSA #Java #LeetCode #IsSebsequenceProblem
To view or add a comment, sign in
-
-
Day 22/100: Longest Common Prefix 📏 Today's challenge was about finding the common starting thread in an array of strings. The Logic: Vertical Scanning Instead of comparing whole words, I looked at them character by character across the entire list. If "flower", "flow", and "flight" all have 'f' and 'l' at the start, that's our prefix. As soon as a mismatch happens, we cut the string and return the result. Complexity: O(S) where S is the sum of all characters in all strings. #100DaysOfCode #Java #DSA #Strings #CodingJourney #Day22 #LearnInPublic
To view or add a comment, sign in
-
-
Solved “Longest Repeating Character Replacement” today — this one really helped me understand sliding window better. At first, I was thinking in terms of overall frequency, but that approach fails because the problem is about a contiguous substring, not the whole string. The key idea that clicked was: Instead of fixing the window size, let it grow and shrink based on a condition. For any window: (window size - max frequency) ≤ k This tells us how many characters we need to replace. If it exceeds k, we shrink the window from the left. One interesting part was that we don’t need to perfectly maintain max frequency while shrinking — even a slightly outdated value still works correctly. This problem really reinforced an important pattern: Don’t decide the window size — let the condition control it. #dsa #slidingwindow #java #coding #problemSolving
To view or add a comment, sign in
-
-
Day 33/50 🚀 — Valid Palindrome (Two Pointer Approach) Today’s problem was a great mix of string manipulation + two pointers. 🔹 Ignored non-alphanumeric characters 🔹 Handled case-insensitivity 🔹 Compared characters from both ends efficiently Key insight: Instead of preprocessing the string, we can optimize in-place using two pointers, skipping unwanted characters on the go. 💡 This improves both readability and performance. Performance: ⚡ Runtime: 2 ms (99%+) 📦 Memory: Efficient #Day33 #LeetCode #TwoPointers #DSA #Java #CodingJourney #50DaysOfCode #ProblemSolving
To view or add a comment, sign in
-
-
🔥 2. Array Coding Questions 1. Find the second largest number in an array. 2. Find the missing number in an array. 3. Find duplicates in an array. 4. Rotate an array by k positions. 5. Find the intersection of two arrays. 6. Move all zeros to the end of the array. 7. Find the maximum subarray sum (Kadane’s Algorithm). 8. Find pairs with a given sum. 9. Merge two sorted arrays. 10. Find the majority element. #arrays #interviewquestions #java #java8
To view or add a comment, sign in
-
💡 Day 47 of LeetCode Problem Solved! 🔧 🌟 344. Reverse String 🌟 🔗 Solution Code: https://lnkd.in/gE6FQpQN Task: Write a function that reverses a string. The input string is given as an array of characters s. You must do this by modifying the input array in-place with O(1) extra memory. Example 1: Input: ["h","e","l","l","o"] Output: ["o","l","l","e","h"] Example 2: Input: ["H","a","n","n","a","h"] Output: ["h","a","n","n","a","H"] #LeetCode #Java #DSA #ProblemSolving #Consistency #100DaysOfChallenge #CodingJourney #KeepGrowing
To view or add a comment, sign in
-
-
Day 82 - Kth Smallest Element in a BST Used inorder traversal to find the kth smallest element in a binary search tree. Approach: • Inorder traversal of BST gives sorted order • Maintain a counter while traversing • When count == k, capture the value Time Complexity: O(n) Space Complexity: O(h) #Day82 #LeetCode #BST #BinaryTree #DSA #Java #CodingJourney
To view or add a comment, sign in
-
-
Day 83 - Path Sum Checked whether a binary tree has a root-to-leaf path equal to a given target sum. Approach: • Subtract current node value from targetSum • Recursively check left and right subtrees • At leaf node, verify if remaining sum equals node value Time Complexity: O(n) Space Complexity: O(h) #Day83 #LeetCode #BinaryTree #Recursion #DSA #Java #CodingJourney
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