String Construction

  • + 0 comments

    Java:

    public static int stringConstruction(String s) {
      boolean[] freqs = new boolean[26]; // a to z
      int cost = 0;
      for (char c : s.toCharArray()) {
        // If the character hasn't been added before, increase the cost
        if (!freqs[c - 'a']) {
          freqs[c - 'a'] = true;
          cost++;
        }
      }
      return cost;
    }