-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay49.cc
More file actions
46 lines (36 loc) · 1.05 KB
/
Day49.cc
File metadata and controls
46 lines (36 loc) · 1.05 KB
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/*
This problem was asked by Yahoo.
You are given a string of length N and a parameter k. The string can be manipulated by taking one of the first k letters and moving it to the end.
Write a program to determine the lexicographically smallest string that can be created after an unlimited number of moves.
For example, suppose we are given the string daily and k = 1. The best we can create in this case is ailyd.
*/
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
string getLexicographicallySmallestString(string s, int k)
{
if (k == 1)
{
string result = s;
for (int i = 1; i < s.size(); i++)
{
string rotated = s.substr(i) + s.substr(0, i);
result = min(result, rotated);
}
return result;
}
else
{
sort(s.begin(), s.end());
return s;
}
}
int main()
{
string s = "daily";
int k = 1;
string result = getLexicographicallySmallestString(s, k);
cout << "Lex smallest string: " << result << endl;
return 0;
}