@1337c0d3r Thank you for this! Appreciate.
Mark Yan
@markieff
Posts made by markieff

RE: Wrong test case: for test case ["wrtkj","wrt"], we are doing this based on "Alien" dictionary.

RE: Wrong test case: for test case ["wrtkj","wrt"], we are doing this based on "Alien" dictionary.
@cgxy1995 I got your point. But I think if we define a new dictionary, both lexicographical order and letter order should be included. Not just the order of all the letters.

RE: Wrong test case: for test case ["wrtkj","wrt"], we are doing this based on "Alien" dictionary.
@cgxy1995 please read what I wrote please, I have explained this.

JAVA 7ms one pass solution with details. Reverse part of the list
The idea to find out the permutation is like following:
we take 1,2,3,4,5 as example.
if assuming we have the array 12345 at beginning, then for these situations:
IIII: 12345
DDDD: 54321
DIDI: 21435
IIDD: 12543As you can see here, they can all be generated by 12345, if we meet 'I', we do nothing. If we meet 'D', we reverse the subarray which has the length: number of D + 1.
Here comes the code:
public int[] findPermutation(String s) { int[] res = new int[s.length()+1]; for (int i = 0; i < res.length; i++) res[i] = i+1; int start = 1, count = 0; for (int i = 0; i < s.length(); i++){ if (s.charAt(i) == 'I'){ if (start != 1) { reverse(res, start, count); start = 1; count = 0; } } else { if (start == 1) start = i; count++; } } if (start != 1) reverse(res, start, count); return res; } private void reverse(int[] res, int lo, int len) { int hi = lo + len; while(lo < hi) { int temp = res[lo]; res[lo] = res[hi]; res[hi] = temp; lo++; hi; } }

RE: one pass O(n) JAVA Solution, Simple and Clear
@apt275 The direct way to find the order is just write down these ten words: zero, one, two, ..., and find those have unique letters, it's pretty easy to find Cuz there are only ten words here.
Of course you can use 'v' for 5, then it will be count[5] = count[7]. The result is the same, so the order doesn't matter, as long as you calculate 5 after 7. 
Simple 2ms BFS Java solution
public int minMutation(String start, String end, String[] bank) { boolean[] visited = new boolean[bank.length]; Queue<String> queue = new LinkedList<>(); queue.add(start); int size = 1, res = 1; while(!queue.isEmpty()){ String temp = queue.poll(); for(int i = 0; i < bank.length; i++){ if (oneDistance(temp, bank[i]) && visited[i] == false){ if (bank[i].equals(end)) return res; queue.add(bank[i]); visited[i] = true; } } size; if (size == 0){ res++; size = queue.size(); } } return 1; } private boolean oneDistance(String str1, String str2) { int count = 0; for (int i = 0; i < str1.length(); i++){ if (str1.charAt(i) != str2.charAt(i)) count++; if (count > 1) return false; } return count == 1; }

RE: O(n) Easy to understand Java Solution
Nice Solution! Thanks for sharing. I think some part can be simplified. Here is my Java solution:
public String frequencySort(String s) { int[] count = new int[256]; StringBuilder sb = new StringBuilder(); List<List<Integer>> list = new ArrayList<>(); for (int i = 0; i < s.length(); i++) count[s.charAt(i)]++; for (int i = 0; i < s.length()+1; i++) list.add(new ArrayList<Integer>()); for (int i = 0; i < count.length; i++) if (count[i] != 0) list.get(count[i]).add(i); for (int i = list.size()1; i >= 0; i){ if (list.get(i) != null){ List<Integer> temp = list.get(i); for(int k = 0; k < temp.size(); k++){ for (int m = 0; m < i; m++){ sb.append(Character.toChars(temp.get(k))); } } } } return sb.toString(); }

O(n) Java Solution, simple and clear
public int minMoves(int[] nums) { if (nums == null  nums.length == 0) return 0; int minN = nums[0], res = 0; for (int num : nums) minN = Math.min(minN, num); for (int num : nums) res += (numminN); return res; }

RE: one pass O(n) JAVA Solution, Simple and Clear
@jdrogin Thanks man, one is enough : )