snippets 0.1.0
Loading...
Searching...
No Matches
string_utils.hpp
Go to the documentation of this file.
1#ifndef STRING_UTILS_H
2#define STRING_UTILS_H
3
4#include <functional>
5#include <vector>
6#include <optional>
7#include <queue>
8#include <cassert>
9#include <unordered_map>
10#include <sstream>
11
12namespace hsc_snippets {
23 static std::vector<int> kmpSearch(const std::string &pat, const std::string &txt) {
24 const int M = static_cast<int>(pat.size());
25 const int N = static_cast<int>(txt.size());
26
27 // Longest Prefix Suffix (lps) array
28 std::vector<int> lps(M);
29 std::vector<int> result;
30
31 // Preprocess the pattern
32 int len = 0;
33 lps[0] = 0; // lps[0] is always 0
34 int i = 1;
35 while (i < M) {
36 if (pat[i] == pat[len]) {
37 len++;
38 lps[i] = len;
39 i++;
40 } else {
41 if (len != 0) {
42 len = lps[len - 1];
43 } else {
44 lps[i] = 0;
45 i++;
46 }
47 }
48 }
49
50 // Search the pattern in txt
51 i = 0; // index for txt[]
52 int j = 0; // index for pat[]
53 while (i < N) {
54 if (pat[j] == txt[i]) {
55 j++;
56 i++;
57 }
58
59 if (j == M) {
60 result.push_back(i - j);
61 j = lps[j - 1];
62 } else if (i < N && pat[j] != txt[i]) {
63 if (j != 0) {
64 j = lps[j - 1];
65 } else {
66 i = i + 1;
67 }
68 }
69 }
70
71 return result;
72 }
73
80 static bool isPalindrome(const std::string &s) {
81 assert(!s.empty());
82 const int n = static_cast<int>(s.size());
83 for (int i = 0; i < n / 2; i++) {
84 if (s[i] != s[n - 1 - i]) {
85 return false;
86 }
87 }
88 return true;
89 }
90
98 template<std::integral T>
99 std::string to_string(const std::vector<T> &vec) {
100 std::stringstream ss;
101 ss << "[";
102 for (size_t i = 0; i < vec.size(); ++i) {
103 ss << vec[i];
104 if (i < vec.size() - 1) {
105 // Check if it's not the last element
106 ss << ", ";
107 }
108 }
109 ss << "]";
110 return ss.str();
111 }
112
120 template<std::integral T>
121 std::string to_string(std::pair<T, T> p) {
122 std::stringstream ss;
123 ss << "{";
124 ss << p.first;
125 ss << ", ";
126 ss << p.second;
127 ss << "}";
128 return ss.str();
129 }
130
139 template<std::integral T>
140 std::string to_string(const std::vector<std::pair<T, T> > &vec) {
141 std::stringstream ss;
142 ss << "[";
143 for (size_t i = 0; i < vec.size(); ++i) {
144 ss << to_string(vec[i]);
145 if (i < vec.size() - 1) {
146 // Check if it's not the last element
147 ss << ", ";
148 }
149 }
150 ss << "]";
151 return ss.str();
152 }
153
162 static std::string to_string(const std::vector<std::string> &vec) {
163 std::stringstream ss;
164 ss << "[";
165 for (size_t i = 0; i < vec.size(); ++i) {
166 ss << vec[i];
167 if (i < vec.size() - 1) {
168 // Check if it's not the last element
169 ss << ", ";
170 }
171 }
172 ss << "]";
173 return ss.str();
174 }
175
183 template<std::integral T>
184 std::string to_string(const std::vector<std::vector<T> > &vec) {
185 std::stringstream ss;
186 ss << "[";
187 for (size_t i = 0; i < vec.size(); ++i) {
188 ss << to_string(vec[i]);
189 if (i < vec.size() - 1) {
190 // Check if it's not the last element
191 ss << ", ";
192 }
193 }
194 ss << "]";
195 return ss.str();
196 }
197
205 static std::vector<std::string> string_split(const std::string &s, char delimiter) {
206 std::vector<std::string> tokens;
207 std::string token;
208 std::istringstream tokenStream(s);
209 while (std::getline(tokenStream, token, delimiter)) {
210 tokens.push_back(token);
211 }
212 return tokens;
213 }
214
226 static std::string string_repeat(const std::string &s, size_t n) {
227 if (n == 0) return "";
228 if (n == 1) return s;
229 if (s.empty()) return "";
230
231 std::string result;
232 result.reserve(n * s.size());
233
234 size_t current_length = s.size();
235 while (result.size() + current_length <= n * s.size()) {
236 result += s;
237 }
238
239 // Append the remaining part to reach exactly n * s.size()
240 result += result.substr(0, (n * s.size()) - result.size());
241
242 return result;
243 }
244
255 static std::string string_repeat(char c, size_t n) {
256 if (n == 0) return "";
257 std::string result(n, c);
258 return result;
259 }
260
268 static std::string string_join(const std::vector<std::string> &strings, const std::string &delimiter) {
269 if (strings.empty()) return "";
270
271 std::ostringstream result;
272 auto iter = strings.begin();
273 result << *iter++; // Add the first element without the delimiter
274
275 while (iter != strings.end()) {
276 result << delimiter << *iter++;
277 }
278
279 return result.str();
280 }
281}
282
283#endif // STRING_UTILS_H
Definition big_integer.hpp:14
static std::string string_join(const std::vector< std::string > &strings, const std::string &delimiter)
Definition string_utils.hpp:268
static std::string string_repeat(const std::string &s, size_t n)
Definition string_utils.hpp:226
static bool isPalindrome(const std::string &s)
Definition string_utils.hpp:80
static std::vector< std::string > string_split(const std::string &s, char delimiter)
Definition string_utils.hpp:205
static std::vector< int > kmpSearch(const std::string &pat, const std::string &txt)
Definition string_utils.hpp:23
std::string to_string(const std::vector< T > &vec)
Definition string_utils.hpp:99