Today’s focus moved beyond built-in functions into writing custom reverse logic on a part of a string. Instead of reversing the entire text directly, the idea was to control specific index ranges and apply logic manually. This felt closer to real problem-solving than simple method usage. What became clearer during this step: - Reversing characters manually builds stronger control over indices and loops - StringBuilder allows in-place modification, which keeps the logic efficient - Breaking a problem into start index, end index, and swaps makes complex operations manageable A small example of reversing only the second half: StringBuilder sb = new StringBuilder("abcdef"); int mid = sb.length() / 2; reverse(sb, mid, sb.length() - 1); // reverse second half System.out.println(sb); public static void reverse(StringBuilder sb, int i, int j) { while (i < j) { char t = sb.charAt(i); sb.setCharAt(i, sb.charAt(j)); sb.setCharAt(j, t); i++; j--; } } Output : abcfed The biggest realization here: - Real algorithmic thinking starts when we control the process instead of relying on ready-made methods - Even a simple reverse can teach index handling, swapping logic, and boundary control This step felt like a shift from basic string handling toward actual problem-solving mindset. #java #stringbuilder #algorithms #problemSolving #codingjourney #learninginpublic
Reversing Strings with StringBuilder in Java
More Relevant Posts
-
Today’s focus shifted toward character-level manipulation using StringBuilder. Instead of only using built-in methods, the goal was to understand how characters can be read, transformed, and written back using logic. What became clearer during practice: - Every character has an ASCII value, which allows manual case conversion - StringBuilder makes it possible to update characters directly without creating new strings - Loop-based transformations help in building real problem-solving thinking, not just method usage One small transformation example: StringBuilder sb = new StringBuilder("PraTiM007"); for (int i = 0; i < sb.length(); i++) { char ch = sb.charAt(i); if (ch >= 'A' && ch <= 'Z') ch = (char)(ch + 32); // convert to lowercase else if (ch >= 'a' && ch <= 'z') ch = (char)(ch - 32); // convert to uppercase sb.setCharAt(i, ch); } System.out.println(sb); Output : pRAtIm007 The biggest realization here: - Real learning starts when logic replaces built-in shortcuts - Small character problems quietly build strong algorithmic thinking Not perfect yet, but the control over strings now feels much more real than before. #java #stringbuilder #problemSolving #codingjourney #learninginpublic #softwaredevelopment
To view or add a comment, sign in
-
𝗗𝗮𝘆 𝟰𝟴/𝟭𝟬𝟬 | 𝗖𝗼𝗻𝘃𝗲𝗿𝘁 𝗕𝗶𝗻𝗮𝗿𝘆 𝗡𝘂𝗺𝗯𝗲𝗿 𝗶𝗻 𝗔 𝗟𝗶𝗻𝗸𝗲𝗱 𝗟𝗶𝘀𝘁 𝘁𝗼 𝗜𝗻𝘁𝗲𝗴𝗲𝗿 Day 48 ✅ — Binary logic meets linked list traversal. 𝗧𝗼𝗱𝗮𝘆'𝘀 𝗣𝗿𝗼𝗯𝗹𝗲𝗺: ✅ 𝗣𝗿𝗼𝗯𝗹𝗲𝗺 #𝟭𝟮𝟵𝟬: Convert Binary Number in a Linked List to Integer (Easy) From LeetCode 𝗪𝗵𝗮𝘁 𝗖𝗹𝗶𝗰𝗸𝗲𝗱: Each node represents a binary digit (0 or 1). The head is the most significant bit. So this isn’t just a linked list problem — it’s a 𝗯𝗶𝗻𝗮𝗿𝘆 𝘁𝗼 𝗱𝗲𝗰𝗶𝗺𝗮𝗹 𝗰𝗼𝗻𝘃𝗲𝗿𝘀𝗶𝗼𝗻 problem wrapped inside linked list traversal. Two key steps: 1️⃣ Compute the length of the list 2️⃣ Traverse again and apply positional binary weights (2^(length-1)) If a node contains 1, add 2^(position) to the result. Classic bit-weight logic. 𝗠𝘆 𝗔𝗽𝗽𝗿𝗼𝗮𝗰𝗵: 👉 First pass: calculate linked list length 👉 Second pass: • If node value is 1 → add 2^(length-1) • Decrement length 👉 Handle last node separately Time Complexity: O(n) Space Complexity: O(1) Two traversals, constant space, clean logic. 𝗠𝘆 𝗥𝗲𝗮𝗹𝗶𝘇𝗮𝘁𝗶𝗼𝗻: This problem reinforces something important: Linked list questions aren’t always about pointer manipulation. Sometimes they test whether you can layer fundamental concepts (like binary math) on top of traversal mechanics. The more I practice, the clearer patterns become: Traversal is routine. The real thinking is in identifying the hidden concept. 𝗖𝗼𝗱𝗲:🔗 https://lnkd.in/g9TyHZGk 𝗗𝗮𝘆 𝟰𝟴/𝟭𝟬𝟬 ✅ | 𝟱𝟮 𝗺𝗼𝗿𝗲 𝘁𝗼 𝗴𝗼! #100DaysOfCode #LeetCode #LinkedList #Binary #DataStructures #CodingInterview #SoftwareEngineer #Java #Algorithms #TimeComplexity #Programming
To view or add a comment, sign in
-
Regular Expression Matching 🔗 Problem Link LeetCode – Regular Expression Matching ⸻ 📌 Problem Statement Implement regular expression matching with support for: • . → Matches any single character • * → Matches zero or more of the preceding element The matching should cover the entire input string, not partial. ⸻ 🎯 Objective Given two strings: • s → Input string • p → Pattern Return true if s matches pattern p, otherwise return false. ⸻ 🧠 Approach Used 1️⃣ Dynamic Programming (DP) We use a 2D boolean table: • dp[i][j] represents → Whether first i characters of string s match → First j characters of pattern p. ⸻ 2️⃣ Base Case • dp[0][0] = true → Empty string matches empty pattern. • Handle patterns like a*, a*b*, etc. Because * can represent zero occurrences. ⸻ 3️⃣ Pattern Matching Logic Case 1: Direct Match or . If: • Current characters are equal OR • Pattern character is . Then: • Value depends on previous diagonal state → dp[i][j] = dp[i-1][j-1] ⸻ Case 2: * Character When pattern character is *, two possibilities exist: 1. Zero occurrences Ignore previous character and * → Move two steps back in pattern 2. One or more occurrences If previous pattern character matches current string character → Stay in same pattern position → Move string pointer This is the key idea of the problem. ⸻ ⏱ Time Complexity • O(m × n) Where: • m = length of string s • n = length of pattern p ⸻ 💾 Space Complexity • O(m × n) Due to 2D DP table. ⸻ 🧩 Key Concepts Covered • Dynamic Programming • 2D DP Table • Pattern Matching • Edge Case Handling • String Manipulation ⸻ 📈 Result • Accepted on LeetCode • All test cases passed • Efficient solution using DP #java #dsa #javaprogram #array #leetcode #solution #data #structure #algorithm #javadeveloper
To view or add a comment, sign in
-
-
𝗗𝗮𝘆 𝟲𝟲/𝟭𝟬𝟬 — 𝗘𝘃𝗮𝗹𝘂𝗮𝘁𝗲 𝗥𝗲𝘃𝗲𝗿𝘀𝗲 𝗣𝗼𝗹𝗶𝘀𝗵 𝗡𝗼𝘁𝗮𝘁𝗶𝗼𝗻 Day 66. Two-thirds done. Today's problem? The reason calculators exist. 𝗧𝗼𝗱𝗮𝘆'𝘀 𝗣𝗿𝗼𝗯𝗹𝗲𝗺: ✅ #𝟭𝟱𝟬: Evaluate Reverse Polish Notation (Medium) 𝗧𝗵𝗲 𝗣𝗿𝗼𝗯𝗹𝗲𝗺: Evaluate expressions in Reverse Polish Notation (RPN). Numbers come first, operators after. Example: ["2","1","+","3","*"] → ((2+1)*3) = 9 No parentheses needed. No order of operations confusion. Just read left to right. 𝗧𝗵𝗲 𝗦𝗼𝗹𝘂𝘁𝗶𝗼𝗻: Stack. When you see a number, push it. When you see an operator, pop two numbers, apply the operation, push the result. The last number standing? That's your answer. Used ArrayList as a stack for cleaner syntax with removeLast(). Same LIFO behavior. 𝗪𝗵𝘆 𝗜𝘁 𝗠𝗮𝘁𝘁𝗲𝗿𝘀: This is how calculators and compilers evaluate expressions internally. RPN eliminates ambiguity. "3 + 4 * 5" needs rules. "3 4 5 * +" doesn't. Understanding this makes you understand how expression parsing works. 𝗖𝗼𝗱𝗲: https://lnkd.in/gXwcqVdP 𝗗𝗮𝘆 𝟲𝟲/𝟭𝟬𝟬 ✅ 𝟲𝟲 𝗱𝗼𝘄𝗻. 𝟯𝟰 𝘁𝗼 𝗴𝗼. #100DaysOfCode #LeetCode #Stack #RPN #Algorithms #ExpressionEvaluation #CodingInterview #Programming #Java #MediumLevel #CompilerDesign
To view or add a comment, sign in
-
Today’s focus was on solving a real logical problem using strings — checking whether multiple words are anagrams. This felt different from earlier steps because it required combining string handling, sorting, and comparison logic instead of only modifying characters. What became clearer during this problem: - Two strings are anagrams when their sorted character sequences become identical - Converting a string to a character array and sorting it makes comparison straightforward - Logical validation across multiple strings builds real problem-solving confidence, not just syntax familiarity A small example used for practice: String[] arr = { "listen", "silent", "enlist" }; String base = sort(arr[0]); boolean ok = true; for (String s : arr) { if (!sort(s).equals(base)) { ok = false; break; } } System.out.println("All anagrams: " + ok); public static String sort(String s) { char[] a = s.toCharArray(); Arrays.sort(a); return new String(a); } Output : All anagrams: true Why this step matters: - It connects string concepts with DSA-style thinking - Shows movement from basic manipulation → logical validation problems - Feels like the point where strings start behaving like a real problem-solving topic, not just syntax #java #strings #anagram #problemSolving #codingjourney #learninginpublic
To view or add a comment, sign in
-
🚀 Day 17 — Valid Parentheses Continuing the Stack pattern. 🧩 Problem solved: Valid Parentheses 💻 Platform: LeetCode (#20) Given a string containing only '(', ')', '{', '}', '[' and ']', determine if the input string is valid. Example: Input: s = "()[]{}" Output: true Input: s = "([)]" Output: false --- 🧠 First Thought (Brute Force) Repeatedly remove adjacent matching pairs like "()", "[]", "{}" until no more removals are possible. If the string becomes empty → valid. But this leads to O(n²) complexity. Clearly inefficient. --- ⚡ Optimal Approach — Stack (via StringBuilder) Key Insight: A closing bracket must always match the most recently seen opening bracket. That’s exactly what a Stack is built for — LIFO. Twist: Instead of Stack<Character>, I used StringBuilder as the stack. Same logic, lighter overhead. Steps: • Iterate through each character • If top of StringBuilder matches current closing bracket → pop (deleteCharAt) • Otherwise → push (append) • If StringBuilder is empty at end → valid Time Complexity: O(n) Space Complexity: O(n) 🔍 What this problem reinforced: ✔ Stack is the go-to structure when order + matching matters ✔ LIFO logic maps perfectly to nested bracket structures ✔ StringBuilder can simulate a stack — not just for string building! This problem made the Stack pattern click in a new way. Solutions are available here: 🔗https://lnkd.in/gW8atfqw Day-17 complete. ✅ #DSA #Java #LeetCode #Stack #ProblemSolving #CodingJourney #LearningInPublic
To view or add a comment, sign in
-
𝗗𝗮𝘆 𝟰𝟳/𝟭𝟬𝟬 | 𝗗𝗲𝗹𝗲𝘁𝗲 𝗡𝗼𝗱𝗲𝘀 𝗙𝗿𝗼𝗺 𝗟𝗶𝗻𝗸𝗲𝗱 𝗟𝗶𝘀𝘁 𝗣𝗿𝗲𝘀𝗲𝗻𝘁 𝗶𝗻 𝗔𝗿𝗿𝗮𝘆 Day 47 ✅ — Hash set meets linked list. 𝗧𝗼𝗱𝗮𝘆'𝘀 𝗣𝗿𝗼𝗯𝗹𝗲𝗺: ✅ 𝗣𝗿𝗼𝗯𝗹𝗲𝗺 #𝟯𝟮𝟭𝟳: Delete Nodes From Linked List Present in Array (Medium) 𝗪𝗵𝗮𝘁 𝗖𝗹𝗶𝗰𝗸𝗲𝗱: Remove all nodes whose values appear in a given array. Simple concept, but the implementation requires combining two data structures efficiently. The key? Convert array to 𝗛𝗮𝘀𝗵 𝗦𝗲𝘁 for O(1) lookups. Then traverse the linked list with dummy node pattern, checking each node against the set. Eighteen days of linked list practice means the traversal logic is automatic. My focus was purely on optimization—hash set instead of repeated array searching. 𝗠𝘆 𝗔𝗽𝗽𝗿𝗼𝗮𝗰𝗵: 👉 Convert nums array to HashSet 👉 Use dummy node for clean edge case handling 👉 Traverse list with prev and curr pointers 👉 If curr.val in set, skip node 👉 Otherwise, move forward Time: O(n + m), Space: O(m) where m = array size 𝗠𝘆 𝗥𝗲𝗮𝗹𝗶𝘇𝗮𝘁𝗶𝗼𝗻: Eighteen linked list problems. The dummy node pattern has appeared so many times it's muscle memory. When fundamentals are solid, you focus on what actually matters—choosing the right data structure. Hash sets turn O(n×m) solutions into O(n+m). That's the difference between passing and timing out. 𝗖𝗼𝗱𝗲:🔗 https://lnkd.in/gFMUU6bn 𝗗𝗮𝘆 𝟰𝟳/𝟭𝟬𝟬 ✅ | 𝟱𝟯 𝗺𝗼𝗿𝗲 𝘁𝗼 𝗴𝗼! #100DaysOfCode #LeetCode #LinkedList #HashSet #DataStructures #CodingInterview #SoftwareEngineer #Java #Algorithms #TimeComplexity #Programming #Optimization
To view or add a comment, sign in
-
While practicing Quick Sort further, I looked into the issue of pivot selection. In the basic implementation, choosing a fixed pivot (like the first element) can sometimes lead to very unbalanced partitions, especially if the array is already sorted or nearly sorted. In such cases, the time complexity can degrade to O(n²). Approach used : Instead of always choosing the same pivot position, the pivot can be selected from a different position (like the middle element or a random index) before performing the partition. The rest of the algorithm remains the same : - choose a pivot - move it to its correct position using partition logic - recursively sort the left and right parts Core Quick Sort logic : static void quickSort(int[] arr, int lo, int hi) { if (lo >= hi) return; int idx = partition(arr, lo, hi); quickSort(arr, lo, idx - 1); quickSort(arr, idx + 1, hi); } Things that became clear : - the pivot choice strongly affects performance - randomized or varied pivot selection helps avoid worst-case patterns - average time complexity remains O(n log n) - recursion stack space is typically O(log n) This made it clear that Quick Sort’s efficiency depends not only on the algorithm itself but also on how intelligently the pivot is chosen. #dsa #algorithms #quicksort #sorting #java #learninginpublic
To view or add a comment, sign in
-
🚀 Day 23 – LeetCode 1208 | Get Equal Substrings Within Budget Today’s problem focused on optimizing substring transformations under a cost constraint. 🧠 Problem Given two strings "s" and "t", each character change has a cost defined by the ASCII difference. Find the maximum length substring that can be converted within a given budget. 🔍 Key Insight Instead of brute force (O(n²)), convert the problem into: → “Longest subarray with sum ≤ k” This immediately maps to the Sliding Window (Two Pointer) technique. ⚙️ Approach • Calculate cost = |s[i] − t[i]| • Expand window → add cost • If budget exceeded → shrink window • Track max length ⏱ Complexity Time: O(n) Space: O(1) 💻 Code (Java) class Solution { public int equalSubstring(String s, String t, int maxCost) { int left = 0, currentCost = 0, maxLen = 0; for (int right = 0; right < s.length(); right++) { currentCost += Math.abs(s.charAt(right) - t.charAt(right)); while (currentCost > maxCost) { currentCost -= Math.abs(s.charAt(left) - t.charAt(left)); left++; } maxLen = Math.max(maxLen, right - left + 1); } return maxLen; } } 📌 Takeaway Whenever you see: • longest substring • contiguous • sum constraint Think → Sliding Window Pattern recognition > memorizing solutions. #LeetCode #DSA #SlidingWindow #Java #ProblemSolving #CodingInterview #SoftwareEngineering #100DaysOfCode #Developers #TechCareer
To view or add a comment, sign in
-
𝐒𝐨𝐥𝐯𝐞𝐝: 𝐄𝐯𝐚𝐥𝐮𝐚𝐭𝐞 𝐑𝐞𝐯𝐞𝐫𝐬𝐞 𝐏𝐨𝐥𝐢𝐬𝐡 𝐍𝐨𝐭𝐚𝐭𝐢𝐨𝐧 (𝐋𝐞𝐞𝐭𝐂𝐨𝐝𝐞 #𝟏𝟓𝟎) Today I solved the 𝐄𝐯𝐚𝐥𝐮𝐚𝐭𝐞 𝐑𝐞𝐯𝐞𝐫𝐬𝐞 𝐏𝐨𝐥𝐢𝐬𝐡 𝐍𝐨𝐭𝐚𝐭𝐢𝐨𝐧 problem, a classic stack-based question that strengthens understanding of data structures and expression evaluation. 𝐏𝐫𝐨𝐛𝐥𝐞𝐦 𝐒𝐮𝐦𝐦𝐚𝐫𝐲: Given an array of strings representing an arithmetic expression in Reverse Polish Notation (RPN), evaluate and return the result. ✔ Operators: +, -, *, / ✔ Division truncates toward zero ✔ Valid RPN expression (no division by zero) 𝐊𝐞𝐲 𝐂𝐨𝐧𝐜𝐞𝐩𝐭: Stack Data Structure Why Stack? Because RPN works on the principle of: Push operands onto the stack When an operator appears → pop last two operands → apply operation → push result back This follows LIFO (Last In, First Out) behavior perfectly. 𝐀𝐩𝐩𝐫𝐨𝐚𝐜𝐡: Traverse each token. If it’s a number → push to stack. If it’s an operator: Pop top two elements Perform operation Push result back Final remaining value in stack = answer Time Complexity: O(n) Space Complexity: O(n) Example: Input → ["2","1","+","3","*"] Output → 9 Explanation → ((2 + 1) * 3) #LeetCode #DataStructures #Java #CodingInterview #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