Interview 150

Custom Sort String

Problem
LC 791
File
interview150_LC791CustomSortString.java
Path
pkg5leetcode/interview150/interview150_LC791CustomSortString.java
Package
pkg5leetcode.interview150
Command
java pkg5leetcode/interview150/interview150_LC791CustomSortString.java

LeetCode solutions

There is no in-browser runner. This is the file from the curriculum, unchanged.

pkg5leetcode/interview150/interview150_LC791CustomSortString.java
1package pkg5leetcode.interview150;2 3/** LC 791 Custom Sort String */4public class interview150_LC791CustomSortString {5  static String customSortString(String order, String s) {6    int[] cnt = new int[26];7    for (char c : s.toCharArray()) cnt[c - 'a']++;8    StringBuilder sb = new StringBuilder();9    for (char c : order.toCharArray()) while (cnt[c-'a']-- > 0) sb.append(c);10    for (int i = 0; i < 26; i++) while (cnt[i]-- > 0) sb.append((char)('a'+i));11    return sb.toString();12  }13 14  public static void main(String[] args) {15    System.out.println(customSortString("cba","abcd"));16  }17}