-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathbiggerIsGreater.c
57 lines (52 loc) · 1.51 KB
/
biggerIsGreater.c
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
47
48
49
50
51
52
53
54
55
56
57
#include <bits/stdc++.h>
#include <string.h>
using namespace std;
static string biggerIsGreater(string w)
{
char new_str[w.length()];
//An array to store the characters in the string
for (int i = 0; i < w.length(); i++)
new_str[i] = w[i];
int size_of_new = sizeof(new_str) / sizeof(new_str[0]);
int i = size_of_new - 1;
while (i > 0 && new_str[i - 1] >= new_str[i])
i--;
//If we reach the beginning without getting a single character that's less than the last character of the sorted sequence
//there is no possible permutation for the string
if (i <= 0)
return ("no answer");
int j = size_of_new - 1;
//Now we iterate forward until we get a character that's smaller than the last character in the sorted sequence
while (new_str[j] <= new_str[i - 1])
j--;
char temp = new_str[i - 1];
new_str[i - 1] = new_str[j];
new_str[j] = temp;
j = size_of_new - 1;
while (i < j)
{
temp = new_str[i];
new_str[i] = new_str[j];
new_str[j] = temp;
i++;
j--;
}
//this is to return the resultant permutation as a string
string new_s = "";
for (i = 0; i < size_of_new; i++)
new_s += new_str[i];
return (new_s);
}
int main()
{
int test_case;
cin >> test_case;
for (int i = 0; i < test_case; i++)
{
string s;
cin >> s;
cout<<(biggerIsGreater(s));
cout<<"\n";
}
return (0);
}