-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Sync LeetCode submission Runtime - 1093 ms (80.64%), Memory - 18.5 MB…
… (49.46%)
- Loading branch information
Showing
2 changed files
with
63 additions
and
0 deletions.
There are no files selected for viewing
26 changes: 26 additions & 0 deletions
26
2303-unique-substrings-with-equal-digit-frequency/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
Given a digit string <code>s</code>, return <em>the number of <strong>unique substrings </strong>of </em><code>s</code><em> where every digit appears the same number of times.</em> | ||
<p> </p> | ||
<p><strong class="example">Example 1:</strong></p> | ||
|
||
<pre> | ||
<strong>Input:</strong> s = "1212" | ||
<strong>Output:</strong> 5 | ||
<strong>Explanation:</strong> The substrings that meet the requirements are "1", "2", "12", "21", "1212". | ||
Note that although the substring "12" appears twice, it is only counted once. | ||
</pre> | ||
|
||
<p><strong class="example">Example 2:</strong></p> | ||
|
||
<pre> | ||
<strong>Input:</strong> s = "12321" | ||
<strong>Output:</strong> 9 | ||
<strong>Explanation:</strong> The substrings that meet the requirements are "1", "2", "3", "12", "23", "32", "21", "123", "321". | ||
</pre> | ||
|
||
<p> </p> | ||
<p><strong>Constraints:</strong></p> | ||
|
||
<ul> | ||
<li><code>1 <= s.length <= 1000</code></li> | ||
<li><code>s</code> consists of digits.</li> | ||
</ul> |
37 changes: 37 additions & 0 deletions
37
2303-unique-substrings-with-equal-digit-frequency/solution.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
# Approach 1: Optimized Brute Force | ||
|
||
# Time: O(n^3) | ||
# Space: O(n^3) | ||
|
||
class Solution: | ||
def equalDigitFrequency(self, s: str) -> int: | ||
n = len(s) | ||
valid_substrings = set() | ||
|
||
for start in range(n): | ||
digit_freq = [0] * 10 # Frequency array for digits 0-9 | ||
|
||
for end in range(start, n): | ||
digit_freq[ord(s[end]) - ord('0')] += 1 | ||
|
||
# Variable to store the frequency all digits must match | ||
common_freq = 0 | ||
is_valid = True | ||
|
||
for count in digit_freq: | ||
if count == 0: | ||
continue # Skip digits not in the substring | ||
if common_freq == 0: | ||
# First digit found, set common_frequency | ||
common_freq = count | ||
if common_freq != count: | ||
# Mismatch in frequency, mark as invalid | ||
is_valid = False | ||
break | ||
|
||
if is_valid: | ||
substring = s[start : end + 1] | ||
valid_substrings.add(substring) | ||
|
||
return len(valid_substrings) | ||
|