Close Menu
Luminari | Learn Docker, Kubernetes, AI, Tech & Interview PrepLuminari | Learn Docker, Kubernetes, AI, Tech & Interview Prep
  • Home
  • Technology
    • Docker
    • Kubernetes
    • AI
    • Cybersecurity
    • Blockchain
    • Linux
    • Python
    • Tech Update
    • Interview Preparation
    • Internet
  • Entertainment
    • Movies
    • TV Shows
    • Anime
    • Cricket
What's Hot

U.S. Dismantles DanaBot Malware Network, Charges 16 in $50M Global Cybercrime Operation

May 23, 2025

Episode 7 – Apocalypse Hotel

May 23, 2025

Hackers using fake Ledger Live app to steal seed phrases and drain crypto

May 23, 2025
Facebook X (Twitter) Instagram
Facebook X (Twitter) Instagram
Luminari | Learn Docker, Kubernetes, AI, Tech & Interview Prep
  • Home
  • Technology
    • Docker
    • Kubernetes
    • AI
    • Cybersecurity
    • Blockchain
    • Linux
    • Python
    • Tech Update
    • Interview Preparation
    • Internet
  • Entertainment
    • Movies
    • TV Shows
    • Anime
    • Cricket
Luminari | Learn Docker, Kubernetes, AI, Tech & Interview PrepLuminari | Learn Docker, Kubernetes, AI, Tech & Interview Prep
Home » Minimum moves required to make string of bracket balanced
Misc

Minimum moves required to make string of bracket balanced

HarishBy HarishJuly 14, 2024No Comments1 Min Read
Facebook Twitter Pinterest LinkedIn Reddit WhatsApp Email
Share
Facebook Twitter Pinterest Reddit WhatsApp Email
Question:

Calculate the minimum number of swaps necessary to make a string balanced

Logic:

  1. Initialize sum = 0 to store the result.

  2. Maintain a count of the number of ‘[‘ brackets encountered.

  3. Reduce this count when encountering a ‘]’ character.

  4. If the count becomes negative, we must start balancing the string.

  5. Let index i represent the current position.

  6. Move forward to the next ‘[‘ at index j.

  7. Increase sum by j – i.

  8. Swap the ‘[‘ at position j with the one at position i, shifting other characters to the right.

  9. Set the count back to 1 and continue traversing the string.

Solution in python:

# Example usage s1 = “[]][][“ s2 = “[[][]]”

print(swapCount(s1)) # Output: 2

print(swapCount(s2)) # Output: 0

There are lot of sources are there when it comes to learning python. I will share if something is worth of sharing.. Stay tuned

Preparing for interview?

  • Checkout our Interview prep page, It might help you.

Share. Facebook Twitter Pinterest LinkedIn WhatsApp Reddit Email
Previous ArticleInterview questions- SAN, FLOGI, PLOGI, Process login and zoning
Next Article Learn Docker for free : Building images
Harish
  • Website
  • X (Twitter)

Related Posts

We’ve entered an era of fintech maximalism according to Mark Goldberg

April 16, 2025

Apple says zero-day bugs exploited against ‘specific targeted individuals’ using iOS

April 16, 2025

India’s Uber-rival BluSmart appears to suspend service in wake of EV loan probe

April 16, 2025

Bluesky feed builder Graze raises $1M, rolls out ads

April 16, 2025

Kia is pitching Americans an affordable EV sedan with EV4 debut

April 16, 2025

NSO lawyer names Mexico, Saudi Arabia, and Uzbekistan as spyware customers accused of 2019 WhatsApp hacks

April 16, 2025
Add A Comment
Leave A Reply Cancel Reply

Our Picks

U.S. Dismantles DanaBot Malware Network, Charges 16 in $50M Global Cybercrime Operation

May 23, 2025

Episode 7 – Apocalypse Hotel

May 23, 2025

Hackers using fake Ledger Live app to steal seed phrases and drain crypto

May 23, 2025

GitLab Duo Vulnerability Enabled Attackers to Hijack AI Responses with Hidden Prompts

May 23, 2025
Don't Miss
Blockchain

Hackers using fake Ledger Live app to steal seed phrases and drain crypto

May 23, 20253 Mins Read

Cybercriminals are using fake Ledger Live apps to drain macOS users’ crypto through malware that…

‘No questions asked’ Bitcoin launderer gets 6 years in prison

May 23, 2025

P2P lending platform comes out of stealth after two years of development

May 22, 2025

Sui validators freeze majority of stolen funds in $220M Cetus hack

May 22, 2025

Subscribe to Updates

Subscribe to our newsletter and never miss our latest news

Subscribe my Newsletter for New Posts & tips Let's stay updated!

About Us
About Us

Welcome to Luminari, your go-to hub for mastering modern tech and staying ahead in the digital world.

At Luminari, we’re passionate about breaking down complex technologies and delivering insights that matter. Whether you’re a developer, tech enthusiast, job seeker, or lifelong learner, our mission is to equip you with the tools and knowledge you need to thrive in today’s fast-moving tech landscape.

Our Picks

Anthropic CEO claims AI models hallucinate less than humans

May 22, 2025

Anthropic’s latest flagship AI sure seems to love using the ‘cyclone’ emoji

May 22, 2025

A safety institute advised against releasing an early version of Anthropic’s Claude Opus 4 AI model

May 22, 2025

Subscribe to Updates

Subscribe to our newsletter and never miss our latest news

Subscribe my Newsletter for New Posts & tips Let's stay updated!

Facebook X (Twitter) Instagram Pinterest
  • Home
  • About Us
  • Advertise With Us
  • Contact Us
  • DMCA Policy
  • Privacy Policy
  • Terms & Conditions
© 2025 luminari. Designed by luminari.

Type above and press Enter to search. Press Esc to cancel.