0% found this document useful (0 votes)
40 views38 pages

Sorted Unique Permutation

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views38 pages

Sorted Unique Permutation

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 38

SORTED UNIQUE

PERMUTATION
TEST TIME ON MANACHER'S
ALGORITHM

URL:
https://forms.gle/aVqbiUfE2quWLoPQA

QR CODE:
Sorted Unique Permutation

INTRODUCTION
 In this programming problem, we are given a string str.

 We have to print the distinct sorted permutations of the


string elements that can be formed.
 Condition: The string may contain a character that will
arise more than one time.
 Permutation is rearranging all the elements of a set
based on a specific sequence or type. The set may be
ordered or may not be ordered.
Sorted Unique Permutation

Example
Input Output

RST RST, RTS, SRT, STR, TRS, TSR


Explanation:
 From this string total 3! = 6 permutations can be formed.

 Let’s fix R and find permutations from s and t. This will


give 2 strings RST, RTS.
 Similarly fixing S will give SRT, STR. And fixing T will
give TRS, TSR.
Sorted Unique Permutation

A few more examples


Input Output

ABC, ACB, BAC, BCA, CAB, CBA


BAC

AAB AAB, ABA, BAA

ABCD, ABDC, ACBD, ACDB, ADBC,

DBCA ADCB BACD, BADC, BCAD, BCDA,


BDAC, BDCA CABD, CADB, CBAD,
CBDA, CDAB, CDBA DABC, DACB,
DBAC, DBCA, DCAB, DCBA
Question: 01 Sorted Unique Permutation

Examples For Repetitions


Input Output

AAL AAL, ALA, LAA

Explanation:

 Length of Str: 3

 A is repeated 2 times: 3!/2 = 3


Sorted Unique Permutation

Algorithm
 Sort the string, if the input isn’t sorted already.

 Obtain the total number of permutations which can be


formed from that string.

 Print the sorted string and then loop for the number of
(permutations-1) times as 1st string is already printed.

 Find the next greater string.


Sorted Unique Permutation
import java.io.*; static int calculateTotal(char[] temp, int
import java.util.*; n) {
int f = factorial(n);
public class EthCOde { // Building HashMap to store frequencies
// Calculating factorial of a of
number // all characters.
static int factorial(int n) { HashMap<Character, Integer> hm =
int f = 1; new HashMap<Character, Integer>();
for (int i = 1; i <= n; i++) for (int i = 0; i < temp.length; i++) {
f = f * i; if (hm.containsKey(temp[i]))
return f; hm.put(temp[i], hm.get(temp[i]) + 1);
} else
// Method to print the array hm.put(temp[i], 1);
static void print(char[] temp) { }
for (int i = 0; i < temp.length; // Traversing hashmap and finding
i++) duplicate elements.
System.out.print(temp[i]); for (Map.Entry e : hm.entrySet()) {
System.out.println(); Integer x = (Integer)e.getValue();
}
// Method to find total number of
Sorted Unique Permutation

if (x > 1) { int min = i;


int temp5 = factorial(x); int j, x = temp[i - 1];
f = f / temp5; for (j = i + 1; j < temp.length; j+
} +)
} if ((temp[j] < temp[min]) &&
return f; (temp[j] > x))
} min = j;
static void nextPermutation(char[] temp) // Swapping the above found
{ characters.
// Start traversing from the end and char temp_to_swap;
// find position 'i-1' of the first temp_to_swap = temp[i - 1];
character temp[i - 1] = temp[min];
// which is greater than its successor. temp[min] = temp_to_swap;
int i; // Sort all digits from position
for (i = temp.length - 1; i > 0; i--) next to 'i-1'
if (temp[i] > temp[i - 1]) // to end of the string.
break; Arrays.sort(temp, i, temp.length);
// Finding smallest character after 'i- // Print the String
1' and print(temp);
// greater than temp[i-1] }
Sorted Unique Permutation

static void printAllPermutations(String public static void main(String[]


s) { args) {
// Sorting String //Input your string
char temp[] = s.toCharArray(); String s = "AAB";
Arrays.sort(temp); printAllPermutations(s);
// Print first permutation }
print(temp); }
// Finding the total permutations
int total = calculateTotal(temp,
temp.length);
for (int i = 1; i < total; i++)
nextPermutation(temp);
}
Sorted Unique Permutation

Complexity

Time Complexity : O(P*Q)

Where P is the size of the string and Q is the number of


permutations possible.

Auxiliary Space : O(n)


Question: Q03

If B5D means B is the father of D; B9D means B is the sister of D;B4D means B is the
brother of D; B3D means B is the wife of D. Which of the following means F is the mother of
K?
A.F3M5K
B.F5M3K
C.F9M4N3K
D.F10M43

Answer : A
Explanation :

F3M → F is the wife of M


M5K → M is the father of K
Therefore, F is the mother of K.
Question: Q04

A is the brother of B. B is the brother of C. D is the father of A. Based on these three


statements, which of the following statements cannot be definitely true ?
A.B is the brother of A.
B.B is the son of D.
C.A is the brother of C.
D.C is the brother of A.

Answer : D
Explanation :

ØA is the brother of B and B is the brother of C. So, C may be the brother or sister of A.
Question: Q05

In a family, there are six members A, B, C, D, E and F. A and B are married couple, A being
the male member. D is the only son of C, who is the brother of A. E is the sister of D. B is the
daughter-in law of F, whose husband has died. How is E related to C ?
A.Sister
B.Daughter
C.Cousin
D.Mother

Answer : B
Explanation :

ØA is a male and married to B. So, A is the husband and B is the wife. C is the brother of A.
D is the son of C. E. who is the sister of D will be the daughter of C. B is the daughter-in-law
of F whose husband has died means F is the mother of A. ØClearly. E is the daughter of C.
Explanation :
Question: Q06

There are six persons A. B, C, D, E and F. C is the sister of F. B is the brother of E's
husband. D is the father of A and grandfather of F. There are two fathers, three brothers and
a mother in the group. Who is the mother ?
A.A
B.B
C.C
D.E

Answer : D
Explanation :

ØD is father of A and grandfather of F. So, A is father of F.


ØThus. D and A are the two fathers. C is the sister of F So. C is the daughter of A.
ØSince there is only one mother, it is evident that E is the wife of A and hence the mother of
C and F.
ØSo, B is brother of A There are three brothers. So. F is the brother of C.
ØClearly, A is E's Husband
Question: Q07
Looking at a portrait of a man, Harsh said, "His mother is the wife of my father's son.
Brothers and sisters I have none." At whose portrait was Harsh looking?
A.His son
B.His cousin
C.His uncle
D.His nephew

Answer : A
Explanation :

ØSince Harsh has no brother or sister, so he is his father's only son. Now, wife
Of my father's son — my wife.
So, Harsh's wife is the man's mother or the man is Harsh's son
Question: Q03

Answer : D
Explanation :
Question: Q03

Answer : D
Explanation :
Question: Q03

Answer : D
Explanation :
Question: Q03

Answer : D
Explanation :
Question: Q03

Answer : D
Explanation :
Question: Q03

Answer : D
Explanation :
Question: Q03

Answer : D
Explanation :
THANK
YOU

+91 78150 [email protected] www.codemithra.com


95095

You might also like