2025 November 29 Problems

88 views
Skip to first unread message

daryl...@gmail.com

unread,
Nov 29, 2025, 12:34:30 PMNov 29
to leetcode-meetup
Feel free to work on any of the problems you want; we'll have people present their solutions at 11:30.

Please post your solutions to this thread so others can use this as a reference.

2103. Rings and Rods Easy 81.4%


1032. Stream of Characters Hard 51.6%

We're going to try to use the same Google meet link as the last few times: https://meet.google.com/xnn-kifj-jnx?pli=1

Anuj Patnaik

unread,
Nov 29, 2025, 12:46:48 PMNov 29
to leetcode-meetup
class Solution:
def countPoints(self, rings: str) -> int:
dict_of_rods = defaultdict(list)
ct = 0
i = 0
while i < len(rings):
color = rings[i]
rod_num = rings[i+1]
if color not in dict_of_rods[rod_num]:
dict_of_rods[rod_num].append(color)
i += 2
for j in dict_of_rods:
if len(dict_of_rods[j]) == 3:
ct += 1
return ct

Anuj Patnaik

unread,
Nov 29, 2025, 1:08:44 PMNov 29
to leetcode-meetup
class Solution:
def executeInstructions(self, n: int, startPos: List[int], s: str) -> List[int]:
answer = []
for i in range(len(s)):
curr_pos = startPos
ct = 0
for j in range(i, len(s)):
if s[j] == "R":
curr_pos = [curr_pos[0], curr_pos[1] + 1]
if curr_pos[1] == n:
break
else:
ct += 1
if s[j] == "L":
curr_pos = [curr_pos[0], curr_pos[1] - 1]
if curr_pos[1] < 0:
break
else:
ct += 1
if s[j] == "U":
curr_pos = [curr_pos[0] - 1, curr_pos[1]]
if curr_pos[0] < 0:
break
else:
ct += 1
if s[j] == "D":
curr_pos = [curr_pos[0] + 1, curr_pos[1]]
if curr_pos[0] == n:
break
else:
ct += 1
answer.append(ct)
return answer

Allen S.

unread,
Nov 29, 2025, 1:32:12 PMNov 29
to leetcode-meetup
func countPoints(rings string) int {
res := 0
rodRings := make([]map[byte]bool, 10)
for i := 0; i < len(rings); i += 2 {
color := rings[i]
rod := rings[i+1] - '0'
if rodRings[rod] == nil {
rodRings[rod] = make(map[byte]bool)
}
rodRings[rod][color] = true
}

for _, ringSet := range rodRings {
if len(ringSet) == 3 {
res++
}
}
return res
}

On Saturday, November 29, 2025 at 9:34:30 AM UTC-8 daryl...@gmail.com wrote:

Lyne Tchapmi

unread,
Nov 29, 2025, 1:54:16 PMNov 29
to leetcode-meetup
from collections import defaultdict
class Solution:
def countPoints(self, rings: str) -> int:
rods = defaultdict(lambda : {"R": 0, "G":0, "B": 0})
for i in range(0, len(rings), 2):
color = rings[i]
rod = rings[i+1]
rods[rod][color] += 1
rods_with_all_colors = 0
for rod, colors in rods.items():
if all([count > 0 for count in colors.values()]):
rods_with_all_colors +=1
return rods_with_all_colors

Allen S.

unread,
Nov 29, 2025, 2:08:18 PMNov 29
to leetcode-meetup
func executeInstructions(n int, startPos []int, s string) []int {
// simulation O(m^2)
ans := make([]int, len(s))
for i := range s {
r := startPos[0]
c := startPos[1]
j := 0
for i+j < len(s) {
switch s[i+j] {
case 'U': r--
case 'D': r++
case 'L': c--
case 'R': c++
}
if r >= 0 && r < n && c >= 0 && c < n {
j++
} else {
break
}
}
ans[i] = j
}
return ans
}

On Saturday, November 29, 2025 at 9:34:30 AM UTC-8 daryl...@gmail.com wrote:

Lyne Tchapmi

