Skip to main content

Write a function to print all the possible permutations of a string. Now, modify the algorithm to discard duplicates.

Below is a sample implementation that prints all possible permutations of a string:
#include
#include
const int MAX_STR = 20;
void CopyStr(char *s2, char *s1, int i)
{
for (int j = 0, k = 0, len = strlen(s2); j < len; j++) {
if (i != j) {
s1[k++] = s2[j];
}
}
s1[k] = '\0';
}
void Permute(char *str1, char *str2)
{
int len = strlen(str1);
if (len == 1) {
//If you do not want to print duplicates, put the result in a
// hash and then print the hash table.
cout << str2 << str1 << "\n";
return;
}
for (int i = 0; i < len; i++) {
char currentChar = str1[i];
char str3[MAX_STR];
CopyStr(str1, str3, i);
int len2 = strlen(str2);
str2[len2] = currentChar;
str2[len2+1] = '\0';
Permute(str3, str2);
str2[len2] = '\0';
}
}
int main ()
{
char someStr[MAX_STR], someOtherStr[MAX_STR];
cout << "Enter a string(less then << MAX_STR << “characters:” ";
cin >> someStr;
someOtherStr[0] = '\0';
cout << "\n\nThe permutations of " << someStr <<" are: \n";
Permute(someStr, someOtherStr);
return 1;
}
There are other much more interesting and optimal ways to avoid printing duplicates. For example,
if the For loop kept a “currentChar history” as it went through the loop, it could skip the call to
Permute() if the character was already in its history, since it knows only duplicate permutations
would be generated. This is both faster and much more memory-efficient, since the total number of
permutations you’d have to store in the hash table grows exponentially with the length of the string
– try running this on even “abcdefghij” and you’ll get 10! = 3628800 permutations. At 10 bytes per
word, that is 36 MB of RAM, plus the hash table overhead. A 15-letter word would take over 13
terabytes! As you can see, a hash table answer is obvious, but clearly is not practical.

Comments

Popular posts from this blog

CKA Simulator Kubernetes 1.22

  https://killer.sh Pre Setup Once you've gained access to your terminal it might be wise to spend ~1 minute to setup your environment. You could set these: alias k = kubectl                         # will already be pre-configured export do = "--dry-run=client -o yaml"     # k get pod x $do export now = "--force --grace-period 0"   # k delete pod x $now Vim To make vim use 2 spaces for a tab edit ~/.vimrc to contain: set tabstop=2 set expandtab set shiftwidth=2 More setup suggestions are in the tips section .     Question 1 | Contexts Task weight: 1%   You have access to multiple clusters from your main terminal through kubectl contexts. Write all those context names into /opt/course/1/contexts . Next write a command to display the current context into /opt/course/1/context_default_kubectl.sh , the command should use kubectl . Finally write a second command doing the same thing into ...

OWASP Top 10 Threats and Mitigations Exam - Single Select

Last updated 4 Aug 11 Course Title: OWASP Top 10 Threats and Mitigation Exam Questions - Single Select 1) Which of the following consequences is most likely to occur due to an injection attack? Spoofing Cross-site request forgery Denial of service   Correct Insecure direct object references 2) Your application is created using a language that does not support a clear distinction between code and data. Which vulnerability is most likely to occur in your application? Injection   Correct Insecure direct object references Failure to restrict URL access Insufficient transport layer protection 3) Which of the following scenarios is most likely to cause an injection attack? Unvalidated input is embedded in an instruction stream.   Correct Unvalidated input can be distinguished from valid instructions. A Web application does not validate a client’s access to a resource. A Web action performs an operation on behalf of the user without checkin...