Skip to content

Commit 51e2782

Browse files
committed
feat(strings): lexicographically largest string in box
1 parent d0334be commit 51e2782

File tree

15 files changed

+111
-16
lines changed

15 files changed

+111
-16
lines changed

datastructures/linked_lists/singly_linked_list/single_linked_list.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,6 @@ def insert_after_node(self, prev_node: Any, data: Any):
9090
current = current.next
9191

9292
def insert_before_node(self, next_key: Any, data: T):
93-
9493
pass
9594

9695
def get_nth_node(self, position: int) -> Union[SingleNode, None]:

datastructures/lists/is_sorted_how/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ def is_sorted_and_how_2(arr):
1515
return (
1616
"yes, ascending"
1717
if is_sorted_with(arr, operator.le)
18-
else "yes, descending" if is_sorted_with(arr, operator.ge) else "no"
18+
else "yes, descending"
19+
if is_sorted_with(arr, operator.ge)
20+
else "no"
1921
)
2022

2123

machine_learning/soccer_predictions/features.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,7 @@
7878
ON cur.matchid = opp.matchid
7979
WHERE cur.teamid != opp.teamid
8080
ORDER BY cur.matchid, cur.teamid
81-
""" % {
82-
"team_game_summary": match_stats.team_game_summary_query()
83-
}
81+
""" % {"team_game_summary": match_stats.team_game_summary_query()}
8482

8583

8684
def get_match_history(history_size):
@@ -286,9 +284,7 @@ def get_wc_history_query(history_size):
286284
return """
287285
SELECT * FROM (%(history_query)s) WHERE competitionid = 4
288286
ORDER BY timestamp DESC, matchid, is_home
289-
""" % {
290-
"history_query": get_history_query(history_size)
291-
}
287+
""" % {"history_query": get_history_query(history_size)}
292288

293289

294290
def get_wc_features(history_size):

machine_learning/soccer_predictions/match_stats.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,7 @@
6565
WHERE dist < 40)
6666
GROUP BY matchid, teamid
6767
ORDER BY matchid, teamid
68-
""" % {
69-
"touch_table": _TOUCH_TABLE
70-
}
68+
""" % {"touch_table": _TOUCH_TABLE}
7169

7270
# Subquery to compute raw number of goals scored. Does not take
7371
# into account own-goals (i.e. if a player scores an own-goal against

puzzles/arrays/lucky_numbers_in_a_matrix/test_lucky_numbers_in_a_matrix.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99

1010
class LuckyNumbersInAMatrixTestCase(unittest.TestCase):
11-
1211
@parameterized.expand(
1312
[
1413
([[3, 7, 8], [9, 11, 13], [15, 16, 17]], [15]),

puzzles/graphs/number_of_islands/union_find.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
class UnionFind:
2-
32
# Initializing the parent list and count variable by traversing the grid
43
def __init__(self, grid):
54
self.parent = []

puzzles/poker/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,9 @@ def hand_rank(hand):
4848
else (
4949
2
5050
if counts == (2, 2, 1)
51-
else 1 if counts == (2, 1, 1, 1) else 0
51+
else 1
52+
if counts == (2, 1, 1, 1)
53+
else 0
5254
)
5355
)
5456
)
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Find the Lexicographically Largest String From Box
2+
3+
You are given a string, word, and an integer numFriends, representing the number of friends participating in a game
4+
organized by Alice.
5+
6+
The game consists of multiple rounds, and in each round:
7+
- The string word is split into exactly numFriends non-empty substrings.
8+
- The split must be unique; no previous round has produced the same sequence of splits.
9+
- All resulting substrings from the split are placed into a box.
10+
11+
When all rounds are over and all possible unique splits have been performed, determine the lexicographically largest
12+
string among all the substrings in the box.
13+
14+
> A string `a` is considered lexicographically larger than a string `b` if:
15+
> - At the first position where `a` and `b` differ, the character in `a` comes later than the corresponding character in
16+
`b` in the alphabet.
17+
> - If `a` is a prefix of `b`, the longer string is considered larger.
18+
19+
**Constraints**
20+
21+
- 1 ≤ `word.length` ≤ 10^3
22+
- `word` consists only of lowercase English letters.
23+
- 1 ≤ `numFriends``word.length`
24+
25+
## Examples
26+
27+
![Example 1](./images/examples/lexicographically_largest_string_from_box_example_1.png)
28+
![Example 2](./images/examples/lexicographically_largest_string_from_box_example_2.png)
29+
![Example 3](./images/examples/lexicographically_largest_string_from_box_example_3.png)
30+
![Example 4](./images/examples/lexicographically_largest_string_from_box_example_4.png)
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
def lexicographically_largest_string_from_box(word: str, num: int) -> str:
2+
n = len(word)
3+
max_substring = ""
4+
5+
# Iterate through every possible starting character of a substring
6+
for i in range(n):
7+
# We want to find the longest valid substring starting at i.
8+
# To do this, we assume this substring acts as the m-th part
9+
# of the split, where m is as large as possible (latest slot).
10+
11+
# If we start at 0, we MUST be the 1st part.
12+
if i == 0:
13+
m = 1
14+
else:
15+
# If we start later, we can be at most the (i+1)-th part
16+
# (since parts 1..m-1 need at least 1 char each).
17+
# We are also capped by the total number of friends.
18+
if num == 1:
19+
# If only 1 num, the part MUST start at 0. i > 0 is invalid.
20+
continue
21+
m = min(num, i + 1)
22+
23+
# Calculate how many parts must come AFTER this one
24+
parts_after = num - m
25+
26+
# The substring must end early enough to leave 1 char
27+
# for each remaining part.
28+
j = n - parts_after
29+
30+
# If the derived end index j is not greater than i,
31+
# this split isn't possible (substring would be empty).
32+
if j > i:
33+
candidate = word[i:j]
34+
if candidate > max_substring:
35+
max_substring = candidate
36+
37+
return max_substring
39.5 KB
Loading

0 commit comments

Comments
 (0)