unread,
Nov 29, 2025, 2:18:41 PMNov 29
to leetcode-meetup
class Solution:
def executeInstructions(self, n: int, startPos: List[int], s: str) -> List[int]:
move = {"L": (0, -1), "R": (0, 1), "U": (-1, 0), "D": (1, 0)}
m = len(s)
def num_instructions_from_i(i):
pos = startPos
for j in range(i, m):
inst = s[j]
pos = (pos[0] + move[inst][0], pos[1] + move[inst][1])
if min(pos) < 0 or max(pos) > n - 1:
return j - i
return m - i
return [num_instructions_from_i(k) for k in range(len(s))]

Anuj Patnaik

unread,
Nov 29, 2025, 2:22:45 PMNov 29
to leetcode-meetup
class Node:
def __init__(self):
self.children = [None] * 26
self.end = False
class Trie:
def __init__(self):
self.root = Node()

def insert(self, word: str) -> None:
curr = self.root
for i in word:
if curr.children[ord(i) - ord("a")] == None:
curr.children[ord(i) - ord("a")] = Node()
curr = curr.children[ord(i) - ord("a")]
curr.end = True
class StreamChecker:
def __init__(self, words: List[str]):
self.trie = Trie()
self.stream = []
for w in words:
self.trie.insert(w[::-1])

def query(self, letter: str) -> bool:
self.stream.insert(0, letter)
curr = self.trie.root
for i in self.stream:
index = ord(i) - ord('a')
if curr.children[index] is None:
return False
curr = curr.children[index]
if curr.end == True:
return True
return False

Allen Baker

unread,
Nov 29, 2025, 2:30:17 PMNov 29
to leetcode-meetup
https://leetcode.com/problems/rings-and-rods/submissions/1842729301
class Solution {
public:
enum ColorOffset { R_OFFSET = 0, G_OFFSET = 1, B_OFFSET = 2, NUM_COLORS };
int countPoints(std::string rings) {
uint32_t rodColorMask = 0;
int numFullRods = 0;
for (size_t i = 0; i < rings.length(); i += 2) {
char color = rings[i];
uint8_t rod = rings[i + 1] - '0';
uint8_t colorOffset = 0;
switch (color) {
case 'R':
colorOffset = R_OFFSET;
break;
case 'G':
colorOffset = G_OFFSET;
break;
case 'B':
colorOffset = B_OFFSET;
break;
default:
break; // Not expected
}
const uint32_t newColorMask = 1
<< ((rod * NUM_COLORS) + colorOffset);
const bool isNewColor = ((rodColorMask & newColorMask) == 0);
rodColorMask |= newColorMask;
const bool hasRed =
((rodColorMask & (1 << ((rod * NUM_COLORS) + R_OFFSET))) != 0);
const bool hasGreen =
((rodColorMask & (1 << ((rod * NUM_COLORS) + G_OFFSET))) != 0);
const bool hasBlue =
((rodColorMask & (1 << ((rod * NUM_COLORS) + B_OFFSET))) != 0);
if (hasRed && hasGreen && hasBlue && isNewColor) {
++numFullRods;
}
}
return numFullRods;
}
};


Gowrima Jayaramu

unread,
Nov 29, 2025, 2:42:53 PMNov 29
to leetcode-meetup
class Solution:
def countPoints(self, rings: str) -> int:
num_rods = 0
color_to_rod = {}
i = 0

# rings = rings[::-1]

if len(rings) < 6:
return 0

while i < len(rings):
color = rings[i]
rod = rings[i + 1]

if rod not in color_to_rod:
color_to_rod[rod] = set()

color_to_rod[rod].add(color)

i += 2

for rod in color_to_rod:
if len(color_to_rod[rod]) == 3:
num_rods += 1

return num_rods

FloM

