💡 LeetCode 3110 – Score of a String 💡 Today, I solved LeetCode Problem #3110: Score of a String, a neat little challenge that emphasizes understanding of ASCII values and absolute differences in Java strings. 🔠✨ 🧩 Problem Overview: You’re given a string s. The score of the string is defined as the sum of the absolute differences between the ASCII values of consecutive characters. Return the total score. 👉 Example: Input → "hello" Output → 13 Explanation → |'e'-'h'| + |'l'-'e'| + |'l'-'l'| + |'o'-'l'| = 3 + 7 + 0 + 3 = 13 💡 Approach: 1️⃣ Initialize a variable sum to store the total score. 2️⃣ Traverse the string from index 1 to end. 3️⃣ For each pair of consecutive characters, find their ASCII difference using Math.abs(). 4️⃣ Add the difference to sum and return it. ⚙️ Complexity Analysis: ✅ Time Complexity: O(n) — Single traversal of the string. ✅ Space Complexity: O(1) — Constant extra space. ✨ Key Takeaways: Strengthened understanding of character encoding and ASCII values. Reinforced clean looping logic and mathematical precision in Java. A great example of how simplicity can elegantly solve a real problem. 🌱 Reflection: Even the smallest coding challenges help sharpen attention to detail — from understanding data types to leveraging built-in functions effectively. Consistency is what transforms learning into mastery. 🚀 #LeetCode #3110 #Java #StringManipulation #ASCII #ProblemSolving #DSA #CodingJourney #CleanCode #AlgorithmicThinking #ConsistencyIsKey
Solved LeetCode 3110: Score of a String in Java
More Relevant Posts
-
💡 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
-
-
💡 LeetCode 1528 – Shuffle String 💡 Today, I solved LeetCode Problem #1528: Shuffle String, which tests how well you can handle index mapping and string reconstruction in Java — an elegant exercise in array manipulation and string handling. ✨ 🧩 Problem Overview: You’re given a string s and an integer array indices. Each character at position i in s must be moved to position indices[i] in the new string. Return the final shuffled string. 👉 Example: Input → s = "codeleet", indices = [4,5,6,7,0,2,1,3] Output → "leetcode" 💡 Approach: 1️⃣ Create a char array of the same length as s. 2️⃣ Loop through each character in s, placing it at the position specified by indices[i]. 3️⃣ Convert the filled character array back to a string. ⚙️ Complexity Analysis: ✅ Time Complexity: O(n) — Single pass through the string. ✅ Space Complexity: O(n) — For the result character array. ✨ Key Takeaways: Reinforced understanding of index mapping between arrays and strings. Practiced how to reconstruct a string efficiently without using extra data structures. A great example of how simple iteration can solve structured reordering problems cleanly. 🌱 Reflection: Sometimes, challenges like this remind us that not every problem requires a complex algorithm — precision, clarity, and clean mapping logic often do the job best. Staying consistent with such problems builds both confidence and coding discipline. 🚀 #LeetCode #1528 #Java #StringManipulation #ArrayMapping #DSA #ProblemSolving #CleanCode #CodingJourney #AlgorithmicThinking #ConsistencyIsKey
To view or add a comment, sign in
-
-
Does Language Matter in DSA? When I first started doing DSA, I saw the usual noise — “Use C++ for performance.” “Python is faster to code in.” “Java is too verbose.” After spending over a year grinding LeetCode and building real-world software in both Java and JavaScript/TypeScript, I can confidently say: the language barely matters once you understand the logic. Yes, certain languages have built-in conveniences — STL in C++, functional tools in Python, or strong typing in Java — but these only affect how fast you write the solution, not whether you can think of one. When I was at peak consistency (my LeetCode streak’s still alive past 390 days), I realized the bottleneck wasn’t syntax or runtime speed — it was pattern recognition and clarity. If you can break a problem down, reason through edge cases, and know the underlying data structures, the language becomes just a medium of expression. For me, Java taught discipline and structure. JavaScript taught speed and freedom. But DSA itself taught me how to think. So, no — language doesn’t decide your DSA skill. Your ability to visualize, simplify, and optimize logic does. Pick the language you’re most fluent in and focus on mastering patterns, not switching compilers. The goal isn’t to impress the judge’s runtime — it’s to make your brain faster than the IDE. #DSA #ProblemSolving #SoftwareEngineering #ProgrammingMindset #JavaDevelopers #CleanCode #JavaScript
To view or add a comment, sign in
-
-
💻 Day 4 of #100DaysOfCode Challenge Topic: Binary Tree Traversals 🌳 Problems Solved: 🔹 94. Binary Tree Inorder Traversal 🔹 144. Binary Tree Preorder Traversal 🔹 145. Binary Tree Postorder Traversal Concept Recap: Today, I explored the three fundamental depth-first traversal techniques used in binary trees: ✅ Inorder (Left → Root → Right) – Produces a sorted order for BSTs. ✅ Preorder (Root → Left → Right) – Useful for creating a copy of the tree or serialization. ✅ Postorder (Left → Right → Root) – Ideal for deleting trees or evaluating expressions. Each traversal follows a recursive approach to explore nodes systematically. Implementing them helped me strengthen my understanding of recursion and how stack frames manage function calls behind the scenes. Key Learnings: 🧠 Understood how traversal order impacts output sequence. ⚙️ Practiced recursive depth-first traversal logic in Java. 🌱 Improved code readability by modularizing recursive functions. #LeetCode #DataStructures #BinaryTree #Recursion #Java #CodingChallenge #100DaysChallenge #Day4
To view or add a comment, sign in
-
-
💻 Strengthening Problem-Solving Skills with LeetCode (Java) Recently explored a few interesting problems that focused on string manipulation, array traversal, and text formatting — each reinforcing clarity and precision in algorithmic thinking: 🔹 Text Justification Implemented a custom text alignment algorithm that evenly distributes spaces between words to fit a given line width. Approach: Greedy + StringBuilder Time Complexity: O(n) 🔹 Two Sum II – Input Array Is Sorted Solved using a two-pointer technique to efficiently find pairs that add up to a target value in a sorted array. Approach: Two Pointers Time Complexity: O(n) 🔹 Is Subsequence Checked whether one string is a subsequence of another using linear traversal and pointer comparison. Approach: Two Pointers Time Complexity: O(n) 🔹 Valid Palindrome Validated alphanumeric palindromes by filtering characters and comparing from both ends. Approach: String cleaning + two-pointer comparison Time Complexity: O(n) These problems helped sharpen my logic-building process and improved my confidence in designing efficient, readable solutions. #LeetCode #Java #ProblemSolving #DSA #Algorithms #Coding #SoftwareEngineering
To view or add a comment, sign in
-
I solved LeetCode 42: Trapping Rain Water in Java, and it turned out to be one of those problems that looks straightforward at first but hides deep lessons in algorithm design and optimization. The problem asks you to calculate how much water can be trapped between bars of different heights after it rains. It sounds simple, but when you try to implement it, you realize how easily brute-force approaches break down. My initial solution used nested loops to check how much water could be trapped at every index. It worked but ran with O(n²) time complexity, which quickly became inefficient for large inputs. That is when I shifted toward precomputation. The key idea is to figure out how much water each bar can hold by knowing the tallest bar to its left and right. Once that information is known, the amount of trapped water at any position is simply the minimum of those two maximum heights minus the height of the bar itself. To make this efficient, I created two arrays: maxLeft[i] stores the tallest bar to the left of index i. maxRight[i] stores the tallest bar to the right of index i. This approach brought the time complexity down to O(n) while keeping the logic simple and elegant. This problem taught me that performance improvements often come from rethinking the way you process information. Precomputation, caching, and avoiding repetitive operations are not just algorithmic tricks. They are the same concepts that drive efficiency in backend engineering, database optimization, and system design. In many ways, this problem reflects real-world engineering. It is not about how fast you can write code, but how effectively you can anticipate what the system will need before it needs it. That mindset is what separates good developers from great ones. What is one problem that helped you truly understand the balance between simplicity and optimization? #Java #Programming #SoftwareDevelopment #ProblemSolving #Algorithms #Coding #DSA #LeetCode #Engineering #SystemDesign
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 75 of #100DaysOfCodingChallenge ✨ Problem: 560. Subarray Sum Equals K 📚 Category: Array Manipulation | Subarray Problems 💡 Approach: Brute Force 🧠 Language: Java 🔍 Problem Understanding: We’re given an integer array nums and an integer k. We need to count how many subarrays (continuous parts of the array) have a sum equal to k. Example: Input: nums = [1, 2, 3], k = 3 Output: 2 Explanation: [1,2] and [3] are the subarrays with sum 3. 🧠 Brute Force Approach: In the brute-force method, we check every possible subarray using two loops: 1️⃣ Start from each element as a subarray beginning. 2️⃣ Keep adding elements until the end, and check if the sum equals k. 3️⃣ If yes, increment the count. Although it’s not the most optimized, this approach helps build a strong foundation in understanding subarray logic. ⚙️ Complexity: Time Complexity: O(n²) Space Complexity: O(1) 🌱 Learning: This problem taught me how to approach subarray-based questions step-by-step — starting from brute force before moving toward optimized solutions like prefix sums + hashmaps. Building clarity in fundamentals always makes optimization easier later 🚀 #Day75 #100DaysOfCode #LeetCode #Java #ProblemSolving #CodingJourney #WomenInTech #DSA #ArrayManipulation
To view or add a comment, sign in
-
-
💡 LeetCode 1929 – Concatenation of Array 💡 Today, I solved LeetCode Problem #1929: Concatenation of Array, a simple yet satisfying problem that tests your understanding of array manipulation and indexing in Java. ⚙️📊 🧩 Problem Overview: You’re given an integer array nums. Your task is to create a new array ans such that ans = nums + nums (i.e., concatenate the array with itself). 👉 Example: Input → nums = [1,2,1] Output → [1,2,1,1,2,1] 💡 Approach: 1️⃣ Find the length n of the given array. 2️⃣ Create a new array of size 2 * n. 3️⃣ Loop through nums once — place each element both at index i and index n + i. 4️⃣ Return the resulting array. ⚙️ Complexity Analysis: ✅ Time Complexity: O(n) — Single traversal of the array. ✅ Space Complexity: O(n) — For the concatenated array. ✨ Key Takeaways: Practiced index manipulation and array construction. Reinforced the importance of efficient iteration. A great warm-up problem that strengthens logical thinking and array fundamentals. 🌱 Reflection: Even the simplest problems build the foundation for solving more complex ones. Every bit of consistent practice helps in mastering problem-solving and clean coding habits. 🚀 #LeetCode #1929 #Java #ArrayManipulation #DSA #CodingJourney #CleanCode #ProblemSolving #AlgorithmicThinking #ConsistencyIsKey
To view or add a comment, sign in
-
-
#Day 3 of the Coding Challenge: The Missing Number! Today, I cracked a classic array problem: Finding the Missing Number in an array containing n distinct numbers from the range [0, n] My final, optimized solution uses the elegant Gauss Summation method with a crucial twist to avoid a common pitfall! # The Problem & The Solution Input: An array nums of length n (e.g., [3, 0, 1]). The numbers are from 0 to n(e.g., 0, 1, 2, 3). Missing Number: 2. Logic: {Missing Number} = {Expected Sum of } 0 { to } n) - ({Actual Sum of Array Elements}) 👨💻 My Optimized Java Code Java class Solution { public int missingNumber(int[] nums) { int n = nums.length; // CRITICAL FIX: Use 'long' for the expected sum calculation // to prevent Integer Overflow for large 'n'. long expectedSum = (long)n * (n + 1) / 2; long actualSum = 0; for(int num : nums){ actualSum += num; } return (int) (expectedSum - actualSum); } } ? Why the long is Key to Optimization While the O(n) summation method is fast, the intermediate value of frac{n(n+1)}{2}can easily exceed Integer.MAX_VALUE if the array is large (around n=65,536). By casting to long, we make the solution robust and production-ready, eliminating the risk of a bug under large constraints! ❓ Quick Poll: Which O(n) method do you prefer for this problem? Gauss Summation (Like above - Math is power!) Bitwise XOR (The clever one that avoids all addition!) Let me know your vote and why in the comments! 👇 #Day3 #CodingChallenge #LeetCode #Java #Programming #Algorithm #DataStructures #TechJobs #SoftwareDevelopment
To view or add a comment, sign in
-
Explore related topics
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