🚀 Day 54 of #100DaysOfCode 🧩 Problem: Minimum Number of Flips to Make the Binary String Alternating Today I solved an interesting problem involving string manipulation, rotations, and sliding window techniques. 💡 Key Idea: An alternating binary string can only follow two patterns: • "010101..." • "101010..." Since the problem allows rotating the string (moving the first character to the end), the trick is to consider all rotations efficiently. 🔑 Optimization Trick: Instead of rotating the string repeatedly, we can concatenate the string with itself ("s + s") and use a sliding window of size "n" to simulate all rotations. Then we compare each window with both alternating patterns and count the minimum flips required. ⚡ Concepts Used • Sliding Window • Greedy Thinking • String Pattern Matching • Optimization Trick ("s + s" rotation) 💻 Language: Python This problem was a great exercise in thinking about string rotations and pattern mismatches efficiently in O(n) time. 📈 Consistency is the key to improving problem-solving skills! #LeetCode #CodingChallenge #Python #DataStructures #Algorithms #ProblemSolving #100DaysOfCode
Python Solution for Minimum Flips to Alternating Binary String
More Relevant Posts
-
🚀 #100DaysOfCode – Day 52 📌 Problem: Sort Colors (LeetCode 75) 💡 Problem Idea: You are given an array containing only 0s, 1s, and 2s, representing three colors. The task is to sort the array in-place so that all 0s come first, then 1s, and then 2s. ⚡ Key Insight: Instead of using a sorting algorithm, we can solve this in one pass using three pointers. We maintain three regions: Left pointer → position for next 0 Right pointer → position for next 2 Current pointer (i) → traverses the array 🎯 Approach (Dutch National Flag Algorithm): If element is 0 → swap with left, move both left and i If element is 1 → just move i If element is 2 → swap with right, decrease right 📈 Complexity: Time Complexity: O(n) Space Complexity: O(1) (in-place sorting) ✅ Efficient because the array is processed only once. 💭 Learning: This problem is a great example of how pointer techniques can optimize sorting problems without using built-in sort functions. #DSA #LeetCode #Python #CodingJourney #ProblemSolving #Algorithms #100DaysOfCode #LinkedInLearning
To view or add a comment, sign in
-
-
🚀 DSA Practice – Check if a String is a Palindrome Today I worked on a fundamental string challenge: Determining whether a given string is a palindrome. 📌 Problem Statement: Given a string s, verify if it reads the same backward as forward. A palindrome is a sequence that is identical even when its order is reversed. 🧠 My Approach: 1️⃣ Clean the Data: Convert the string to a uniform case (lowercase) to ensure the comparison is case-insensitive. 2️⃣ Reverse and Compare: Use Python’s powerful slicing technique s[::-1] to create a reversed version of the string. 3️⃣ Verification: Compare the original string with the reversed one. If they match, it's a palindrome; otherwise, it's not. ⚙️ Example: Input: "madam" → Output: True Input: "hello" → Output: False 📊 Complexity: Time Complexity: $O(n)$ (where $n$ is the length of the string, as we traverse it to reverse). Auxiliary Space: $O(n)$ (to store the reversed version of the string). 💻 Language Used: Python This problem is a great way to practice string slicing and understanding how data is stored and compared in memory. It's a stepping stone toward more complex "Two-Pointer" string problems! #DSA #Python #CodingPractice #ProblemSolving #Strings #Palindrome #LearningInPublic
To view or add a comment, sign in
-
-
Spent an evening porting one of my all-time favourite generative art pieces to Python — and I couldn't be happier with how it turned out. 🎨 Substrate was originally created by Jared Tarbell back in 2004 at complexification.net. The idea is beautifully simple: cracks grow across a canvas, always branching at right angles, filling the enclosed regions with soft watercolor-like sand. The result looks like aerial geology, or cracked ceramic glaze, or something that just feels alive. The original was written in Processing. I ported it to Python with a live preview GUI, numba JIT acceleration (~35× faster than pure Python), and a few quality-of-life additions — canvas size presets (1080p, 4K), fill % stop control, PNG/JPEG export. To try it yourself: - Download the ZIP → https://lnkd.in/gwcanwQx → green Code button → Download ZIP - Unzip, open a terminal in the folder - Run "pip install Pillow numpy numba" then "python substrate_gui.py" Full credit to Jared Tarbell for the original algorithm and the inspiration. Some ideas are just too beautiful not to revisit. 🔗 https://lnkd.in/gwcanwQx #GenerativeArt #Python #OpenSource #CreativeCoding #Numba
To view or add a comment, sign in
-
-
🚀 DSA Practice Update! Solved Remove Duplicates from Sorted Array on LeetCode with an optimized approach. Brute Force: O(n log n) time | O(n) space Optimized (Two Pointer):O(n) time | O(1) space Choosing the right approach matters more than just solving the problem. The Brute Force approach removes duplicates by using extra data structures like a set and then sorting the array again. Because of sorting, its time complexity becomes O(n log n) and it also requires O(n) extra space. Additionally, it is not in-place, which makes it less efficient and not ideal for optimized solutions. “I used a two-pointer approach where one pointer tracks the position of unique elements and the other scans the array. This gives O(n) time and O(1) space complexity.” Consistency + Optimization = Growth 📈 #DSA #LeetCode #InterviewPrep #Python #CodingJourney
To view or add a comment, sign in
-
-
The Basics of Strings Focus: String Properties & Indexing Day 3 was all about Strings. I thought I knew what a "word" was, but Python sees things a bit differently. 🧐 What clicked today: Case Sensitivity: Python is picky! Variable and variable are two different citizens. Slicing: String Methods: Indexing: Learning that counting starts at 0 was a bit of a brain-bender at first, but it makes so much sense now. Immutability: This was the big "Aha!" moment—knowing that once a string is created, you can’t just reach in and change a character. You have to build a new one. It’s these small details that make the difference between code that runs and code that crashes. 💻 #Strings #CodingJourney #PythonLearning #ContinuousGrowth #PythonDay3
To view or add a comment, sign in
-
-
🚀 Solved a great problem today: “Consecutive 1’s Not Allowed” At first glance, it looked like a simple binary string problem… but it quickly turned into a lesson in pattern recognition and dynamic thinking. 📌 What the problem was about: Count all binary strings of length n such that no two 1’s are consecutive. 💡 What I learned: Instead of brute forcing all combinations (which would be exponential), the key was to observe a pattern: If a string ends with 0 → we can add 0 or 1 If it ends with 1 → we can only add 0 This leads to a recurrence: 👉 dp[n] = dp[n-1] + dp[n-2] Which is basically the Fibonacci pattern in disguise. 🧠 Big takeaway: Many problems are not about coding harder… they’re about seeing the hidden pattern behind the problem. This was a reminder that: Brute force is rarely the answer Thinking in terms of state transitions is powerful Optimization often comes from observation, not syntax 📷 Sharing my solution screenshot below 👇 #DataStructures #DynamicProgramming #ProblemSolving #Python #LearningInPublic #DataAnalyticsJourney
To view or add a comment, sign in
-
-
Day 67 of 365 Days of code the infamous brainrot number Qn 1) Reorder list You are given the head of a singly linked-list. The positions of a linked list of length = 7 for example, can intially be represented as: [0, 1, 2, 3, 4, 5, 6] Reorder the nodes of the linked list to be in the following order: [0, 6, 1, 5, 2, 4, 3] Notice that in the general case for a list of length = n the nodes are reordered to be in the following order: [0, n-1, 1, n-2, 2, n-3, ...] You may not modify the values in the list's nodes, but instead you must reorder the nodes themselves. approach: use 5 pointers(scary :")) #365daysOfCode #NeetCode #leetcode #DSA #python #LeetCode #ProblemSolving #Algorithms #365dayschallenge
To view or add a comment, sign in
-
-
Merge Intervals (LeetCode 56) - Medium Key Learnings: Sorting: By sorting intervals based on their start times, we can process them in a single pass (O(N)). Overlap Logic: Comparing the current_start with the previous_end time is the secret sauce to identifying overlaps. Space-Time Tradeoff: Sorting takes O(N log N) time, and we use O(N) space to store the results. #CodingJourney #LeetCode #Blind75 #SDEPreparation #SoftwareEngineering #Python #DataStructures
To view or add a comment, sign in
-
-
Reducing 3Sum from O(n³) to O(n²): The Power of Sorting + Two Pointers Most developers first approach 3Sum with nested loops, hitting O(n³) complexity. The breakthrough: sort the array first, then reduce it to n iterations of the Two Sum problem. For each element as the fixed anchor, use two pointers on the remaining sorted portion to find pairs that complete the triplet. Sorting unlocks directional movement (move left if sum too high, right if too low), collapsing a dimension of complexity. The Critical Detail: Duplicate handling isn't just about correctness — it's about recognizing that sorted order lets you skip duplicates in O(1) rather than using a HashSet for deduplication. The while nums[l] == nums[l-1] skip after finding a valid triplet prevents processing identical combinations. Time: O(n²) | Space: O(1) excluding output #AlgorithmOptimization #TwoPointers #Sorting #ComplexityReduction #Python #CodingInterview #SoftwareEngineering
To view or add a comment, sign in
-
-
Hello everyone, I implemented the Mixed-Exponential Jump Diffusion (MEJD) model for European options from scratch in Python. The model extends classical jump-diffusion frameworks by allowing jump sizes to follow a mixture of exponential distributions, providing greater flexibility in capturing the fat tail behaviour observed in financial markets. Key results: 1. Reduces exactly to Black-Scholes when jump intensity λ = 0 2. Matches published numerical benchmarks to within 0.15% Paper: https://lnkd.in/gpSUmS-F GitHub: https://lnkd.in/gs-5M3gP #Python #QuantitativeFinance
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