unread,
Dec 3, 2025, 2:39:34 PM (12 days ago) Dec 3
to leetcode-meetup
Time: O(m) Memory: O(m), where m is size of m.
class Solution {
public:

vector<int> executeInstructions(int n, vector<int>& startPos, string s) {
// Say we have:
// 0 1 2 3 4 5
// R R D D L U
// For now, say we only care about horizontal movement:
// 0 1 2 3 4 5
// R R - - L -
// Say indexesOfXs is {2 : {0, 4}}, {3 : {1}}.
// That means that given that we start at position 1. Once we
// leave index 0, we'll be at position 2. Once we leave index 1,
// we'll be at position 3. Once we leave index 4, we'll be at
// position 2.
// The indexes we must leave in order to be at position 2
// are {0, 4}. The indexes we must leave to be at position
// 3 are {1}.
unordered_map<int, deque<int>> indexesOfYs{};
unordered_map<int, deque<int>> indexesOfXs{};

int xVal = startPos[1];
int yVal = startPos[0];

for(int ii=0; ii<s.size(); ++ii)
{
if (s[ii] == 'R')
{
++xVal;
indexesOfXs[xVal].push_back(ii);
}
else if (s[ii] == 'L')
{
--xVal;
indexesOfXs[xVal].push_back(ii);
}
else if (s[ii] == 'U')
{
--yVal;
indexesOfYs[yVal].push_back(ii);
}
else // 'D'
{
++yVal;
indexesOfYs[yVal].push_back(ii);
}
}

vector<int> ans(s.size(), 0);

// At the start zero steps are removed, we start at index 0.
int exSteps = 0;
// If the position is -1 or n, then we have stepped off the grid.
int yLookForMax = n;
int yLookForMin = -1;
int xLookForMax = n;
int xLookForMin = -1;
// ii is the index in s. We lose the left most letter after each iteration.

// The positions reached in indexesOfXs and indexesOfYs assume we are going through each
// of the indices in s, but at the end of the iteration the left most letter is deleted.
// Say the left most letter is an R. Then instead of correcting all the values in indexesOfXs,
// update xLookForMax from n to (n + 1).
for(int ii=0; ii<s.size(); ++ii)
{
// For each direction find the smallest index for sought position.
int smallestIndex = s.size();
if (indexesOfYs.find(yLookForMax) != indexesOfYs.end())
{
while( (!indexesOfYs[yLookForMax].empty()) && (indexesOfYs[yLookForMax].front() < exSteps))
{
indexesOfYs[yLookForMax].pop_front();
}
if (indexesOfYs[yLookForMax].empty())
{
indexesOfYs.erase(yLookForMax);
}
else
{
smallestIndex = std::min(smallestIndex, indexesOfYs[yLookForMax].front());
}
}
if (indexesOfYs.find(yLookForMin) != indexesOfYs.end())
{
while(!indexesOfYs[yLookForMin].empty() && (indexesOfYs[yLookForMin].front() < exSteps))
{
indexesOfYs[yLookForMin].pop_front();
}
if (indexesOfYs[yLookForMin].empty())
{
indexesOfYs.erase(yLookForMin);
}
else
{
smallestIndex = std::min(smallestIndex, indexesOfYs[yLookForMin].front());
}
}
if (indexesOfXs.find(xLookForMax) != indexesOfXs.end())
{
while(!indexesOfXs[xLookForMax].empty() && (indexesOfXs[xLookForMax].front() < exSteps))
{
indexesOfXs[xLookForMax].pop_front();
}
if (indexesOfXs[xLookForMax].empty())
{
indexesOfXs.erase(xLookForMax);
}
else
{
smallestIndex = std::min(smallestIndex, indexesOfXs[xLookForMax].front());
}
}
if (indexesOfXs.find(xLookForMin) != indexesOfXs.end())
{
while(!indexesOfXs[xLookForMin].empty() && (indexesOfXs[xLookForMin].front() < exSteps))
{
indexesOfXs[xLookForMin].pop_front();
}
if (indexesOfXs[xLookForMin].empty())
{
indexesOfXs.erase(xLookForMin);
}
else
{
smallestIndex = std::min(smallestIndex, indexesOfXs[xLookForMin].front());
}
}
ans[ii] = smallestIndex - exSteps;

++exSteps;

if (s[ii] == 'R')
{
++xLookForMin;
++xLookForMax;
}
else if (s[ii] == 'L')
{
--xLookForMin;
--xLookForMax;
}
else if (s[ii] == 'U')
{
--yLookForMin;
--yLookForMax;
}
else // 'D'
{
++yLookForMin;
++yLookForMax;
}

}

return ans;

}
};
Reply all
Reply to author
Forward
0 new messages