File tree 3 files changed +78
-0
lines changed
3 files changed +78
-0
lines changed Original file line number Diff line number Diff line change
1
+ /*
2
+ * @author : YashMeh
3
+ * @date : 28/1/2019
4
+ */
5
+ #include < bits/stdc++.h>
6
+ using namespace std ;
7
+ // Using STL
8
+ void permute (string s)
9
+ {
10
+ sort (s.begin (),s.end ());
11
+ do
12
+ {
13
+ cout<<s<<endl;
14
+ } while (next_permutation (s.begin (),s.end ()));
15
+ }
16
+ // Using recursion
17
+ void permute (string smallPart,int n,string firstChar)
18
+ {
19
+ if (n==1 )
20
+ {
21
+ cout<<firstChar+smallPart<<endl;
22
+ return ;
23
+ }
24
+ for (int i=0 ;i<n;i++)
25
+ {
26
+ permute (smallPart.substr (1 ),n-1 ,firstChar+smallPart[0 ]);
27
+ rotate (smallPart.begin (),smallPart.begin ()+1 ,smallPart.end ());
28
+ }
29
+ }
30
+ main ()
31
+ {
32
+ string s=" 1234" ;
33
+ string r=" " ;
34
+ permute (" 1234" );
35
+ permute (s,s.size (),r);
36
+
37
+ }
Original file line number Diff line number Diff line change @@ -402,3 +402,44 @@ int main(){
402
402
return 0;
403
403
}
404
404
```
405
+ ### [Solution by @YashMeh](./C++/day10AllPermutations.cpp)
406
+
407
+ ```cpp
408
+ /*
409
+ * @author : YashMeh
410
+ * @date : 28/1/2019
411
+ */
412
+ #include<bits/stdc++.h>
413
+ using namespace std;
414
+ //Using STL
415
+ void permute(string s)
416
+ {
417
+ sort(s.begin(),s.end());
418
+ do
419
+ {
420
+ cout<<s<<endl;
421
+ } while (next_permutation(s.begin(),s.end()));
422
+ }
423
+ //Using recursion
424
+ void permute(string smallPart,int n,string firstChar)
425
+ {
426
+ if(n==1)
427
+ {
428
+ cout<<firstChar+smallPart<<endl;
429
+ return;
430
+ }
431
+ for(int i=0;i<n;i++)
432
+ {
433
+ permute(smallPart.substr(1),n-1,firstChar+smallPart[0]);
434
+ rotate(smallPart.begin(),smallPart.begin()+1,smallPart.end());
435
+ }
436
+ }
437
+ main()
438
+ {
439
+ string s="1234";
440
+ string r="";
441
+ permute("1234");
442
+ permute(s,s.size(),r);
443
+
444
+ }
445
+ ```
You can’t perform that action at this time.
0 commit comments