Skip to content

Commit d315fe5

Browse files
authored
Dictionary Order(Larger) Solution Added
1 parent f77ec8e commit d315fe5

File tree

1 file changed

+83
-0
lines changed

1 file changed

+83
-0
lines changed
+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/* Hacker Blocks */
2+
/* Title - Dictionary Order(Larger) */
3+
/* Created By - Akash Modak */
4+
/* Date - 14/07/2020 */
5+
6+
// Take as input str, a string. Write a recursive function which prints all the words possible by rearranging the characters of this string which are in dictionary order larger than the given string.
7+
// The output strings must be lexicographically sorted.
8+
9+
// Input Format
10+
// Single line input containing a string
11+
12+
// Constraints
13+
// Length of string <= 10
14+
15+
// Output Format
16+
// Display all the words which are in dictionary order larger than the string entered in a new line each. The output strings must be sorted.
17+
18+
// Sample Input
19+
// cab
20+
21+
// Sample Output
22+
// cba
23+
// Explanation
24+
// The possible permutations of string "cab" are "abc" , "acb" ,"bac" , "bca" , "cab" and "cba" . Only one of them is lexicographically greater than "cab". We only print "cba".
25+
26+
#include <iostream>
27+
#include<string.h>
28+
#include<set>
29+
#include<iterator>
30+
31+
using namespace std;
32+
33+
char temp[1000];
34+
35+
set<string>s;
36+
37+
void permute(string in, int i)
38+
{
39+
if(in[i]=='\0')
40+
{
41+
// cout<<in<<endl;
42+
43+
s.insert(in); //THIS IS NOT WORKING FINE
44+
45+
return;
46+
}
47+
48+
for(int j=i;in[j]!='\0';j++)
49+
{
50+
swap(in[i],in[j]);
51+
permute(in,i+1);
52+
swap(in[i],in[j]);
53+
}
54+
55+
56+
}
57+
58+
59+
int main()
60+
{
61+
string in;
62+
cin>>in;
63+
64+
string temp;
65+
66+
temp=in;
67+
68+
69+
permute(in,0);
70+
71+
set<string> :: iterator itr;
72+
73+
74+
for(itr=s.begin();itr!=s.end();itr++)
75+
{
76+
if(*itr>temp)
77+
{
78+
cout<<*itr<<endl;
79+
}
80+
}
81+
82+
return 0;
83+
}

0 commit comments

Comments
 (0)