diff --git a/README.md b/README.md index cf30b6a..0667213 100644 --- a/README.md +++ b/README.md @@ -103,10 +103,11 @@ This repo tracks my journey solving LeetCode problems — not just code, but my | 72 | 1302 | [Deepest Leaves Sum](src/exercises/1302.deepest-leaves-sum.py) | Medium | Python | | 73 | 2016 | [Maximum Difference Between Increasing Elements](src/exercises/2016.maximum-difference-between-increasing-elements.py) | Easy | Python | | 74 | 2053 | [Kth Distinct String in an Array](src/exercises/2053.kth-distinct-string-in-an-array.py) | Easy | Python | -| 75 | 2294 | [Partition Array Such That Maximum Difference Is K](src/exercises/2294.partition-array-such-that-maximum-difference-is-k.py) | Medium | Python | -| 76 | 3442 | [Maximum Difference Between Even and Odd Frequency I](src/exercises/3442.maximum-difference-between-even-and-odd-frequency-i.py) | Easy | Python | -| 77 | 3582 | [Generate Tag for Video Caption](src/exercises/3582.generate-tag-for-video-caption.py) | Easy | Python | -| 78 | 3583 | [Count Special Triplets](src/exercises/3583.count-special-triplets.py) | Easy | Python | +| 75 | 2116 | [Check if a Parentheses String Can Be Valid](src/exercises/2116.check-if-a-parentheses-string-can-be-valid.py) | Medium | Python | +| 76 | 2294 | [Partition Array Such That Maximum Difference Is K](src/exercises/2294.partition-array-such-that-maximum-difference-is-k.py) | Medium | Python | +| 77 | 3442 | [Maximum Difference Between Even and Odd Frequency I](src/exercises/3442.maximum-difference-between-even-and-odd-frequency-i.py) | Easy | Python | +| 78 | 3582 | [Generate Tag for Video Caption](src/exercises/3582.generate-tag-for-video-caption.py) | Easy | Python | +| 79 | 3583 | [Count Special Triplets](src/exercises/3583.count-special-triplets.py) | Easy | Python | ## 🛠️ Tools & Scripts - `create_missing_notes.sh`: Automatically creates missing note files for solved problems diff --git a/src/exercises/2116.check-if-a-parentheses-string-can-be-valid.py b/src/exercises/2116.check-if-a-parentheses-string-can-be-valid.py new file mode 100644 index 0000000..9488a8a --- /dev/null +++ b/src/exercises/2116.check-if-a-parentheses-string-can-be-valid.py @@ -0,0 +1,57 @@ +from string import * +from re import * +from datetime import * +from collections import * +from heapq import * +from bisect import * +from copy import * +from math import * +from random import * +from statistics import * +from itertools import * +from functools import * +from operator import * +from io import * +from sys import * +from json import * +from builtins import * +import string +import re +import datetime +import collections +import heapq +import bisect +import copy +import math +import random +import statistics +import itertools +import functools +import operator +import io +import sys +import json +from typing import * + + +# @leet start +class Solution: + def canBeValid(self, s: str, locked: str) -> bool: + if len(s) % 2 != 0: + return False + mo = 0 + mxo = 0 + for i in range(len(s)): + if locked[i] == "1": + mo = mo + 1 if s[i] == "(" else mo - 1 + mxo = mxo + 1 if s[i] == "(" else mxo - 1 + else: + mo -= 1 + mxo += 1 + if mxo < 0: + return False + mo = mo if mo > 0 else 0 + return mo == 0 + + +# @leet end