/
githubmirror
/
interviews
Обзор
Документация
Войти
/
githubmirror
/
interviews
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
cracking-the-coding-interview/chapter-nine-recursion-and-dynamic-programming/AllPermutations.java
31 строка
881 B
Kevin Naughton Jr
finish renaming files and directories
27 мар 2018, 19:52
27 мар 2018, 19:52
ec6dfb5
Код
Авторство
О чём код?
/* write a method to compute all permutations of a string of unique characters */ public class AllPermutations { public static ArrayList<String> getPerms(String str) { if(str == null) { return null; } ArrayList<String> permutations = new ArrayList<String>(); if(str.length() == 0) { //base case permutations.add(""); return permutations; } char first = str.charAt(0); //get the first character String remainder = str.substring(1); //remove the 1st character ArrayList<String> words = getPerms(remainder); for(String word : words) { for(int j = 0; j <= word.length(); j++) { String s = insertCharAt(word, first, j); permutations.add(s); } } return permutations; } public static String insertCharAt(String word, char c, int i) { String start = word.substring(0, i); String end = word.substring(i); return start + c + end; } }