Skip to content

Commit

Permalink
Adding Java Code (#111)
Browse files Browse the repository at this point in the history
  • Loading branch information
spattk authored and MadhavBahl committed Jan 3, 2019
1 parent 0875503 commit b8e5f9f
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 0 deletions.
54 changes: 54 additions & 0 deletions day10/Java/StringPermutations.java
@@ -0,0 +1,54 @@
/**
* @date 03/01/19
* @author spattk (Sitesh Pattanaik)
*/

import java.util.*;

class StringPermutations {

static void printArr(char[] arr){
for(char x : arr){
System.out.print(x);
}
}

static void printAllPermutationsUtil(String str, boolean[] visited, char[] res, int index){

if(index==str.length())
{
printArr(res);
System.out.println();
return;
}

for(int i=0;i<str.length();i++)
{
if(visited[i]){
continue;
}
res[index] = str.charAt(i);
visited[i] = true;
printAllPermutationsUtil(str, visited, res, index+1);
visited[i] = false;
}
}

static void printAllPermutations(String str){
int n = str.length();
boolean[] visited = new boolean[n];
Arrays.fill(visited, false);
char[] res = new char[n];
printAllPermutationsUtil(str, visited, res, 0);
}

public static void main (String[] args) {
Scanner sc = new Scanner(System.in);
String str = sc.next();
char[] temp = str.toCharArray();
Arrays.sort(temp);
str = new String(temp);
printAllPermutations(str);
System.out.println();
}
}
60 changes: 60 additions & 0 deletions day10/README.md
Expand Up @@ -218,6 +218,66 @@ public class Permutation {
}
```

## Java Implementation

### [Solution](./Java/StringPermutations.java)

```java
/**
* @date 03/01/19
* @author spattk ( Sitesh Pattanaik )
*/
import java.util.*;

class StringPermutations {

static void printArr(char[] arr){
for(char x : arr){
System.out.print(x);
}
}

static void printAllPermutationsUtil(String str, boolean[] visited, char[] res, int index){

if(index==str.length())
{
printArr(res);
System.out.println();
return;
}

for(int i=0;i<str.length();i++)
{
if(visited[i]){
continue;
}
res[index] = str.charAt(i);
visited[i] = true;
printAllPermutationsUtil(str, visited, res, index+1);
visited[i] = false;
}
}

static void printAllPermutations(String str){
int n = str.length();
boolean[] visited = new boolean[n];
Arrays.fill(visited, false);
char[] res = new char[n];
printAllPermutationsUtil(str, visited, res, 0);
}

public static void main (String[] args) {
Scanner sc = new Scanner(System.in);
String str = sc.next();
char[] temp = str.toCharArray();
Arrays.sort(temp);
str = new String(temp);
printAllPermutations(str);
System.out.println();
}
}
```

## C++ Implementation

### [Solution](./C++/permutationday10.cpp)
Expand Down

0 comments on commit b8e5f9f

Please sign in to comment.