Skip to content

Commit

Permalink
Added day10 cpp codes (#198)
Browse files Browse the repository at this point in the history
* Added day10 cpp codes

* Readme and code modified
  • Loading branch information
YashMeh authored and MadhavBahl committed Feb 8, 2019
1 parent f7fadeb commit c430e13
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 0 deletions.
Binary file added day10/C++/day10AllPermutations
Binary file not shown.
37 changes: 37 additions & 0 deletions day10/C++/day10AllPermutations.cpp
@@ -0,0 +1,37 @@
/*
* @author : YashMeh
* @date : 28/1/2019
*/
#include<bits/stdc++.h>
using namespace std;
//Using STL
void permute(string s)
{
sort(s.begin(),s.end());
do
{
cout<<s<<endl;
} while (next_permutation(s.begin(),s.end()));
}
//Using recursion
void permute(string smallPart,int n,string firstChar)
{
if(n==1)
{
cout<<firstChar+smallPart<<endl;
return;
}
for(int i=0;i<n;i++)
{
permute(smallPart.substr(1),n-1,firstChar+smallPart[0]);
rotate(smallPart.begin(),smallPart.begin()+1,smallPart.end());
}
}
main()
{
string s="1234";
string r="";
permute("1234");
permute(s,s.size(),r);

}
41 changes: 41 additions & 0 deletions day10/README.md
Expand Up @@ -402,3 +402,44 @@ int main(){
return 0;
}
```
### [Solution by @YashMeh](./C++/day10AllPermutations.cpp)
```cpp
/*
* @author : YashMeh
* @date : 28/1/2019
*/
#include<bits/stdc++.h>
using namespace std;
//Using STL
void permute(string s)
{
sort(s.begin(),s.end());
do
{
cout<<s<<endl;
} while (next_permutation(s.begin(),s.end()));
}
//Using recursion
void permute(string smallPart,int n,string firstChar)
{
if(n==1)
{
cout<<firstChar+smallPart<<endl;
return;
}
for(int i=0;i<n;i++)
{
permute(smallPart.substr(1),n-1,firstChar+smallPart[0]);
rotate(smallPart.begin(),smallPart.begin()+1,smallPart.end());
}
}
main()
{
string s="1234";
string r="";
permute("1234");
permute(s,s.size(),r);
}
```

0 comments on commit c430e13

Please sign in to comment.