Repeated String

Solutions Let’s call l the length of the string. Thus, we have copies = n / l copies of the string. Let’s call occ the number of occurrences of a in the string. Thus, we have at least occ * l occurrences of a. The extra number of a is calculated from the possible remainder of the first n / l characters in the string. So we calculate the number of a in the first n%l characters. [Read More]

Staircase

Solutions This exercise is very easy. At Coding Gym we experiment with such exercises to learn more. Some solutions follow. C++: 1 2 3 4 5 int N; cin >> N; for (auto i=1; i<=N; ++i) { cout << string(N-i, ' ') << string(i, '#') << endl; } C#: 1 2 3 int n = Convert.ToInt32(Console.ReadLine()); for (int i = 1; i <= n; i++) Console.WriteLine(new String(' ', n - i) + new String('#', i)); Python: [Read More]

Super Reduced String

Solutions A solution changes the string by removing same adjacent elements. Here is a C++ implementation: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #include <string> #include <iostream> #include <algorithm> using namespace std; int main() { string S; cin >> S; auto finder = [&]{ return adjacent_find(begin(S), end(S)); }; for (auto adjFind = finder(); adjFind != end(S); adjFind = finder()) { S. [Read More]

Tasty Meal

Solutions To solve the problem we can easily observe that swapping the first c with the last p minimizes the tastiness. The answer is \(0\) when either: there are not p there are not c the last p is before the first c For example: 1 pppccc Instead, if neither of the conditions above occur, we swap the first occurrence of c with the last occurrence of p and we proceed with computing the tastiness. [Read More]

The Product Name

Solutions Basically, each character \(out_i\) of the output string \(out\) occurs the least number of times at index \(i\) among all the input strings. We can count the occurrences of each letter of the alphabet (26) for each position of the input strings. Since all the strings have \(5\) characters, we have \(5\) frequency tables. Thus, we use constant additional storage - for instance, an array containing 5 arrays of 26 integers. [Read More]

Unique identifier

Solutions A brute force solution involves building the binary string one character at a time, starting from an empty string. At each step, it tries adding a 0 or a 1 and continues until the string reaches length n. When a full-length string is formed, it checks if it’s already in the given list. If it’s not, the function prints it and stops. This approach guarantees that the first missing string found is among the smallest in lexicographical order. [Read More]