Day 10 Problem Statement: The goal is to make two strings exactly the same by deleting characters, where deleting a character costs its ASCII value. We want to do this with the minimum total cost. Approach: Start comparing both strings from the beginning. At any position, you have two pointers: i pointing to s1 j pointing to s2 If the characters at these positions are the same, keep them and move both pointers forward (no cost). If the characters are different, you have two choices: Delete the character from s1 (pay its ASCII value and move i) Delete the character from s2 (pay its ASCII value and move j) Choose the option that costs less overall. If one string finishes, you must delete all remaining characters from the other string. Why Dynamic Programming is needed The same (i, j) positions appear again and again during recursion. To avoid recomputing, store the result for each (i, j) in a DP table. dp[i][j] represents: Minimum delete cost to make s1[i…] and s2[j…] equal. #HappyCoding #LeetCodeDaily #Java #DSA #ProblemSolving #Consistency
Min Cost to Make Two Strings Equal
More Relevant Posts
-
--- For Loop in Java The for loop is used when the number of iterations is known in advance. It helps execute a block of code repeatedly in a controlled and readable way. Structure of a for loop: Initialization → Condition → Update Example: public class Main { public static void main(String[] args) { for (int i = 1; i <= 5; i++) { System.out.println(i); } } } This loop prints numbers from 1 to 5 by initializing a counter, checking the condition, and updating the counter after each iteration. Why for loops are important: • Useful for iterating over arrays and strings • Commonly used in DSA problems • Helps write concise and readable code Understanding loops is essential for problem-solving and building logical thinking in programming. #Java #ForLoop #DSA #ProgrammingBasics #ControlFlow #SoftwareEngineering
To view or add a comment, sign in
-
Day 10 / #100DaysOfCode LeetCode 712 — Minimum ASCII Delete Sum for Two Strings (Medium) Problem: Given two strings, delete characters from either string so that both become equal, while minimizing the sum of ASCII values of deleted characters. Approach: - Used Dynamic Programming where dp[i][j] represents the minimum delete sum to make substrings s1[i:] and s2[j:] equal - If characters match, move both pointers forward with no cost - Otherwise, delete one character and take the minimum cost between the two choices Key insight: This is a variation of LCS-style DP, but instead of maximizing length, we minimize the deletion cost. #LeetCode #DSA #Java
To view or add a comment, sign in
-
-
After finishing Arrays & ArrayLists, I revisited some core Java basics that I had already used everywhere — but hadn’t fully thought through. This revision focused on literals, operators, comments, and increment/decrement behavior. What became clearer this time: - literals are the actual constant values stored in memory, not just numbers in code - underscores in numeric literals exist for readability, not logic - operators don’t just “do operations” — they define how expressions are evaluated - the number of operands changes how an operator behaves - comments improve readability but are completely ignored by the compiler - pre-increment and post-increment can change program flow in subtle ways - a small change in expression order can change the final output Revisiting increment and decrement operators especially made me realize how easy it is to assume behavior instead of tracing execution step by step. This felt less like learning something new and more like removing hidden confusion from things I was already using daily. Sometimes going back to basics after solving harder problems makes everything cleaner. #Java #CoreConcepts #LearningInPublic #Programming #CodingJourney #JavaDeveloper #ProblemSolving
To view or add a comment, sign in
-
💡 𝗝𝗮𝘃𝗮/𝐒𝐩𝐫𝐢𝐧𝐠 𝐁𝐨𝐨𝐭 𝐏𝐞𝐫𝐟𝐨𝐫𝐦𝐚𝐧𝐜𝐞 𝐓𝐢𝐩 🔥 💎 𝗣𝗿𝗲𝗳𝗲𝗿 𝗦𝘁𝗿𝗶𝗻𝗴𝗕𝘂𝗶𝗹𝗱𝗲𝗿 𝗢𝘃𝗲𝗿 𝗦𝘁𝗿𝗶𝗻𝗴 𝗖𝗼𝗻𝗰𝗮𝘁𝗲𝗻𝗮𝘁𝗶𝗼𝗻 𝗶𝗻 𝗟𝗼𝗼𝗽𝘀 🐌 𝗧𝗵𝗲 𝗣𝗿𝗼𝗯𝗹𝗲𝗺 𝘄𝗶𝘁𝗵 𝗦𝘁𝗿𝗶𝗻𝗴 𝗖𝗼𝗻𝗰𝗮𝘁𝗲𝗻𝗮𝘁𝗶𝗼𝗻 Strings are immutable in Java, so the '+' operator creates a new String object with every concatenation. In loops, this creates thousands of temporary objects that need to be garbage collected. This dramatically impacts both performance and memory usage. 🔥 𝗪𝗵𝘆 𝗦𝘁𝗿𝗶𝗻𝗴𝗕𝘂𝗶𝗹𝗱𝗲𝗿 𝗶𝘀 𝗕𝗲𝘁𝘁𝗲𝗿 StringBuilder uses a mutable character buffer and modifies it in place without creating new objects. In benchmarks with 10,000 iterations, StringBuilder completes in ~4ms while '+' operator takes ~400ms. That's 100x faster with significantly lower memory allocation. ✅ 𝗪𝗵𝗲𝗻 𝘁𝗼 𝗨𝘀𝗲 𝗘𝗮𝗰𝗵 ◾ Use StringBuilder for loops and multiple concatenations. ◾ Use '+' for simple, single-line string building (2-3 strings). ◾ The compiler optimizes simple '+' usage, but not in loops. #java #springboot #programming #softwareengineering #softwaredevelopment
To view or add a comment, sign in
-
-
📅 Day 64 of #100DaysOfLeetCode 🧮 Problem: 712. Minimum ASCII Delete Sum for Two Strings Difficulty: Medium 🧠 Key Idea The problem is a Dynamic Programming on strings variant, similar to Edit Distance. Instead of minimizing operations, we minimize the sum of ASCII values of deleted characters to make both strings equal. We compare prefixes of both strings and decide whether to: keep matching characters, or delete a character from either string with minimum cost. ✅ Approach (Bottom-Up DP / Tabulation) State Definition: dp[i][j] → Minimum ASCII delete sum to make s1[0…i-1] and s2[0…j-1] equal. 🔹 Step 1: Base Cases If s2 is empty → delete all characters of s1 If s1 is empty → delete all characters of s2 🔹 Step 2: DP Transition If characters match → no deletion needed Else → choose the cheaper delete: delete from s1 delete from s2 🔹 Final Answer Stored in dp[n][m] ⏱ Complexity Time: O(n × m) Space: O(n × m) #LeetCode #Java #ProblemSolving #CodingChallenge #100DaysOfCode #DSA #LearningEveryday
To view or add a comment, sign in
-
-
Abstract Class vs Interface : 🔹 Abstract Class ✔ Allows partial implementation ✔ Can contain fields & methods ✔ Supports single inheritance ✔ Best for closely related classes 🔹 Interface ✔ Defines only method contracts ✔ No implementation logic ✔ Supports multiple inheritance ✔ Best for defining capabilities #AbstractClass #Interface #OOPConcepts #Java #CSharp #DotNet #Programming #CodingInterview #SoftwareDevelopment #LearnToCode #TechExplained
To view or add a comment, sign in
-
-
Started learning Strings in Java today. After working with arrays and matrices, this felt like moving into how real programs actually handle text and user input. The focus was on understanding how strings are created, how input is taken, and how basic built-in methods like charAt() and length() work internally. String s = "Pratim"; System.out.println(s.charAt(0)); // Output : P System.out.println(s.length()); // Output : 6 What became clear today: - Strings are not primitive values; they behave like objects with built-in functionality - Accessing characters requires methods like charAt() instead of direct indexing - length() is a method call, not a property like in arrays - Even simple text handling introduces a different way of thinking compared to numbers This felt like the starting point of working with real-world data, where most problems involve text rather than just numbers. Beginning the Strings module today. #Java #DSA #Strings #LearningInPublic #ProblemSolving #CodingJourney #Programming #JavaDeveloper #DeveloperJourney
To view or add a comment, sign in
-
Day 4/25 – LeetCode Challenge 🚀 🔸Problem: Max Consecutive Ones 🔸Difficulty: Easy 🔸Topic: Arrays, Sliding Window 🔸Language: Java Approach 🛠️: ▫️Traverse the binary array once while tracking the current streak of 1s. ▫️Increment the count when a 1 is found and reset it when a 0 appears. ▫️Maintain a variable to store the maximum streak seen so far. ▫️Handle edge cases like single-element arrays explicitly. ▫️This linear scan ensures optimal performance with minimal overhead. Key Learnings 📚: 🔹Efficient use of sliding window/counting techniques 🔹Importance of maintaining running state in array traversal 🔹Handling edge cases to avoid incorrect results 🔹Achieving O(n) time complexity with constant space #25DaysOfLeetCode #LeetCode #DSA #Java #ProblemSolving #Coding #Consistency
To view or add a comment, sign in
-
-
💡 𝗝𝗮𝘃𝗮/𝐒𝐩𝐫𝐢𝐧𝐠 𝐁𝐨𝐨𝐭 𝐂𝐥𝐞𝐚𝐧 𝐂𝐨𝐝𝐞 𝐓𝐢𝐩 🔥 💎𝗪𝗵𝗮𝘁 𝗶𝘀 𝗣𝗮𝘁𝘁𝗲𝗿𝗻 𝗠𝗮𝘁𝗰𝗵𝗶𝗻𝗴 𝗳𝗼𝗿 𝗶𝗻𝘀𝘁𝗮𝗻𝗰𝗲𝗼𝗳 Introduced in Java 16 and finalized in Java 17, Pattern Matching for instanceof allows you to test object types and access their properties without verbose type checks and manual casting. It transforms repetitive instanceof-cast chains into clean, declarative expressions that clearly communicate your intent. ✅ 𝗞𝗲𝘆 𝗔𝗱𝘃𝗮𝗻𝘁𝗮𝗴𝗲𝘀 ◾ 𝗖𝗼𝗻𝗰𝗶𝘀𝗲𝗻𝗲𝘀𝘀: Combines type check, casting, and property access into a single readable expression. ◾ 𝗘𝘅𝗽𝗿𝗲𝘀𝘀𝗶𝘃𝗲𝗻𝗲𝘀𝘀: Enables direct method calls and reduces boilerplate code significantly. ◾ 𝗧𝘆𝗽𝗲 𝗦𝗮𝗳𝗲𝘁𝘆: Compile-time validation prevents casting errors and ensures type correctness. 🤔 Which one do you prefer? #java #springboot #programming #softwareengineering #softwaredevelopment
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