Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Print all permutations of string #64

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions Deadlock.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@


/*Java program to demonstrate a solution for Deadlock in Multi-threading */

public class Deadlock {

public static Object Resource1 = new Object(); /* Creating 2 objects in order to acquire lock */
public static Object Resource2 = new Object();

public static void main(String args[]) {

ThreadDemo1 T1 = new ThreadDemo1(); /* Creating 2 thread objects */
ThreadDemo2 T2 = new ThreadDemo2();

T1.start();
T2.start();
}

private static class ThreadDemo1 extends Thread { /* Illustration of thread by extending Thread class*/

public void run() {
synchronized (Resource1) {
System.out.println("Thread 1: Holding Resource 1..."); /* Thread 1 acquiring lock on Resource 1 */

try {
Thread.sleep(10);
} catch (InterruptedException e) {
}
System.out.println(
"Thread 1: Waiting for Resource 2..."); /*
* Thread 1 in waiting state while it is trying to
* acquire Resource 2 but already locked
*/

synchronized (Resource2) {
System.out
.println("Thread 1: Holding Resource 1 & 2..."); /* Thread 1 acquiring lock on Resource 2 */
}
}
}
}

private static class ThreadDemo2 extends Thread { /* Illustration of thread by extending Thread class*/
public void run() {
synchronized (Resource1) {
System.out.println("Thread 2: Holding Resource 1..."); /* Thread 2 acquiring lock on Resource 1 */

try {
Thread.sleep(10);
} catch (InterruptedException e) {
}
System.out.println(
"Thread 2: Waiting for Resource 2..."); /*
* Thread 2 in waiting state while it is trying to
* acquire Resource 2 but already locked
*/

synchronized (Resource2) {
System.out
.println("Thread 2: Holding Resource 1 & 2..."); /* Thread 2 acquiring lock on Resource 2 */
}
}
}
}
}
56 changes: 56 additions & 0 deletions Permutations.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@

/* Java program to obtain permutations of a given string */

public class Permutations {

/*
* Function for swapping the characters at position i with character at position
* j
*/

public static String swapString(String s, int i, int j) {

char[] b = s.toCharArray(); /* conversion of the string char array to perform swapping */

char ch;
ch = b[i];
b[i] = b[j];
b[j] = ch;
return String.valueOf(b);
}

/* Function for generating different permutations of the string */

public static void generatePermutation(String str, int start, int end) {

/* Prints the permutations */

if (start == end - 1)
System.out.println(str);
else {
for (int i = start; i < end; i++) {

/* Swapping the string by fixing a character */
str = swapString(str, start, i);

/*
* Recursively calling function generatePermutation() for rest of the characters
*/
generatePermutation(str, start + 1, end);

/* Backtracking and swapping the characters again. */
str = swapString(str, start, i);
}
}
}

public static void main(String[] args) {

String str = "BAD";
int len = str.length(); /* Find the length of the given string */

System.out.println("All the permutations of the string are: ");
generatePermutation(str, 0, len);
}

}