Package Com.leetcode;import Java.util.arraylist;public class permutation {public static void main (string[] args) { arraylist<string> res = PERMS2 ("abc"); System.out.println (res);} Law one: public static arraylist<string> perms1 (String s) {arraylist<string> res = new Arraylist<string> ( if (s = = null) return Null;if (S.isempty ()) {Res.add (""); return res;} for (int i = 0; i < s.length (); i++) {char c = s.charat (i); String start = s.substring (0, I); String end = s.substring (i + 1); arraylist<string> words = perms1 (start + end); for (string word:words) {string newstr = c + word;res.add (NEWSTR);}} return res;} Law II: public static arraylist<string> Perms2 (String s) {arraylist<string> res = new Arraylist<string> ( if (s = = null) return Null;if (S.isempty ()) {Res.add (""); return res;} char c = s.charat (0); String reminder = s.substring (1); arraylist<string> words = PERMS2 (reminder); for (String word:words) {for (int i = 0; I <= word.length (); i++) {Strin G newstr = InsErtcharat (Word, C, i); Res.add (NEWSTR);}} return res;} public static string Insertcharat (string s, char c, int i) {string start = s.substring (0, I); String end = S.substring (i); return start + C + end;}}
Java implements the full array of strings