🔹 Day 62 of #100DaysOfLeetCodeChallenge 🔹 🚀 Problem: String to Integer (atoi) 🔑 Topic: String Manipulation 🧠 Approach: Step 1: Trim leading whitespaces. Step 2: Check for a sign (+ or -). Step 3: Convert valid digits to integer until a non-digit is found. Step 4: Clamp values beyond 32-bit signed range to [-2³¹, 2³¹ - 1]. Used a long to handle overflow safely before final conversion. ✅ ⏳ Time Complexity: O(n) 💾 Space Complexity: O(1) 📌 Example: Input: "42" → Output: 42 Input: " -042" → Output: -42 🎯 Takeaway: Always handle edge cases — whitespace, signs, and overflow — while converting strings to numbers. 💡 #LeetCode #String #Parsing #Java #ProblemSolving #100DaysOfLeetCodeChallenge 🚀
How to convert string to integer in Java with edge cases
More Relevant Posts
-
🔹 Day 50: Maximum Average Subarray I (LeetCode #643) 📌 Problem Statement: Given an integer array nums and an integer k, find the contiguous subarray of length k that has the maximum average value and return this value. ✅ My Approach: I used the sliding window technique to efficiently calculate the sum of subarrays of length k. First, I computed the sum of the first k elements. Then, as the window slid forward, I subtracted the element going out and added the new element entering the window. I continuously updated the maximum sum encountered and finally returned the average by dividing it by k. 📊 Complexity: Time Complexity: O(n) Space Complexity: O(1) ⚡ Submission Stats: Runtime: 2 ms (Beats 99.90%) Memory: 56.27 MB (Beats 66.43%) 💡 Reflection: This problem highlighted how the sliding window technique can drastically improve performance over recalculating sums for each subarray, making the approach both elegant and efficient. 🚀 #LeetCode #Java #SlidingWindow #Optimization #100DaysOfCode #Day50
To view or add a comment, sign in
-
-
🌟 Day 92 of #100DaysOfLeetCode — Shuffle String (LeetCode 1528) Today’s problem was a simple yet elegant string manipulation challenge — “Shuffle String”, which tests your ability to rearrange elements based on given indices. 🧩 Problem: We are given a string s and an integer array indices. Each character in s should be placed at the new position given by indices[i]. The goal is to return the newly shuffled string. 🧠 Logic: Create a character array result of the same length as s. Loop through each index i. Place s.charAt(i) in result[indices[i]]. Finally, return the new string formed from result. ⏱️ Time Complexity: O(n) 💾 Space Complexity: O(n) 💬 This problem reminded me how simple array manipulation techniques can efficiently solve string rearrangement tasks — a great warm-up for more complex mapping problems! #Day92 #LeetCode #100DaysOfCode #Java #ProblemSolving #CodingJourney #WomenInTech
To view or add a comment, sign in
-
-
🔹 Day 46: Is Subsequence (LeetCode #392) 📌 Problem Statement: Given two strings s and t, return true if s is a subsequence of t, or false otherwise. A subsequence is formed by deleting some (possibly none) characters from the original string without disturbing the relative order of the remaining characters. ✅ My Approach: I used a two-pointer technique — one pointer iterates through string t, and the other tracks progress through string s. Each time a matching character is found, the pointer for s moves forward. If we reach the end of s, it means all its characters appeared in sequence within t. 📊 Complexity: Time Complexity: O(n) Space Complexity: O(1) ⚡ Submission Stats: Runtime: 2 ms (Beats 68.53%) Memory: 41.32 MB (Beats 87.28%) 💡 Reflection: A simple yet elegant problem that highlights how pointer movement can efficiently handle string comparisons without extra memory usage. ✨ #LeetCode #Java #Strings #TwoPointers #100DaysOfCode #Day46
To view or add a comment, sign in
-
-
🔥 **Blind 75 Challenge – Day 8: Longest Consecutive Sequence (LeetCode #128)** Today’s problem focuses on **detecting consecutive sequences efficiently** using a HashSet — one of the most elegant O(n) solutions in the #Blind75 list. 🧩 **Problem Statement:** Given an unsorted array of integers, return the length of the longest consecutive elements sequence. You must solve it in **O(n)** time. **Example:** Input: `[100, 4, 200, 1, 3, 2]` Output: `4` (sequence: 1, 2, 3, 4) 💭 **My Approach:** * Store all numbers in a **HashSet** for O(1) lookups * For each number, start counting only if it’s the **beginning of a sequence** (`num - 1` not in set) * Expand forward until the sequence breaks * Track the longest streak 🧠 **Time Complexity:** O(n) 💾 **Space Complexity:** O(n) 💡 **Key Takeaway:** This problem reinforces how **sets and linear scans** can replace nested loops — a simple yet powerful optimization mindset. #LeetCode #DSA #CodingChallenge #Java #ProblemSolving #DailyCoding #CareerGrowth #Blind75
To view or add a comment, sign in
-
-
🚀 Day 39 of #100DaysOfLeetCode Problem: 125. Valid Palindrome 🧩 Goal: Check if a given string is a palindrome — ignoring spaces, punctuation, and case. 💻 Approach: 1️⃣ Remove all non-alphanumeric characters. 2️⃣ Convert to lowercase. 3️⃣ Compare the cleaned string with its reverse. class Solution { public boolean isPalindrome(String s) { StringBuilder cleaned = new StringBuilder(); for (char c : s.toCharArray()) { if (Character.isLetterOrDigit(c)) { cleaned.append(Character.toLowerCase(c)); } } String str = cleaned.toString(); String rev = cleaned.reverse().toString(); return str.equals(rev); } } 🕒 Time Complexity: O(n) 💡 Concepts Used: String Manipulation, Two-Pointer Logic ✅ A simple yet powerful problem that helps strengthen string handling fundamentals in Java. #Day39 #LeetCode #Java #CodingChallenge #100DaysOfCode #ProblemSolving
To view or add a comment, sign in
-
-
📌 Day 22/100 – Sort Colors (LeetCode 75) 🔹 Problem: Given an array containing 0s, 1s, and 2s (representing red, white, and blue), sort them in-place so that all same colors stay together in the order: 0 → 1 → 2. No built-in sorting allowed. 🔹 Approach Used: Counting Sort Count occurrences of 0, 1, and 2. Overwrite the array based on those counts. Done in single pass + rewrite, no extra sorting logic needed. 🔹 Time & Space Complexity: ⏳ Time: O(n) — one pass to count, one pass to rewrite 🧠 Space: O(1) — constant extra space 🔹 Key Learnings: ✅ Problems become simpler when we leverage constraints (only 3 values here) ✅ Counting sort is perfect when input range is small & fixed ✅ Always look for in-place optimizations before thinking of extra space #100DaysOfCode #LeetCode #Java #ProblemSolving #DSA #CodingJourney
To view or add a comment, sign in
-
-
💡 Day 34/100 ✅ Remove Zeros in Decimal Representation Today’s challenge was about removing all the zeros from a given number’s decimal form. For example, 1020030 → 123. It might look simple, but it reinforced the importance of handling edge cases like n = 0 or n = 1000, which can lead to empty strings or parsing errors. 🧠 Key Features: Practiced both String-based and Math-based approaches. Explored the difference between using int and long for large numbers. Strengthened understanding of number-to-string conversions and parsing. ⚙️ Time Complexity: O(d) (where d = number of digits) 💾 Space Complexity: O(d) #Day33Of100 #DSA #Java #ProblemSolving #CodingJourney #100DaysOfCode #LearnEveryday #LogicBuilding
To view or add a comment, sign in
-
-
💻 Day 48 of #LeetCode100DaysChallenge Solved LeetCode 151: Reverse Words in a String — a simple yet elegant string manipulation problem that tests precision and edge-case handling. 🧩 Problem: Given a string s, reverse the order of words while removing extra spaces. Each word is a sequence of non-space characters, and the result should contain exactly one space between words with no leading or trailing spaces. 💡 Approach Used — String Splitting & Reversal: 1️⃣ Trim leading and trailing spaces. 2️⃣ Split the string by spaces to get individual words. 3️⃣ Filter out empty strings caused by multiple spaces. 4️⃣ Reverse the list of words and join them with a single space. ⚙️ Complexity: Time: O(N) Space: O(N) ✨ Key Takeaways: ✅ Strengthened understanding of string manipulation and trimming. ✅ Learned to handle irregular spacing efficiently. ✅ Practiced building clean, readable string-processing logic. #LeetCode #100DaysOfCode #Java #StringManipulation #ProblemSolving #DSA #CodingJourney #WomenInTech
To view or add a comment, sign in
-
-
🌳 Day 60 of #100DaysOfCode 🌳 🔹 Problem: Balanced Binary Tree – LeetCode ✨ Approach: Used a post-order DFS traversal to calculate subtree heights while checking balance at every node. If the height difference of any subtree exceeds 1, return -1 immediately for an early exit — efficient and elegant! ⚡ 📊 Complexity Analysis: Time: O(n) — each node visited once Space: O(h) — recursion stack space, where h is the tree height ✅ Runtime: 0 ms (Beats 100%) ✅ Memory: 44.29 MB 🔑 Key Insight: A balanced tree isn’t just about equal heights — it’s about smart recursion that detects imbalance early, saving both time and memory. 🌿 #LeetCode #100DaysOfCode #Java #DSA #BinaryTree #Recursion #ProblemSolving #AlgorithmDesign #CodeJourney #ProgrammingChallenge
To view or add a comment, sign in
-
-
#100DaysOfCode – Day 68 String Manipulation Problem 1:- Largest Odd Number in String Task:- Given a numeric string, return the largest-valued odd number (as a substring) or an empty string if none exists. Example: Input: num = "35427" → Output: "35427" My Approach: Started scanning the string from right to left. The first odd digit encountered marks the end of the required substring. Returned the substring from start to that index. Time Complexity:- O(N) Space Complexity:- O(1) Sometimes, it’s not about complex algorithms just a small logical observation can lead to an efficient solution. #takeUforward #100DaysOfCode #Java #ProblemSolving #LeetCode #CodeNewbie #StringManipulation #LogicBuilding #CleanCode
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