Day 88 of #100DaysOfCode Solved Decrypt String from Alphabet to Integer Mapping in Java 🔡 Approach Today's challenge involved mapping a string of digits to lowercase English characters, where single digits '1' through '9' map to 'a' through 'i', and two digits followed by a '#' (e.g., '10#', '11#') map to 'j' through 'z'. I used an iterative approach with a while loop and an index i to traverse the input string. I checked ahead to see if a two-digit mapping existed by looking for a '#' at s.charAt(i + 2). If a '#' was found, I parsed the two-digit number (e.g., "10", "11") using s.substring(i, i + 2), converted it to an integer, and then mapped it to the correct character by adding it to the ASCII value of 'a' and subtracting one. I then advanced the index i by 3. If no '#' was found, it meant the current character s.charAt(i) was a single-digit mapping. I converted this digit to an integer and mapped it to a character similarly, then advanced the index i by 1. This approach processes the string from left to right, correctly handling the two-digit encodings first, which resulted in a quick runtime that beat 79.30% of other submissions. #Java #100DaysOfCode #LeetCode #CodingChallenge #Algorithms #StringManipulation #ProblemSolving
Solved Decrypt String Challenge in Java with Iterative Approach
More Relevant Posts
-
Day 90 of #100DaysOfCode Solved Squares of a Sorted Array in Java 🔠 Approach The task was to take an array of integers sorted in non-decreasing order, square each number, and then return the result array also sorted in non-decreasing order. Brute-Force Method The solution implemented here is a straightforward two-step brute-force approach: Squaring: I iterated through the input array nums and replaced each element with its square (i.e., nums[i] * nums[i]). This handles both positive and negative numbers correctly. Sorting: After squaring all elements, I used Java's built-in Arrays.sort(nums) method to sort the entire array. While correct, this approach has a time complexity dominated by the sorting step, which is O(NlogN), where N is the number of elements. The runtime of 10 ms shows that a more efficient, two-pointer approach (which can solve this in O(N) time) is generally preferred for optimal performance. #Java #100DaysOfCode #LeetCode #CodingChallenge #Algorithms #Array #Sorting #ProblemSolving
To view or add a comment, sign in
-
-
💻 DSA – Day 13 : Strings & StringBuilder Today I explored Strings in Java, a core concept used in almost every application — from input handling to algorithms, data processing, and problem-solving. Strings look simple, but there's a lot happening under the hood. 🌈 What I learned today 🔹 ❓ What are Strings? A sequence of characters stored as an object in Java. Strings are immutable, which means once created, they cannot be changed. 🔹 ⌨️ Input & Output Took string inputs using Scanner and printed them with basic operations. 🔹 📏 String Length Used .length() to find the number of characters. 🔹 ➕ String Concatenation Joined strings using +, concat(), and StringBuilder. 🔹 🔡 charAt() Method Accessed characters using index values. 🔹 🔄 Check if a String is Palindrome Compared characters from both ends to verify if the string reads the same backwards. 🔹 🧭 Shortest Path Problem Calculated the shortest path (N/E/W/S directions) using coordinate logic. 🔹 ⚖️ String compare() Compared strings lexicographically using .compareTo(). 🔹 ✂️ substring() Function Extracted specific parts of the string. 🔹 🏆 Print Largest String Compared strings alphabetically to find the largest one. 🔹 🔒 Why Strings Are Immutable? Because they are stored in the String Pool and designed for security, caching, and thread-safety. 🧵 StringBuilder – Faster & Mutable 🔹 Unlike Strings, StringBuilder is mutable 🔹 Perfect for modifying strings repeatedly (loops, concatenation, dynamic strings) ✨ Extra Concepts Learned 🔹 🔤 Convert Letters to Uppercase Converted the first letter of each word to uppercase (title case-style logic). 🔹 📦 String Compression Implemented character frequency compression like: aaabbccc → a3b2c3 🚀 Loving the learning curve Strings are everywhere — mastering them builds a strong foundation for upcoming DSA topics. #DSA #Strings #Java #StringBuilder #CodingJourney #100DaysOfCode #LearningInPublic #ProblemSolving #Day13
To view or add a comment, sign in
-
💡 LeetCode 1768 – Merge Strings Alternately 💡 Today, I solved LeetCode Problem #1768, a delightful string manipulation problem that strengthens the fundamentals of character traversal and string building using StringBuilder in Java. 🧩 Problem Overview: You’re given two strings, word1 and word2. Your task is to merge them alternately — take one character from each string in turn, and if one string is longer, append its remaining characters at the end. 👉 Examples: Input → word1 = "abc", word2 = "pqr" → Output → "apbqcr" Input → word1 = "ab", word2 = "pqrs" → Output → "apbqrs" Input → word1 = "abcd", word2 = "pq" → Output → "apbqcd" 💡 Approach: 1️⃣ Use two pointers i and j to iterate through both strings. 2️⃣ Append one character from word1, then one from word2, alternately. 3️⃣ Continue until both strings are fully traversed. 4️⃣ Return the merged string using StringBuilder.toString(). ⚙️ Complexity Analysis: ✅ Time Complexity: O(n + m) — Each character from both strings is processed once. ✅ Space Complexity: O(n + m) — For the resulting merged string. ✨ Key Takeaways: Reinforced understanding of two-pointer traversal and StringBuilder for efficient string concatenation. Showed how simple looping logic can elegantly handle strings of unequal lengths. Practiced writing clean, readable, and efficient code for basic string problems. 🌱 Reflection: Even simple problems like this one are valuable for sharpening algorithmic thinking and code clarity. Mastering these fundamental string operations lays the groundwork for tackling more advanced text-processing challenges later on. 🚀 #LeetCode #1768 #Java #StringManipulation #TwoPointerTechnique #DSA #ProblemSolving #CleanCode #AlgorithmicThinking #DailyCoding #ConsistencyIsKey
To view or add a comment, sign in
-
-
🔍 Problem Statement: Given an integer array sorted in non-decreasing order, return an array of the squares of each number sorted in non-decreasing order. 🧠 Key Idea: Since the array is already sorted, negative numbers can affect the order after squaring. To handle this efficiently, I used the two-pointer approach — one starting from the beginning and one from the end — comparing squares and filling the result array from the back. ⚙️ Time Complexity: O(n) 🗂️ Space Complexity: O(n) 💬 Code (Java): import java.util.Arrays; class Solution { public int[] sortedSquares(int[] nums) { int n = nums.length; int[] res = new int[n]; int left = 0, right = n - 1, index = n - 1; while (left <= right) { int lsq = nums[left] * nums[left]; int rsq = nums[right] * nums[right]; if (lsq > rsq) { res[index--] = lsq; left++; } else { res[index--] = rsq; right--; } } return res; } } ✨ Learning: Improved my understanding of two-pointer technique. Reinforced the importance of optimizing sorting-related problems. #Java #DSA #Coding #ProblemSolving #StriversSheet #LearningEveryday
To view or add a comment, sign in
-
-
Day 38/100 – #100DaysOfCode 🚀 | #Java #LeetCode #StringParsing #Validation ✅ Problem Solved: Valid Number 🧩 Problem Summary: Given a string, determine whether it represents a valid number. This includes handling cases like: Integers Decimals Scientific notation (e / E) Sign symbols (+ / -) Trailing and leading spaces 💡 Approach Used: This is a string interpretation + state validation problem. I validated the number by checking: Only one decimal point allowed. Only one e or E, and it must split the number into valid components. Sign characters allowed only at the start or just after e/E. At least one digit must appear appropriately. Instead of using regex or parsing libraries, I implemented logical rules step-by-step to ensure strong input handling. ⚙️ Time Complexity: O(n) 📦 Space Complexity: O(1) ✨ Takeaway: Even simple inputs can require careful rule-based parsing — validating strings is more about logic than syntax. #Java #LeetCode #Strings #Parser #StateMachine #ProblemSolving #100DaysOfCode #CodingChallenge
To view or add a comment, sign in
-
-
Day 39 — Ones and Zeroes (0–1 Knapsack Variation) Problem: 474. Ones and Zeroes Difficulty: Medium Language: Java Status: Solved Problem Summary: Given an array of binary strings strs and two integers m and n, find the maximum subset size such that the total number of '0's ≤ m and total '1's ≤ n. Key Concept: This is a 2D 0–1 Knapsack problem, where: Each string represents an item with two “weights”: count of '0's and '1's. We must maximize the number of strings (the “value”) within the constraints of available m and n. Algorithm Steps: Count zeros and ones for each string. Iterate backwards (to avoid overwriting previous states). Update dp[i][j] = max(dp[i][j], dp[i - zeros][j - ones] + 1) for all valid i, j. Time Complexity: O(len(strs) * m * n) Space Complexity: O(m * n) Learning Takeaway: This problem strengthens understanding of multidimensional dynamic programming. The backward iteration pattern is crucial in 0–1 Knapsack-style DP to prevent reuse of the same item. #Day39 #100DaysOfCode #LeetCode #DynamicProgramming #Knapsack #DP #Java #Algorithms #CodingChallenge #ProblemSolving #DSA
To view or add a comment, sign in
-
-
🕒 𝗧𝘄𝗼 𝗠𝗶𝗻𝘂𝘁𝗲 𝗣𝗿𝗲𝗽 — 𝗟𝗮𝗺𝗯𝗱𝗮𝘀 & 𝗙𝘂𝗻𝗰𝘁𝗶𝗼𝗻𝗮𝗹 𝗜𝗻𝘁𝗲𝗿𝗳𝗮𝗰𝗲𝘀 (𝗝𝗮𝘃𝗮) Let’s clear this once and for all 👇 ✨ 𝗪𝗵𝗮𝘁’𝘀 𝘁𝗵𝗲 𝗿𝗲𝗮𝗹 𝗿𝗲𝗹𝗮𝘁𝗶𝗼𝗻 𝗯𝗲𝘁𝘄𝗲𝗲𝗻 𝗟𝗮𝗺𝗯𝗱𝗮 & 𝗙𝘂𝗻𝗰𝘁𝗶𝗼𝗻𝗮𝗹 𝗜𝗻𝘁𝗲𝗿𝗳𝗮𝗰𝗲? A Lambda Expression (→) in Java is nothing but a short-hand implementation of a Functional Interface — an interface having exactly one abstract method. 👉 The Lambda provides the implementation for that single method, removing the boilerplate of anonymous inner classes. 💭 𝗕𝗲𝗳𝗼𝗿𝗲 𝗝𝗮𝘃𝗮 𝟴 — 𝘄𝗲𝗿𝗲 𝗙𝘂𝗻𝗰𝘁𝗶𝗼𝗻𝗮𝗹 𝗜𝗻𝘁𝗲𝗿𝗳𝗮𝗰𝗲𝘀 𝗲𝘃𝗲𝗻 𝘂𝘀𝗲𝗳𝘂𝗹? Absolutely ✅ They existed long before Lambdas — think of Runnable, Callable, or Comparator. 👉 We used them via anonymous inner classes to pass behavior. 👉 Java 8 simply brought syntactic elegance + functional style. 🚫 𝗖𝗮𝗻 𝘄𝗲 𝘂𝘀𝗲 𝗟𝗮𝗺𝗯𝗱𝗮𝘀 𝘄𝗶𝘁𝗵𝗼𝘂𝘁 𝗙𝘂𝗻𝗰𝘁𝗶𝗼𝗻𝗮𝗹 𝗜𝗻𝘁𝗲𝗿𝗳𝗮𝗰𝗲𝘀? No 🙅♂️ Lambdas must target a Functional Interface. They can’t exist independently — the compiler converts them into an instance of that interface behind the scenes. 🧩 𝗜𝗻 𝘀𝗵𝗼𝗿𝘁: 𝗙𝘂𝗻𝗰𝘁𝗶𝗼𝗻𝗮𝗹 𝗜𝗻𝘁𝗲𝗿𝗳𝗮𝗰𝗲 → 𝗗𝗲𝗳𝗶𝗻𝗶𝘁𝗶𝗼𝗻 𝗟𝗮𝗺𝗯𝗱𝗮 𝗘𝘅𝗽𝗿𝗲𝘀𝘀𝗶𝗼𝗻 → 𝗜𝗺𝗽𝗹𝗲𝗺𝗲𝗻𝘁𝗮𝘁𝗶𝗼𝗻 Together, they brought 𝗙𝘂𝗻𝗰𝘁𝗶𝗼𝗻𝗮𝗹 𝗣𝗿𝗼𝗴𝗿𝗮𝗺𝗺𝗶𝗻𝗴 flavor to Java 🚀 💡 𝗙𝗼𝗹𝗹𝗼𝘄-𝘂𝗽 𝗾𝘂𝗲𝘀𝘁𝗶𝗼𝗻𝘀 𝘁𝗼 𝗿𝗲𝗳𝗹𝗲𝗰𝘁 𝗼𝗻: • Why is @FunctionalInterface annotation optional but recommended? • Can a Functional Interface have default or static methods? • What happens if a second abstract method is added accidentally? • How does type inference work with Lambdas? • Difference between Anonymous Class vs Lambda in memory and scope? • How are Lambdas implemented internally (invokedynamic)? Follow Paras Gupta, and do share with others. Happy Learning ❣️ Try to engage with answers or your experience to the relevant questions too. #Java #FunctionalProgramming #LambdaExpressions #JavaInterviewPrep #CodeWisdom #2MinPrep
To view or add a comment, sign in
-
💻 Day 53 of #100DaysOfCode 💻 Today, I solved LeetCode Problem #709 – To Lower Case 🔠 This problem focuses on string manipulation and character encoding (ASCII values) — one of the simplest yet fundamental concepts in text processing. 💡 Key Learnings: Reinforced understanding of ASCII value ranges for uppercase and lowercase alphabets. Practiced efficient string traversal and conditional conversion using StringBuilder in Java. Time complexity: O(n) — iterates through the string once. Space complexity: O(n) — for the output string. 💻 Language: Java ⚡ Runtime: 0 ms — Beats 100% 🚀 📉 Memory: 41.78 MB — Beats 43.48% 🧠 Approach: 1️⃣ Iterate through each character in the string. 2️⃣ Check if it’s an uppercase letter (A–Z). 3️⃣ Convert it to lowercase by adding 32 (based on ASCII values). 4️⃣ Append to the final string using StringBuilder. ✨ Takeaway: Sometimes, understanding the basics like ASCII values can help you write your own efficient methods — without relying on built-in functions! #100DaysOfCode #LeetCode #Java #CodingChallenge #ProblemSolving #Algorithms #StringManipulation #SoftwareEngineering #LearningEveryday #CleanCode
To view or add a comment, sign in
-
-
✅ Day 35 of #100DaysOfCode Challenge 📘 LeetCode Problem 112: Path Sum 🧩 Problem Statement: Given the root of a binary tree and an integer targetSum, return true if there exists a root-to-leaf path whose sum of node values equals targetSum. Example: Input: root = [5,4,8,11,null,13,4,7,2,null,null,null,1], targetSum = 22 Output: true Explanation: Path 5 → 4 → 11 → 2 gives the sum = 22 ✅ 💡 Simple Approach: If the tree is empty → return false If it’s a leaf node → check if its value equals targetSum Otherwise → subtract node value and check left and right recursively 💻 Easiest Java Code: class Solution { public boolean hasPathSum(TreeNode root, int sum) { if (root == null) return false; if (root.left == null && root.right == null && root.val == sum) return true; sum = sum - root.val; return hasPathSum(root.left, sum) || hasPathSum(root.right, sum); } } ⚙ Complexity: ⏱ Time: O(n) → visit each node once 💾 Space: O(h) → recursion stack (h = height of tree) 🌿 Small code, big concept — recursion makes trees easy 🌱 #Day35 #100DaysOfCode #LeetCode #Java #DSA #BinaryTree #Recursion #CodingChallenge
To view or add a comment, sign in
-
-
Day 85 of #100DaysOfCode Solved Range Sum Query - Immutable (NumArray) in Java ➕ Approach Today's problem was a classic that demonstrates the power of pre-computation: finding the sum of a range in an array many times. The optimal solution is the Prefix Sum technique. Pre-computation: In the constructor, I built a prefixSum array where prefixSum[i] holds the sum of all elements from index 0 up to index $i-1$ in the original array. This takes $O(N)$ time. $O(1)$ Query: The magic happens in the sumRange(left, right) method. The sum of any range $[left, right]$ is found instantly by calculating prefixSum[right + 1] - prefixSum[left]. The cost of a single $O(N)$ setup is outweighed by the ability to perform every subsequent query in $O(1)$ time! #Java #100DaysOfCode #LeetCode #CodingChallenge #Algorithms #Arrays #PrefixSum #Optimization #ProblemSolving
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