# GeeksforGeeks

A computer science portal for geeks

## Permutations of a string without repeatition

Home Forums Algorithms Permutations of a string without repeatition

Tagged:

This topic contains 8 replies, has 4 voices, and was last updated by  agrawal25 3 years, 3 months ago.

Viewing 9 posts - 1 through 9 (of 9 total)
• Author
Posts
• #73641

Anonymous

How to find all the permutations of a string such that two equal permutations appear only once.

That is all permutations should be unique

#96662

Shekhu
Participant

@Arpan,

If we do not have duplicate characters then there won’t have any repeated permutations, right?

Now, can you explain the problem for the strings where we duplicate characters like “ABCA”. All permutations of this string are:

ABCA

ACBA

BACA

BCAA

CBAA

CABA

#96663

Shekhu
Participant

Sorry, I missed some of them. All permutations are:

ABCA

ABAC

ACBA

ACAB

AACB

AABC

BACA

BAAC

BCAA

BCAA

BACA

BAAC

CBAA

CBAA

CABA

CAAB

CAAB

CABA

ABCA

ABAC

ACBA

ACAB

AACB

AABC

And got the problem also

#96664

Shekhu
Participant

Here is one simple solution:

Use the permutation function discussed in the post http://geeksforgeeks.org/?p=767.

Instead of just printing the string, store the string in a Hash Table say HT. If the string is already present in HT, then ignore it. Else print it.

#96666

Anonymous

May be this will help you >>>>

```
import java.util.SortedSet ;
import java.util.TreeSet ;
import java.util.Scanner ;
import java.util.Arrays ;

/**
*
* @author Ashu
* Recursive method to find all permutations of a String without any duplicates
*   (if exists).
*/
public final class PermutationRecursive
{
private SortedSet <String> set = new TreeSet <String> () ;

private void permuteString(String begStr, String endStr)
{
int length = endStr.length() ;
if (length == 1) set.add(begStr + endStr) ;
else
{
for (int i = 0 ; i < length ; i++)
{
String newStr = endStr.substring(0, i) + endStr.substring(i+1) ;
permuteString(begStr + endStr.charAt(i), newStr) ;
}
}
}

private void print(SortedSet <String> list)
{
int k = 0 ;
for (String str : list)
{
System.out.println(++k + " --> " + str) ;
}
System.out.println("Frequency of Permutations : " + k) ;
}

private void search(SortedSet <String> list, String sch)
{
int loc = Arrays.binarySearch(list.toArray(), sch) + 1 ;
System.out.println("String Found At :: " + loc) ;
}

public static void main(String[] args)
{
PermutationRecursive pR = new PermutationRecursive() ;
Scanner scan = new Scanner(System.in) ;

System.out.print("Enter a String :: ") ;
String str = scan.nextLine() ;

pR.permuteString("", str) ;
pR.print(pR.set) ;

System.out.print("Enter the String to be searched :: ") ;
String input = scan.nextLine() ;
scan.close() ;
pR.search(pR.set, input) ;
}
}
```

#96697

Anonymous

Coverage: algorithm construction, conditional statements, iterative statements

In mathematics, the notion of permutation is used with several slightly different meanings, all related to the act of permuting (rearranging) objects or values. Informally, a permutation of a set of objects is an arrangement of those objects into a particular order. See this link.

Your task is to create a program that accepts a set of number no wherein 0<n<=100 and number of subset (r) wherein 0<r<=n Compute the number of possible permutations of the subset from the set assuming that the values has no repetition, and order doesn’t matter.

Example:

n=5, r=3

result: 10

#96698

kasireddi
Participant
```class StringPermutation {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String input = "abb";
permutation(input, "");
}

private static void permutation(String input, String sofar) {
// TODO Auto-generated method stub

if (input.equals("")) {
System.out.printf("%s,", sofar);
}
for (int i = 0; i < input.length(); i++) {
char c = input.charAt(i);
if (input.indexOf(c, i + 1) != -1)
continue;
permutation(input.substring(0, i) + input.substring(i + 1), sofar+c);
}
}

}```

#96699

kasireddi
Participant
```class StringPermutation {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String input = "abb";
permutation(input, "");
}

private static void permutation(String input, String sofar) {
// TODO Auto-generated method stub

if (input.equals("")) {
System.out.printf("%s,", sofar);
}
for (int i = 0; i < input.length(); i++) {
char c = input.charAt(i);
if (input.indexOf(c, i + 1) != -1)
continue;
permutation(input.substring(0, i) + input.substring(i + 1), sofar+c);
}
}
}```

#96700

agrawal25
Participant

# include <iostream>

# include <string>

using namespace std;

int count=0;

void permun(string s1, string s2){

int n=s2.size();

if(s2.size()==2){

cout<<s1<<s2[0]<<s2[1]<<endl;

cout<<s1<<s2[1]<<s2[0]<<endl;

::count+=2;

}

else{

for(int i=0; i<n; i++){

int x;

x=s2.find(s2);

string s, s3;

s=s2;

s3=s.erase(x, 1);

permun(s1+s2, s3);

}

}

}

int main(){

string str;

cin>>str;

sort(str.begin(), str.end());

string fxd;

fxd= “”;

permun(fxd, str);

cout<<::count<<endl;

system(“pause”);

return 0;

}

Viewing 9 posts - 1 through 9 (of 9 total)

You must be logged in to reply to this topic.