#2553 — Separate the Digits in an Array (Easy) ✅ Problem: Given an array of positive integers, decompose each integer into its individual decimal digits and return them in a single array, preserving the original order of both numbers and digits. Example: nums = [13, 25, 83, 77] → [1, 3, 2, 5, 8, 3, 7, 7] Approach: Iterate through the input array, convert each integer to a string, extract every character, and map it back to its numeric value by subtracting the character '0'. Append the results sequentially. Implementation insight: The logic can be expressed in two equivalent styles — an explicit loop with an intermediate List, or a single Java stream pipeline using flatMap to flatten each number into its digit stream. Both achieve O(n × d) time and O(n × d) space, where d ≤ 6 (max digits per number). Key takeaways: String conversion keeps digit extraction clean and readable. The ordering guarantee (number order → digit order) is straightforward to maintain with a linear traversal. Stream pipelines can reduce boilerplate without sacrificing clarity when used judiciously. 🔗 Solution: https://lnkd.in/gG6cYw6Z #LeetCode #Java #Coding #Algorithms #ProblemSolving
Separate Array Digits into Single Array
More Relevant Posts
-
Today I tackled the Sequence Equation problem, which involves working with permutations and index mapping. 🔍 Problem Insight: Given a permutation p, find values of y such that: 👉 p[p[y]] = x for all x from 1 to n 💡 Key Idea: Instead of brute force, I used: Position mapping for fast lookup Double indexing trick (pos[pos[x]]) 🚀 Why this works: It reduces complexity from O(n²) to O(n), making it highly efficient. 🧠 What I learned: Importance of inverse mapping Optimizing lookup operations Writing clean and efficient Java code 📌 Example: Input: [5, 2, 1, 3, 4] Output: [4, 2, 5, 1, 3] 🔖 Tags #Java #DataStructures #Algorithms #CodingChallenge #ProblemSolving #100DaysOfCode #DeveloperJourney #SoftwareDeveloper #Hackerrank #CodingPractice #TechLearning
To view or add a comment, sign in
-
-
Nested encoded strings can look complex at first, but stacks make the decoding process systematic and manageable. 🚀 Day 144/365 — DSA Challenge Solved: Decode String Problem idea: Given an encoded string in the format: k[encoded_string] Repeat the string inside brackets exactly k times and return the fully decoded result. Efficient approach: 1. Traverse the string character by character 2. Build numbers for repeat counts 3. Use stacks to store: - Previous strings - Repeat counts 4. When ] appears: - Repeat current substring - Merge it with previous stored string Key Learning: - Stack-based string decoding - Handling nested patterns - Managing multiple states simultaneously - Parsing strings efficiently ⏱ Time: O(n) 📦 Space: O(n) Day 144/365 complete. 💻 221 days to go. Code: https://lnkd.in/dad5sZfu #DSA #Java #Stack #String #LeetCode #ProblemSolving #LearningInPublic
To view or add a comment, sign in
-
-
Day 134: Keeping It Simple ✅📊 Problem 2784: Check if Array is Good After yesterday's intense difference array logic, today was a welcome return to basic validation and set mechanics. The goal was to check if an array is a base permutation of length n with one extra copy of n. The Strategy: • Bound Check: The maximum valid number in the array must equal nums.length−1. Anything larger is an immediate false. • Set Filtering: I used a HashSet to keep track of unique numbers. • Duplicate Logic: The only allowed duplicate is the maximum element n. If any other number repeats, or if n repeats more than once, the array is invalid. I went with Java today for a quick, clean O(N) hash set approach. Day 134—consistency doesn't always require a complex algorithm, just solid logic. 🚀 #LeetCode #Java #DataStructures #Algorithms #ProblemSolving #DailyCode
To view or add a comment, sign in
-
-
✳️Day 51 of #100DaysOfCode✳️ 🚀 Cracking the Sliding Window: LeetCode 1004 (Max Consecutive Ones III) I recently tackled this popular interview question using an efficient Sliding Window approach in Java. 🔹The challenge: Find the maximum number of consecutive 1s in a binary array if you can flip at most k 0s. ⭐Here is the step-by-step breakdown of how my solution works: 🔸Initialize Two Pointers: Use a fast pointer (j) to expand the window and a slow pointer (i) to contract it, keeping track of the maxLen and the number of zeroes encountered. 🔸Expand the Window: Iterate through the array with j. If nums[j] == 0, increment the zero counter. 🔹Shrink When Invalid: If the number of zeros exceeds k, move the left pointer i forward. If the element leaving the window (nums[i]) is a 0, decrement the zero counter. 🔹Update Max Length: At each step, calculate the valid window size using j - i + 1 and update the maximum length found so far. 🔹Complexity: Time Complexity: O(N) – Each element is visited at most twice. Space Complexity: O(1) – Optimal memory usage with no extra data structures. Always satisfying to see all test cases pass with clean, readable code! 💻✨ #LeetCode #Java #DataStructures #Algorithms #SlidingWindow #CodingLife #SoftwareEngineering
To view or add a comment, sign in
-
-
🚀 Day 13 — #100DaysOfLeetCode Today’s problem: Split Array Largest Sum (LeetCode 410) Initially, I tried solving this problem using brute force by checking every possible partition, but the complexity became too high. Then I explored DP Memoization, which was much better, but the most interesting part was learning the optimal approach using Binary Search + Greedy. ✔ Binary searched the answer space ✔ Greedily created partitions ✔ Minimized the largest subarray sum efficiently 📊 Complexity Comparison: Approach → Time → Space 🔸 Brute Force → O(2^n) → O(n) 🔸 DP Memoization → O(n² × k) → O(nk) ⭐ Binary Search + Greedy → O(n log(sum)) → O(1) Big takeaway from today: Sometimes the best solution comes from changing the way we search for the answer. #LeetCode #100DaysOfLeetCode #DSA #Java #ProblemSolving #Algorithms #CodingInterview #SoftwareDevelopment
To view or add a comment, sign in
-
-
𝐃𝐚𝐲 𝟏𝟑𝟎/𝟑𝟔𝟓 🚀 📌 𝐋𝐞𝐞𝐭𝐂𝐨𝐝𝐞 𝐏𝐎𝐓𝐃: 𝐋𝐨𝐧𝐠𝐞𝐬𝐭 𝐂𝐨𝐦𝐦𝐨𝐧 𝐒𝐮𝐟𝐟𝐢𝐱 𝐐𝐮𝐞𝐫𝐢𝐞𝐬 🔎 𝐀𝐩𝐩𝐫𝐨𝐚𝐜𝐡: • Build a Trie using reversed words from wordsContainer • Each Trie node stores: - shortest word length - corresponding smallest index • Traverse query strings in reverse order • Follow longest matching suffix path in Trie • Return best matching index stored at deepest valid node 🔍 𝐀𝐥𝐠𝐨𝐫𝐢𝐭𝐡𝐦: • Trie + String Processing ⏱ 𝐓𝐢𝐦𝐞 𝐂𝐨𝐦𝐩𝐥𝐞𝐱𝐢𝐭𝐲: • O(total characters in wordsContainer + wordsQuery) 🧠 𝐒𝐩𝐚𝐜𝐞 𝐂𝐨𝐦𝐩𝐥𝐞𝐱𝐢𝐭𝐲: • O(total characters in Trie) 📈 𝐊𝐞𝐲 𝐓𝐚𝐤𝐞𝐚𝐰𝐚𝐲: • Reversing strings transforms suffix problems into prefix matching problems 🔗 Problem Link: https://lnkd.in/dXYjF78v #LeetCode #LeetcodePOTD #365DaysOfCode #DSA #Java #Trie #Strings #ProblemSolving
To view or add a comment, sign in
-
-
🚀 Building my own JVM targeted programming language This diagram shows the full journey from writing code --> running it on the Java Virtual Machine. • Lexer → Converts source code into tokens (example: int x = 10 → keywords, identifiers, symbols) • Parser → Takes tokens and builds structure • AST (Abstract Syntax Tree) → A tree representation of the program logic (this is the “brain” of the code structure) • Type Checker → Validates types (optional but powerful) • Optimizer → Improves performance before execution • Bytecode Generator → Converts AST → JVM bytecode (.class file) JVM Execution Flow: • Class Loader → Loads compiled classes • Bytecode Verifier → Ensures safety + correctness • Runtime Data Areas → Heap, Stack, Method Area • Execution Engine → → Interpreter (runs code line by line) → JIT Compiler (converts hot code to native machine code) These components represent the core architecture of a programming language that targets the JVM. 🫡🫡 #ProgrammingLanguages #JVM #CompilerDesign #BuildInPublic #SoftwareEngineering #systemdesing
To view or add a comment, sign in
-
-
Day 91/100 | #100DaysOfDSA 🧩🚀 Today’s problem: Word Break A classic Dynamic Programming problem that strengthens string partitioning intuition. Problem idea: Check if a string can be segmented into valid words from a given dictionary. Key idea: Use DP to track valid prefixes. Why? • Brute force tries all partitions → exponential • DP helps store results of subproblems • If a prefix is valid, we build on it How it works: • Create a DP array dp[i] → can substring [0..i-1] be formed? • Initialize dp[0] = true (empty string is valid) • For each index i, check all possible splits j < i • If dp[j] == true and substring (j → i) exists in dictionary → mark dp[i] = true Optimization used: • HashSet for O(1) lookup • Limit substring length using max word length Time Complexity: O(n²) Space Complexity: O(n) Big takeaway: Whenever you see string segmentation / partition problems, think DP on prefixes. 🔥 This pattern is widely used in problems like sentence formation, decoding, etc. Day 91 done. 🚀 #100DaysOfCode #LeetCode #DSA #Algorithms #DynamicProgramming #Strings #DP #Java #CodingJourney #ProblemSolving #InterviewPrep #TechCommunity
To view or add a comment, sign in
-
-
Today’s LeetCode Daily Problem 💻 LeetCode 3093. Longest Common Suffix Queries => Problem Task The problem asks us to find the string from wordsContainer having the longest common suffix with each query string. A brute force approach would compare every query with every container word and calculate suffix matches manually. • N = max wordsContainer.length = 10^4 • M = max wordsQuery.length = 10^4 • L = max string length = 5 * 10^3 Brute force time complexity becomes: O(N * M * L) > 10^8 which is far beyond acceptable limits for the given constraints. => Core Observations 1. Suffix comparison becomes easier if we reverse the strings. 2. Now the suffix matching problem transforms into a prefix matching problem, and the best data structure for efficient prefix matching is Trie / Prefix Tree. => Core Intuition 1. We insert all wordsContainer strings into the Trie in reverse order. 2. Every Trie node stores: • wordLen: smallest length of the word passing through that node • wordIdx: index of the smallest length word passing through that node. If lengths are the same, store the smaller index 3. Reaching deeper in the Trie means a longer common suffix. 4. And among all words sharing that suffix, we must return: • the smallest length word index • if lengths are the same, return the smaller index 5. So every Trie node itself stores the best possible answer for that suffix. This removes the need for any extra searching during query processing. => Edge Case If the query string does not match with any word in wordsContainer, then its longest common suffix becomes: "" (empty string) In this case: • return the index of the word with the smallest length • if multiple words have the same smallest length, return the smaller index => Complexity • Time complexity: O(N * L + M * L) • Space complexity: O(N * L) => My Detailed Solution with Dry Run + Visualization + Intuitive and Clean Code: https://lnkd.in/d75ZxJie Feel free to share your approach in the comments. #leetcode #leetcodedailycodingchallenge #java #dsa #programming #coding #softwareengineering #competitiveprogramming #algorithms #computerscience
To view or add a comment, sign in
-
-
Grinding DSA (Day 96) Problem: 153 - Find Minimum in Rotated Sorted Array ✅ Solved a classic binary search problem on rotated arrays. The challenge was to efficiently locate the minimum element without scanning the entire array. It was interesting to see how comparing mid and right pointers helps identify the unsorted half and narrow the search space in logarithmic time. #LeetCode #DSA #BinarySearch #Arrays #ProblemSolving #CodingJourney #Java
To view or add a comment, sign in
-