Today I solved the Unique Binary String problem on LeetCode using two different approaches in Java. • Method 1 (Optimized – Cantor’s Diagonal Approach) Constructed a new binary string by flipping the i-th bit of the i-th string. This guarantees the generated string differs from every string in the array at least at one position. Time Complexity: O(n) Space Complexity: O(n) (for the result string) • Method 2 (Brute / Less Optimized) Converted each binary string to its decimal representation, stored them, and checked for a number whose binary form (length n) is not present in the list. Time Complexity: O(n²) Space Complexity: O(n) Exploring multiple approaches to the same problem helps strengthen algorithmic thinking and optimization skills. #LeetCode #Java #Algorithms #ProblemSolving #CodingPractice #DataStructures
LeetCode Solution: Unique Binary String Problem in Java
More Relevant Posts
-
Day 4 of my DSA Journey 🚀 Today’s challenge: The classic Palindrome Number problem. For today's solution, I focused on an intuitive approach using String manipulation in Java. Sometimes the best way to solve a problem is to break it down into steps that make logical sense right out of the gate! 🧠 My Approach: Handle the edge case: Negative numbers can never be palindromes, so return false immediately. Convert the integer to a String. Use a for loop to iterate backwards through the string, building a new reversed string character by character. Compare the original string with the reversed string to determine if it's a palindrome. ⚡ Key Learnings & Java Gotchas: String Iteration: In Java, strings aren't exactly like arrays, so I had to use .charAt(i) instead of standard bracket notation [i] to grab individual characters. The Comparison Trap: I learned the crucial difference between == and .equals(). In Java, == compares memory locations, but .equals() checks if the actual text values are the same. A huge "aha!" moment for handling Strings! Edge Case Handling: Always check for negative numbers first to save processing time. 📌 Complexity: Time: O(n) — where n is the number of digits, since we loop through the string once. Space: O(n) — for storing the string representations of the number. Every bug is a lesson, and every solved problem is a step forward. On to the next one! 💪 #DSA #DataStructures #Algorithms #ProblemSolving #CodingJourney #Java #TechJourney#DSAJourney #LeetCode #Coding #LearnInPublic #GrowthMindset
To view or add a comment, sign in
-
-
\🚀 Day 15 of My LeetCode Journey 🧩 Problem Solved: 1980. Find Unique Binary String 💻 Language: Java Today I worked on a problem where we are given n unique binary strings, each of length n, and we need to find another binary string of length n that does not exist in the list. 🔍 Key Insight: Instead of checking all possible binary combinations, we can build a new string by flipping the i-th bit of the i-th string. This guarantees the new string will differ from every string in the array at least at one position. ⚡️ Concept Used: Diagonalization technique 📈 Time Complexity: O(n) 📦 Space Complexity: O(n) 💡 This problem reminded me that sometimes the smartest solutions come from simple observations rather than brute force approaches. Consistency is key — solving problems daily to strengthen my Java and problem-solving skills. #LeetCode #Java #CodingJourney #ProblemSolving #100DaysOfCode #DataStructures #LearningInPublic
To view or add a comment, sign in
-
-
🚀 Day 15 of #50DaysLeetCode Challenge Today I solved the “Longest Common Prefix” problem on LeetCode using Java. 🔹 Problem: Find the longest common prefix among an array of strings. If no common prefix exists, return an empty string "". Example: Input: ["flower","flow","flight"] Output: "fl" Input: ["dog","racecar","car"] Output: "" 🔹 Approach I Used: ✔ Took the first string as the initial prefix ✔ Compared it with each string in the array ✔ If mismatch occurs, reduced the prefix step by step ✔ Continued until all strings share the same prefix 🔹 Key Insight: You don’t need complex logic — just keep shrinking the prefix until it matches all strings. 🔹 Concepts Practiced: • String manipulation • Iterative comparison • Edge case handling #LeetCode #DSA #Java #Algorithms #ProblemSolving #CodingChallenge #50DaysOfCode
To view or add a comment, sign in
-
-
🚀 Day 11 of #50DaysLeetCode Challenge Today I tackled a HARD problem: “Regular Expression Matching” on LeetCode using Java. 🔹 Problem: Given a string "s" and a pattern "p", implement regex matching with support for: • "." → matches any single character • "*" → matches zero or more of the preceding element Return true if the entire string matches the pattern. Example: Input: "s = "aa", p = "a*"" → Output: "true" Input: "s = "aa", p = "a"" → Output: "false" 🔹 Approach I Used: I used Dynamic Programming (DP) — anything else here is inefficient or breaks on edge cases. ✔ Created a 2D DP table "dp[m+1][n+1]" ✔ "dp[i][j]" represents whether "s[0...i-1]" matches "p[0...j-1]" ✔ Handled "*" carefully: - Treat it as zero occurrence → "dp[i][j-2]" - Or one/more occurrence → match previous character ✔ Built the solution bottom-up 🔹 Concepts Practiced: • Dynamic Programming (2D DP) • String pattern matching • Handling complex edge cases #LeetCode #DSA #Java #DynamicProgramming #Algorithms #CodingChallenge #50DaysOfCode
To view or add a comment, sign in
-
-
🚀 Tackling algorithmic challenges one substring at a time! Just solved the “Number of Substrings Containing All Three Characters” problem in Java using a sliding window approach. ✨ Key takeaway: Efficient solutions often come from thinking in terms of windows and counts rather than brute force. 💻 Output validated with test cases — clean, optimized, and accepted! #Java #CodingChallenge #ProblemSolving #DataStructures #Algorithms #LeetCode #ProgrammingJourney
To view or add a comment, sign in
-
-
Day 60 of #100DaysOfLeetCode 💻✅ Solved #389. Find the Difference problem in Java. Approach: • Initialized a variable to store the sum • Subtracted ASCII values of all characters in string s • Added ASCII values of all characters in string t • The remaining value represents the extra character • Converted the result back to character and returned it Performance: ✓ Runtime: 3 ms (Beats 38.82% submissions) ✓ Memory: 43.34 MB (Beats 30.69% submissions) Key Learning: ✓ Learned a clever use of ASCII values for problem solving ✓ Practiced using math-based approach instead of extra data structures ✓ Improved understanding of string manipulation techniques Learning one problem every single day 🚀 #Java #LeetCode #DSA #Strings #Math #ProblemSolving #CodingJourney #100DaysOfCode
To view or add a comment, sign in
-
-
🚀 Day 90 of My 100 Days LeetCode Challenge | Java Today’s challenge was a fascinating logic + mathematical insight problem. The task was to generate a binary string that does not exist in a given list of binary strings, where all strings have the same length. Instead of trying every possible combination, the solution becomes much simpler when you use the idea of diagonal flipping. By flipping the i-th bit of the i-th string, we guarantee that the newly created string differs from every string in the list at least in one position. This clever trick ensures the generated string is always unique. ✅ Problem Solved: Find Unique Binary String ✔️ All test cases passed (186/186) ⏱️ Runtime: 0 ms 🧠 Approach: Mathematical Insight (Diagonal Construction) 🧩 Key Learnings: ● Sometimes the smartest solutions come from mathematical reasoning rather than brute force. ● The diagonalization technique guarantees uniqueness. ● Understanding problem constraints can drastically simplify the approach. ● Logical insights can reduce exponential possibilities to linear solutions. ● Elegant algorithms often come from simple observations. This problem was a great reminder that a small logical trick can outperform complex algorithms. 🔥 Day 90 complete — sharpening my problem-solving intuition and algorithmic thinking. #LeetCode #100DaysOfCode #Java #Math #Algorithms #ProblemSolving #DSA #CodingJourney #Consistency
To view or add a comment, sign in
-
-
Day 21 of #100DaysOfCode Today, I worked on the classic string problem: implementing the strStr() function in Java. The goal: Find the first occurrence of a substring (needle) inside another string (haystack). Approach I used: - Applied a sliding window technique - Compared substrings using "substring(i, j)" - Returned the starting index when a match is found Key learning: Understanding how index-based string operations work and how "substring()" helps in breaking down problems step-by-step. Also realized the importance of optimizing solutions to avoid unnecessary string creation. Consistency is slowly turning concepts into confidence! Looking forward to improving this further with more optimized approaches #Java #Coding #DSA #LeetCode #ProblemSolving #LearningJourney #Consistency
To view or add a comment, sign in
-
-
🚀 Day 98 - #100DaysOfCode Today’s problem was all about string comparison and operations simulation in Java. 💡 Problem Insight: Given a list of operations like "++X", "X++", "--X", "X--", we need to compute the final value of X after performing all operations. ⚠️ One key learning today: In Java, always use .equals() for string comparison instead of ==. Using == compares references, not actual content — a very common mistake! 🧠 Approach: Initialize x = 0 Traverse through each operation Increment or decrement based on the operation string 📌 What I Improved Today: Better understanding of string handling in Java Avoiding common pitfalls in comparisons Writing cleaner conditional logic #Java #CodingJourney #LeetCode #100DaysOfCode #ProblemSolving #Consistency
To view or add a comment, sign in
-
-
LeetCode Problem || Find Unique Binary String (1980)🚀 Today I solved the problem "Find Unique Binary String" using Java. 🔹 Problem: We are given an array of n binary strings, each of length n. The goal is to return a binary string of length n that does not exist in the array. 🔹 Approach (Diagonal Flip Technique): The idea is simple but powerful: Traverse the array using index i. Look at the i-th character of the i-th string (nums[i][i]). Flip the bit (0 → 1, 1 → 0). Append it to a result string. 💡 Time Complexity: O(n) Practicing problems like this strengthens logical thinking and problem-solving skills. #LeetCode #Java #CodingPractice #ProblemSolving #DSA
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