Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package stringpermrecursion;
- import javax.swing.*;
- public class StringPermRecursion {
- public static void main(String[] args) {
- //ask for a string
- String input = JOptionPane.showInputDialog("Input a string:");
- //transfer the input to a string array
- char arrayStr[] = input.toCharArray();
- //pass the string, pointers to first and last element of string
- permutation( arrayStr , 0, input.length() - 1 );
- }
- public static void permutation( char[] arr, int strLeft, int strRight ){
- if( strLeft == strRight ){
- for(int i = 0; i < arr.length; i++){
- System.out.print( arr[ i ]);
- }
- System.out.print( "\n" );
- }
- else{
- for(int i = strLeft; i <= strRight; i++ ){
- swap( arr, strLeft, i );
- permutation( arr, strLeft+1, strRight );
- swap( arr, strLeft, i );
- }
- }
- }
- //swap two elements of the array
- public static void swap( char[] a, int l, int j ){
- char temp = a[l];
- a[l] = a[j];
- a[j] = temp;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement