You are given a string s. The score of a string is defined as the sum of the absolute difference between the ASCII values of adjacent characters.
Return the score of s.
Example 1:
Input: s = "hello"
Output: 13
Explanation:
The ASCII values of the characters in s are: 'h' = 104, 'e' = 101, 'l' = 108, 'o' = 111. So, the score of s would be |104 - 101| + |101 - 108| + |108 - 108| + |108 - 111| = 3 + 7 + 0 + 3 = 13.
Example 2:
Input: s = "zaz"
Output: 50
Explanation:
The ASCII values of the characters in s are: 'z' = 122, 'a' = 97. So, the score of s would be |122 - 97| + |97 - 122| = 25 + 25 = 50.
Constraints:
2 <= s.length <= 100s consists only of lowercase English letters.When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
To find the 'score' of a string using brute force, we essentially try out every single possible arrangement or combination within the string. We will evaluate each of these arrangements based on the rules of the problem to find the best one. This involves a lot of checking and comparing to find the optimal solution.
Here's how the algorithm would work step-by-step:
def score_of_a_string_brute_force(input_string): number_of_characters = len(input_string)
maximum_score = 0
# Iterate through all possible substring starting positions
for substring_start_index in range(number_of_characters + 1):
# Iterate through all possible substring ending positions
for substring_end_index in range(substring_start_index + 1, number_of_characters + 1):
substring = input_string[substring_start_index:substring_end_index]
current_score = calculate_substring_score(substring)
# Keep track of the maximum score
if current_score > maximum_score:
maximum_score = current_score
return maximum_score
def calculate_substring_score(substring):
score = 0
for character in substring:
score += ord(character)
return scoreThe best way to solve this problem is to keep track of the count of each letter. We then iterate through the string and keep track of the score.
Here's how the algorithm would work step-by-step:
def score_of_string(input_string):
letter_counts = {}
for letter in input_string:
if letter in letter_counts:
letter_counts[letter] += 1
else:
letter_counts[letter] = 1
total_score = 0
# Iterate through each character in the string
for letter in input_string:
# Lookup count of the current letter
count_of_letter = letter_counts[letter]
# Calculate the score and add to total
total_score += (ord(letter) - ord('a') + 1) * count_of_letter
# The score is calculated after iterating.
return total_score| Case | How to Handle |
|---|---|
| Empty string input | Return 0 as an empty string has a score of zero according to the problem's implied base case. |
| Null string input | Throw an IllegalArgumentException or return 0, depending on the specified error handling behavior. |
| String with single parentheses like '(' or ')' | Throw an IllegalArgumentException or return an error value since the input is not balanced. |
| String with unbalanced parentheses, e.g., '(()' | Throw an IllegalArgumentException or return an error value to indicate invalid input format. |
| String with maximum allowed length (consider constraints) | Ensure the solution's space and time complexity are within acceptable bounds and don't cause stack overflow or memory issues. |
| Deeply nested parentheses, e.g., '((((((())))))))' | Verify that the recursion depth doesn't exceed limits or that iterative solutions don't become excessively slow due to large stack usage. |
| String with many consecutive '()' pairs, e.g., '()()()()()' | Confirm that repeated addition doesn't cause performance bottlenecks and that the score is calculated correctly. |
| String with complex nesting and concatenation, e.g., '(()(()))' | Validate correct precedence and evaluation order of nested and concatenated expressions. |