Skip to content

Commit c430e13

Browse files
YashMehMadhavBahl
authored andcommitted
Added day10 cpp codes (#198)
* Added day10 cpp codes * Readme and code modified
1 parent f7fadeb commit c430e13

File tree

3 files changed

+78
-0
lines changed

3 files changed

+78
-0
lines changed

day10/C++/day10AllPermutations

37.4 KB
Binary file not shown.

day10/C++/day10AllPermutations.cpp

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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+
}

day10/README.md

+41
Original file line numberDiff line numberDiff line change
@@ -402,3 +402,44 @@ int main(){
402402
return 0;
403403
}
404404
```
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+
```

0 commit comments

Comments
 (0)