Tag Archives: permutation

Palindrome Permutation II

This one is from leetcode. Given a string s, return all the palindromic permutations (without duplicates) of it. Return an empty list if no palindromic permutation could be form. For example: Given s = “aabb”, return [“abba”, “baab”]. Given s = “abc”, return []. Solution. We should get string where each unique char is only… Read More »

Permutation II

https://leetcode.com/problems/permutations-ii/ Given a collection of numbers that might contain duplicates, return all possible unique permutations. For example, [1,1,2] have the following unique permutations: [1,1,2], [1,2,1], and [2,1,1]. Solution. Solution is inspired from here. This is a very good one. First we sort the array. We should set a a used[] array. When we use num[i], if… Read More »

Permutation, Subset, All group of Palindrome

Today, I found a great coding style for series problem, like permutation, subset and group of palindrome: public static void getPermutationUtil(String str, int start, List<List<String>> result, List<String> currResult) { if(start==str.length()) { ArrayList<String> curr = new ArrayList<String>(currResult); result.add(curr); } else { for(int i=start; i<str.length(); i++) { String currStr = str.substring(start, i+1); currResult.add(currStr); getPermutationUtil(str, i + 1, result,… Read More »