Compare commits

...

5 Commits

Author SHA1 Message Date
user 8db8b2a98f feat(repo): add repository description 2026-07-29 21:26:55 +04:00
user d8c1f83711 fix: handle odd-length palindromes by preserving middle character 2026-07-29 21:19:50 +04:00
user 70090a66e7 refactor: remove unnecessary using declarations and qualify std symbols
Replace global using declarations with explicit std:: qualifications.
Update ranges algorithms usage to std::ranges and fix permutation
iterator
offset to correctly handle 1-based k indexing.
2026-07-29 21:19:50 +04:00
user b7f174dc2d fix: correct permutation iterator offset for k index
Adjust iterator advancement to account for 1-based k indexing.
This fixes returning the wrong permutation when selecting the k-th
result.
2026-07-29 21:19:50 +04:00
user eedf8d826a feat: add solution for smallest palindromic rearrangement ii
- add problem 3518 implementation
- add build configuration with debug and release modes
- add test cases and examples
- add makefile for problem-specific build workflow
2026-07-29 21:19:50 +04:00
6 changed files with 295 additions and 2 deletions
+46
View File
@@ -0,0 +1,46 @@
TARGET_NAME := smallest_palindromic_rearrangement_ii
PROBLEM_ID := $(notdir $(CURDIR))
BUILD_DIR := $(BUILD_ROOT)/$(PROBLEM_ID)
TARGET := $(BUILD_DIR)/$(TARGET_NAME)
SOURCES := \
main.cpp \
solution.cpp
OBJECTS := $(SOURCES:%.cpp=$(BUILD_DIR)/%.o)
all: build
build: $(TARGET)
$(TARGET): $(OBJECTS)
@mkdir -p $(BUILD_DIR)
$(CC) $(CFLAGS) $^ $(LDFLAGS) -o $@
$(BUILD_DIR)/%.o: %.cpp
@mkdir -p $(BUILD_DIR)
$(CC) $(CFLAGS) -c $< -o $@
run: build
$(TARGET)
test: run
clean:
rm -rf $(BUILD_DIR)
rebuild: clean build
.PHONY: all build run test clean rebuild
+74
View File
@@ -0,0 +1,74 @@
# 3518. Smallest Palindromic Rearrangement II
You are given a string `s` and an integer `k`.
Return the k-th palindromic of `s`. If there are fewer than `k` distinct palindromic permutations, return an empty string.
Note: Different rearrangements that yield the same palindromic string are considered identical and are counted once.
## Example 1
**Input:**
```text
s = "abba", k = 2
````
**Output:**
```text
"baab"
```
**Explanation:**
The two distinct palindromic rearrangements of `"abba"` are `"abba"` and `"baab"`.
Lexicographically, `"abba"` comes before `"baab"`. Since `k = 2`, the output is `"baab"`.
## Example 2
**Input:**
```text
s = "aa", k = 2
```
**Output:**
```text
""
```
**Explanation:**
There is only one palindromic rearrangement: `"aa"`.
The output is an empty string since `k = 2` exceeds the number of possible rearrangements.
## Example 3
**Input:**
```text
s = "bacab", k = 1
```
**Output:**
```text
"abcba"
```
**Explanation:**
The two distinct palindromic rearrangements of `"bacab"` are `"abcba"` and `"bacab"`.
Lexicographically, `"abcba"` comes before `"bacab"`. Since `k = 1`, the output is `"abcba"`.
## Constraints
* `1 <= s.length <= 104`
* `s` consists of lowercase English letters.
* `s` is guaranteed to be palindromic.
* `1 <= k <= 106`
+59
View File
@@ -0,0 +1,59 @@
#include "solution.cpp"
#include <cassert>
#include <iostream>
int main(int argc, char *argv[]) {
(void)argc;
(void)argv;
Solution s;
// Example 1
{
std::string result = s.smallestPalindrome("abba", 2);
assert(result == "baab");
}
// Example 2
{
std::string result = s.smallestPalindrome("aa", 2);
assert(result == "");
}
// Example 3
{
std::string result = s.smallestPalindrome("bacab", 1);
assert(result == "abcba");
}
// Additional tests
// Single palindrome
{
std::string result = s.smallestPalindrome("aaa", 1);
assert(result == "aaa");
}
// k exceeds number of permutations
{
std::string result = s.smallestPalindrome("aabb", 3);
assert(result == "");
}
// Two different characters
{
std::string result = s.smallestPalindrome("aabbcc", 1);
assert(result == "abccba");
}
// Already sorted palindrome
{
std::string result = s.smallestPalindrome("abcba", 2);
assert(result == "bacab");
}
std::cout << "All tests passed!\n";
return 0;
}
+43
View File
@@ -0,0 +1,43 @@
#include <algorithm>
#include <limits>
#include <set>
#include <stdexcept>
#include <string>
class Solution {
public:
std::string smallestPalindrome(std::string s, int k) {
std::string half = s.substr(0, s.size() / 2);
char middle = (s.size() % 2 == 1) ? s[s.size() / 2] : '\0';
std::ranges::sort(half);
std::set<std::string> permutations;
do {
permutations.insert(half);
} while (std::ranges::next_permutation(half).found);
if (permutations.size() > std::numeric_limits<int>::max()) {
throw std::overflow_error("Number of permutations exceeds int limit");
}
int size = static_cast<int>(permutations.size());
if (k > size) {
return std::string();
}
auto it = permutations.begin();
std::advance(it, k - 1);
std::string mirroredHalf = *it;
std::ranges::reverse(mirroredHalf);
if (middle != '\0') {
return *it + std::string(1, middle) + mirroredHalf;
}
return *it + mirroredHalf;
}
};
+45
View File
@@ -0,0 +1,45 @@
export ROOT_DIR := $(CURDIR)
export BUILD_ROOT := $(ROOT_DIR)/build
export CC = gcc
export LDFLAGS = -lstdc++
BUILD_TYPE ?= Release
ifeq ($(BUILD_TYPE),Debug)
export CFLAGS = -std=c++23 -Wall -Wextra -Werror -Wpedantic -g -O0
else ifeq ($(BUILD_TYPE),Release)
export CFLAGS = -std=c++23 -Wall -Wextra -Werror -Wpedantic -O2
else
$(error Unknown BUILD_TYPE: $(BUILD_TYPE))
endif
.PHONY: all clean build run test rebuild
all:
ifndef PROBLEM_ID
$(error PROBLEM_ID is not set)
endif
$(MAKE) -C $(PROBLEM_ID)
clean:
rm -rf $(BUILD_ROOT)
# NOTE: Examples
# make build PROBLEM_ID=3518 BUILD_TYPE=Debug
# make build PROBLEM_ID=3518
build:
mkdir -p $(BUILD_ROOT)/$(PROBLEM_ID)
$(MAKE) -C $(PROBLEM_ID) build
run:
$(MAKE) -C $(PROBLEM_ID) run
test:
$(MAKE) -C $(PROBLEM_ID) test
rebuild:
$(MAKE) -C $(PROBLEM_ID) clean
$(MAKE) -C $(PROBLEM_ID) build
+28 -2
View File
@@ -1,3 +1,29 @@
# leet-code
# LeetCode
Репозиторий с моими решениями задач из LeetCode на C++. Используется для практики алгоритмов и структур данных, а также отслеживания прогресса в решении задач различной сложности.
Репозиторий с моими решениями задач с LeetCode.
## Что внутри
* Решения задач, сгруппированные по номеру.
* Реализации на разных языках программирования — выбор языка зависит от конкретной задачи и целей.
* Практика изучения и применения возможностей современных стандартов C++, когда это целесообразно.
* Коммиты оформлены в соответствии с Conventional Commits.
## Цели
* Практика алгоритмов и структур данных.
* Подготовка к техническим собеседованиям.
* Изучение различных подходов к решению задач.
* Отслеживание собственного прогресса и эволюции решений.
## Структура
```text
<номер-задачи>/
├── solution.cpp
└── ...
```
Каждая директория соответствует одной задаче LeetCode и содержит её решение.
Репозиторий пополняется по мере решения новых задач.