Assign letter values 1 to 26 to maximize a string's total value

Quick Overview

Assign each lowercase letter a distinct value from 1 to 26 so that the total value of a given string is as large as possible. It tests frequency counting and proving that a greedy choice is optimal.

Assign letter values 1 to 26 to maximize a string's total value

Company: Bank of America

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

You are given a string `s` of lowercase English letters. Assign each of the 26 letters of the alphabet a unique integer value from 1 to 26, so that every value is used exactly once across all 26 letters. The beauty of `s` is the sum of the assigned values of all its characters. Return the maximum possible beauty of `s` when the assignment is chosen optimally. ### Function Signature ```python def maximum_beauty(s: str) -> int: ``` ### Rules - Every occurrence of a letter adds that letter's value, so a letter that appears 3 times adds 3 times its value. - Letters that do not appear in `s` still receive a value, but they add nothing to the beauty. ### Constraints - `1 <= len(s) <= 10^5` - `s` consists only of lowercase English letters. ### Examples ```text Input: s = "abbccc" Output: 152 ``` The counts are c = 3, b = 2 and a = 1. Giving c the value 26, b the value 25 and a the value 24 yields `3 * 26 + 2 * 25 + 1 * 24 = 78 + 50 + 24 = 152`. ```text Input: s = "zzzzzz" Output: 156 ``` Only z appears, 6 times. Giving it 26 yields `6 * 26 = 156`. ```text Input: s = "hello" Output: 124 ``` The letter l appears twice and h, e and o once each. Giving l the value 26 and the other three letters 25, 24 and 23 in any order yields `2 * 26 + 25 + 24 + 23 = 124`.

Overview: Assign each lowercase letter a distinct value from 1 to 26 so that the total value of a given string is as large as possible. It tests frequency counting and proving that a greedy choice is optimal.

Bank of America logo
Bank of America
Nov 24, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

You are given a string s of lowercase English letters. Assign each of the 26 letters of the alphabet a unique integer value from 1 to 26, so that every value is used exactly once across all 26 letters.

The beauty of s is the sum of the assigned values of all its characters. Return the maximum possible beauty of s when the assignment is chosen optimally.

Function Signature

def maximum_beauty(s: str) -> int:

Rules

  • Every occurrence of a letter adds that letter's value, so a letter that appears 3 times adds 3 times its value.
  • Letters that do not appear in s still receive a value, but they add nothing to the beauty.

Constraints

  • 1 <= len(s) <= 10^5
  • s consists only of lowercase English letters.

Examples

Input: s = "abbccc"
Output: 152

The counts are c = 3, b = 2 and a = 1. Giving c the value 26, b the value 25 and a the value 24 yields 3 * 26 + 2 * 25 + 1 * 24 = 78 + 50 + 24 = 152.

Input: s = "zzzzzz"
Output: 156

Only z appears, 6 times. Giving it 26 yields 6 * 26 = 156.

Input: s = "hello"
Output: 124

The letter l appears twice and h, e and o once each. Giving l the value 26 and the other three letters 25, 24 and 23 in any order yields 2 * 26 + 25 + 24 + 23 = 124.