[LeetCode] Strobogrammatic Number II

247. Strobogrammatic Number II

Given an integer n, return all the strobogrammatic numbers that are of length n. You may return the answer in any order.

A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside down).

Read more
[LeetCode] Binary Tree Postorder Traversal

145. Binary Tree Postorder Traversal

Given the root of a binary tree, return the postorder traversal of its nodes’ values.

Read more
[LeetCode] Binary Tree Preorder Traversal

144. Binary Tree Preorder Traversal

Given the root of a binary tree, return the preorder traversal of its nodes’ values.

Read more
[LeetCode] Longest Happy Prefix

1392. Longest Happy Prefix

A string is called a happy prefix if is a non-empty prefix which is also a suffix (excluding itself).

Given a string s, return the longest happy prefix of s. Return an empty string “” if no such prefix exists.

Read more
[LeetCode] Remove All Occurrences of a Substring

1910. Remove All Occurrences of a Substring

Given two strings s and part, perform the following operation on s until all occurrences of the substring part are removed:

  • Find the leftmost occurrence of the substring part and remove it from s.

Return s after removing all occurrences of part.

A substring is a contiguous sequence of characters in a string.

Read more
[LeetCode] Shortest Palindrome

214. Shortest Palindrome

You are given a string s. You can convert s to a palindrome by adding characters in front of it.

Return the shortest palindrome you can find by performing this transformation.

Read more
[LeetCode] Repeated Substring Pattern

459. Repeated Substring Pattern

Given a string s, check if it can be constructed by taking a substring of it and appending multiple copies of the substring together.

Read more
[LeetCode] Hand of Straights

846. Hand of Straights

Alice has some number of cards and she wants to rearrange the cards into groups so that each group is of size groupSize, and consists of groupSize consecutive cards.

Given an integer array hand where hand[i] is the value written on the ith card and an integer groupSize, return true if she can rearrange the cards, or false otherwise.

Read more
[LeetCode] Divide Array in Sets of K Consecutive Numbers

1296. Divide Array in Sets of K Consecutive Numbers

Given an array of integers nums and a positive integer k, check whether it is possible to divide this array into sets of k consecutive numbers.

Return true if it is possible. Otherwise, return false.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
class Solution {
public:
bool isPossibleDivide(vector<int>& nums, int k) {
if(nums.size() % k) return false;
map<int, int> count;
for(auto n : nums) count[n]++;
while(!count.empty()) {
auto be = count.begin();
if(be->second) {
auto nxt = next(be);
for(int i = 1; i < k; i++,nxt++) {
if(nxt == count.end()) return false;
if(nxt->first != be->first + i) return false;
if(nxt->second < be->second)return false;
nxt->second -= be->second;

}

}
count.erase(be);

}
return true;
}
};

[LeetCode] Longest Substring with At Most Two Distinct Characters

159. Longest Substring with At Most Two Distinct Characters

Given a string s, return the length of the longest substring that contains at most two distinct characters.

Read more