HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

You need to diversify your strings

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
youryouneeddiversifystrings

Problem

Challenge:


Write a program which prints all the permutations of a string in alphabetical order.

Specifications:


Your program should accept a file as its first argument.

The file contains input strings, one per line.

Print the permutations of the string separated by commas, in alphabetical order.

We consider that digits < upper case letters < lower case letters.

The sorting should be performed in ascending order.

Solution:

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Arrays;
import java.util.Scanner;

public class PermutateString {
    public static void main(String[] args) throws FileNotFoundException {
        Scanner input = new Scanner(new File(args[0]));

        while (input.hasNextLine()) {
            System.out.println(
                permutateString(sortString(input.nextLine()))
            );
        }
        input.close();
    }

    private static void permutate(String prefix, String str, StringBuilder sb) {
        int len = str.length();

        if (len == 1) {
            sb.append(',').append(prefix).append(str);
        } else {
            for (int i = 0; i < len; i++) {
                permutate(prefix + str.charAt(i), str.substring(0, i) + str.substring(i + 1, len), sb);
            }
        }
    }

    private static String permutateString(String input) {
        StringBuilder characterBuilder = new StringBuilder();
        permutate("", input, characterBuilder);

        return characterBuilder.substring(1);
    }

    private static String sortString(String input) {
        char[] temp = input.toCharArray();
        Arrays.sort(temp);
        return String.valueOf(temp);
    }
}


Sample Input:

hat
abc
Zu6


Sample Output:

aht,ath,hat,hta,tah,tha
abc,acb,bac,bca,cab,cba
6Zu,6uZ,Z6u,Zu6,u6Z,uZ6


I first started solving this using a list of lists, but that required a lot of loops within loops and making size! lists seemed memory intensive..and confusing, This

Solution

Not much to comment on the permutation, but generally speaking you should use try-with-resources, Files.lines() and a healthy dose of Java 8 features for your main() method:

public static void main(String[] args) throws FileNotFoundException {
    try(Stream lines = Files.lines(Paths.get(args[0]))) {
        lines.map(PermutateString::sortString)
                .map(PermutateString::permutateString)
                .forEach(System.out::println);
    } catch (IOException e) {
        // do something with this, e.g. show file not found message?
    }
}


Please check the use of Paths.get() for your case too...

Code Snippets

public static void main(String[] args) throws FileNotFoundException {
    try(Stream<String> lines = Files.lines(Paths.get(args[0]))) {
        lines.map(PermutateString::sortString)
                .map(PermutateString::permutateString)
                .forEach(System.out::println);
    } catch (IOException e) {
        // do something with this, e.g. show file not found message?
    }
}

Context

StackExchange Code Review Q#97414, answer score: 4

Revisions (0)

No revisions yet.