2025 July 12 Problems

103 views
Skip to first unread message

daryl...@gmail.com

unread,
Jul 12, 2025, 12:44:58 PMJul 12
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.


3597. Partition String Medium 56.2%



Please download and import the following iCalendar (.ics) files to your calendar system.

FloM

unread,
Jul 12, 2025, 1:10:28 PMJul 12
to leetcode-meetup
Time: O(n), Mem: O(n). Say n is the number of decimal digits in num*num*num. Say n is 10 because num < 1000, (really makes time and memory constant).
class Solution {
public:

vector<char> getReversedHex(uint32_t num)
{
vector<char> hexAns{};
while(num != 0)
{
int n = num % 16;
num /= 16;

if (n < 10)
{
hexAns.push_back(static_cast<char>(n + 48));
}
else
{
hexAns.push_back(static_cast<char>(n+55));
}
}
return hexAns;
}

vector<char> getReversedTriHex(uint32_t num)
{
vector<char> triHex{};
while(num != 0)
{
int n = num % 36;
num /= 36;

if (n < 10)
{
triHex.push_back(static_cast<char>(n + 48));
}
else
{
triHex.push_back(static_cast<char>(n+55));
}
}
return triHex;
}
string concatHex36(int n) {

string ans{};
vector<char> hexVec = getReversedHex(n * n);
vector<char> triHexVec = getReversedTriHex(n * n * n);

for(uint32_t ii=0; ii<hexVec.size(); ++ii)
{
uint32_t jj = hexVec.size() - 1 - ii;
ans += hexVec[jj];
}

for(uint32_t ii=0; ii<triHexVec.size(); ++ii)
{
uint32_t jj = triHexVec.size() - 1 - ii;
ans += triHexVec[jj];
}

return ans;
}
};

FloM

unread,
Jul 12, 2025, 1:16:37 PMJul 12
to leetcode-meetup
3597. Partition String Medium
Already done this problem.
Time: O(n^2) Mem:O(n). n is the length of s.
class Solution {
public:
vector<string> partitionString(string s) {
unordered_set<string> seenSegments{};
vector<string> ans{};
string curSegment = "";
for(int ii=0; ii<s.size(); ++ii)
{
char c = s[ii];
curSegment.push_back(c);

// if current segment has not been seen, then add it to ans.
// and reset curSegment.
if (!seenSegments.contains(curSegment))
{
ans.push_back(curSegment);
seenSegments.insert(curSegment);
curSegment = "";
}
}
return ans;
}
};

Sharad Bagri

unread,
Jul 12, 2025, 1:19:28 PMJul 12
to leetcod...@googlegroups.com
class Solution:
def partitionString(self, s: str) -> List[str]:
seen_set = set()
seen = list()
curr_str = ""
for a_char in s:
curr_str += a_char
if curr_str in seen_set:
continue
else:
seen_set.add(curr_str)
seen.append(curr_str)
curr_str = ""
return seen

Time & space complexity: O(N) 

Anuj Patnaik

unread,
Jul 12, 2025, 1:28:58 PMJul 12
to leetcode-meetup
class Solution:
def number_to_str_base(n, b):
final_str = ""
while n > 0:
if n % b >= 10:
final_str = chr(ord("A") + (n%b) - 10) + final_str
else:
final_str = str(n%b) + final_str
n = n//b
return final_str

def concatHex36(self, n: int) -> str:
return Solution.number_to_str_base(n * n, 16) + Solution.number_to_str_base(n * n * n, 36)



Sharad Bagri

unread,
Jul 12, 2025, 1:58:59 PMJul 12
to leetcod...@googlegroups.com
class Solution:
def __init__(self):
self.repr = {}
for i, char in enumerate("ABCDEFGHIJKLMNOPQRSTUVWXYZ"):
self.repr[10+i] = char

def convert_to_base(self, base, num):
num_str = ""
while num>0:
remainder = num % base
num_str = str(remainder if remainder <10 else self.repr[remainder]) + num_str
num = num // base
return num_str
def concatHex36(self, n: int) -> str:
return self.convert_to_base(16, n**2) + self.convert_to_base(36, n**3)

 

daryl...@gmail.com

unread,
Jul 12, 2025, 2:04:16 PMJul 12
to leetcode-meetup
3597. Partition String Medium 56.2%

Straightforward solution that basically follows the algorithm in the problem description. Main design question is how to check whether a segment exists already. I just used a set here, though a trie would also work. Main performance bottleneck is dealing with string construction than anything else.

O(s^2) time
O(s) space

class Solution:
    def partitionString(self, s: str) -> List[str]:
        segment = ""
        seen = set()
        result = []
        for letter in s:
            segment += letter
            if segment not in seen:
                seen.add(segment)
                result.append(segment)
                segment = ""
        return result

On Saturday, July 12, 2025 at 9:44:58 AM UTC-7 daryl...@gmail.com wrote:

Sourabh Majumdar

unread,
Jul 12, 2025, 2:20:55 PMJul 12
to leetcode-meetup
// 3597 | Partition String
class Solution {
public:
vector<string> partitionString(string s) {

std::unordered_set<std::string> seen_segments;
std::vector<std::string> partitions; // I will add the segments to the partitions in order of when I see them.
std::string current_segment = "";
for(auto character : s) {
current_segment += character;
if (seen_segments.contains(current_segment)) {
continue;
}

// otherwise, add this segment and start a new one.
seen_segments.insert(current_segment);
partitions.push_back(current_segment);
current_segment = "";
}

return partitions;
}
};

Allen S.

unread,
Jul 12, 2025, 2:31:23 PMJul 12
to leetcode-meetup
func concatHex36(n int) string {
hd := strings.ToUpper((strconv.FormatInt(int64(n*n), 16)))
ht := strings.ToUpper((strconv.FormatInt(int64(n*n*n), 36)))
return hd + ht
}

Allen S.

unread,
Jul 12, 2025, 2:31:40 PMJul 12
to leetcode-meetup
func partitionString(s string) []string {
seen := make(map[string]bool)
segment := ""
segments := make([]string, 0, len(seen))
for _, c := range s {
segment += string(c)
if !seen[segment] {
seen[segment] = true
segments = append(segments, segment)
segment = ""
}
}
return segments
}

Sourabh Majumdar

unread,
Jul 12, 2025, 2:54:13 PMJul 12
to leetcode-meetup
class Solution {
public:
std::string ConvertToBase(long long number, int base) {
std::stack<std::string> representation_stack;

while(number > 0) {
std::lldiv_t div = std::lldiv(number, base);
long long digit = div.rem;
number = div.quot;

if (digit > 9) {
digit = digit - 9;
char letter = (char)('A' + digit - 1);
representation_stack.push(std::string(1, letter));
continue;
}

representation_stack.push(std::to_string(digit));
}

// convert the stack to a string
std::string rep_string = "";

while(!representation_stack.empty()) {
std::string character = representation_stack.top();
representation_stack.pop();

rep_string += character;
}

return rep_string;
}
string concatHex36(int n) {

long long number = n;
long long squared_num = std::pow(number, 2);
long long cubed_num = std::pow(number, 3);

std::string hexadecimal_string = ConvertToBase(squared_num, 16);
std::string hexatrigesimal_string = ConvertToBase(cubed_num, 36);

return hexadecimal_string + hexatrigesimal_string;
}
};

Reply all
Reply to author
Forward
0 new messages