LeetCode Problem 1758: "Minimum changes to make alternating binary string": You are given a string s consisting only of the characters '0' and '1'. In one operation, you can change any '0' to '1' or vice versa. The string is called alternating if no two adjacent characters are equal. For example, the string "010" is alternating, while the string "0100" is not. Return the minimum number of operations needed to make s alternating. Approach: Here are two possibilities, first do not make change in the 1st character and apply rules following it, second consider step of changing the very first character and apply rules following it. The below implementation in Python works on this principle only, simple and straightforward. #Python #LeetCode #Strings #OptimalSolution #CompetitiveProgramming #DataStructures #Algorithms #ProblemSolving
Min Operations to Make Alternating Binary String
More Relevant Posts
-
🚀 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
To view or add a comment, sign in
-
-
🚀 Day 1 – DSA Series Two Sum (LeetCode) Starting my DSA problem-solving series with the classic Two Sum problem, implemented in Python. 🧠 Problem Statement Given an integer array nums and an integer target, return the indices of the two numbers such that they add up to the target. Constraints: • Exactly one valid solution exists. • The same element cannot be used twice. • The answer can be returned in any order. Example: nums = [2, 7, 11, 15], target = 9 Output: [0, 1] Because 2 + 7 = 9. 💡 Approach Implemented I used the nested loop (brute force) approach: • Iterate through each element • Check all remaining elements • Return indices when sum equals target ⏱ Complexity Analysis Time Complexity: O(n²) Space Complexity: O(1) Even for a well-known problem, breaking it down step by step helps strengthen logical thinking and reinforces core fundamentals before jumping to optimized solutions. I’ll be solving and sharing one DSA problem daily -focusing on clarity, approach, and complexity analysis. Let’s build consistency. 🚀 #DSA #LeetCode #Python #Algorithms #ProblemSolving #SoftwareEngineering #CodingJourney
To view or add a comment, sign in
-
-
LeetCode Problem 74: "Search a 2D matrix": You are given an m x n integer matrix matrix with the following two properties: Each row is sorted in non-decreasing order. The first integer of each row is greater than the last integer of the previous row. Given an integer target, return true if target is in matrix or false otherwise. You must write a solution in O(log(m * n)) time complexity. Approach: To solve this problem, we can use binary search algorithm, firstly find the required row in which the target element would reside and then apply binary search algorithm on that row, if still the element is not found return False else return True. #Python #DSA #LeetCode #BinarySearch #DataStructures #Algorithms #ProblemSolving #CompetitiveProgramming #Matrix
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
-
-
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
-
-
One thing keeps coming up when I build agents with LLMs. Tool interfaces matter more than people admit. If the tool description is vague or the inputs/outputs shift unexpectedly, the agent starts guessing and loops or picks the wrong path. I used to stuff long explanations into the tool schema hoping it would help reasoning. It mostly added noise. Now I keep descriptions short, explicit about formats, and add a quick example return if the output can be tricky. The model follows the contract better and wastes fewer tokens. It isn't magic. Clear interfaces cut hallucinations and retries more than tweaking prompts ever did. Anyone else notice this when switching between simple scripts and real agent loops? #aiagents #llms #python #toolcalling
To view or add a comment, sign in
-
-
LeetCode 23: Merge k sorted list: You are given an array of k linked-lists lists, each linked-list is sorted in ascending order. Merge all the linked-lists into one sorted linked-list and return it. Approach: Simple and straightforward, iterate through each of the linked-list in the given list, push each node's value into a priority queue using heapq module, create a new linked-list using the elements of priority queue. Time complexity: O(n logn) where n is the total number of values. Space complexity: O(n) #Python #LeetCode #DSA #CompetitiveProgramming #DataStructures #Algorithms #ProblemSolving
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
-
-
Binary Search on Rotated Arrays: Adapting Logarithmic Search to Broken Invariants Standard binary search requires sorted data. When an array is rotated (e.g., [4,5,6,7,0,1,2]), the sorted property breaks globally but persists locally — one half is always properly sorted. The adaptation: determine which half is sorted by comparing endpoints, then check if the target falls within that sorted range. This preserves O(log n) complexity despite the rotation disrupting global order. The Design Lesson: When invariants break, look for partial invariants. Here, global sorting is lost but local sorting remains. This "find the preserved property" approach applies broadly — searching in nearly-sorted data, handling corrupted indices with known structure, or working with time-series data with periodic gaps. The algorithm adapts to what guarantees still hold. Time: O(log n) | Space: O(1) #BinarySearch #AdaptiveAlgorithms #RotatedArrays #InvariantPreservation #Python #AlgorithmDesign #SoftwareEngineering
To view or add a comment, sign in
-
-
LeetCode Problem 1582: "Special Positions in a Binary Matrix": Given an m x n binary matrix mat, return the number of special positions in mat. A position (i, j) is called special if mat[i][j] == 1 and all other elements in row i and column j are 0 (rows and columns are 0-indexed). Approach: Initialize two arrays, one stores the number of 1s in each row of the matrix and other stores the number of 1s in each column, by traversing the matrix for filling each of the arrays. Now re-traverse the matrix and if mat[i][j]==1, check for the number of 1s in corresponding row and column, if number of 1s in that row and column is equal to one, we get our required position, calculate the total number of such positions. #Python #LeetCode #Matrices #ProblemSolving #CompetitiveProgramming #DataStructures #Algorithms #Arrays #Coding
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