Given a string s
, find the number of substrings that start and end with the same character.
Example 1:
Input: s = "abcba"
Output: 7
Explanation:
The substrings that start and end with the same character are: "a", "b", "c", "b", "a", "bcb", "abcba".
Example 2:
Input: s = "aba"
Output: 4
Explanation:
The substrings that start and end with the same character are: "a", "b", "a", "aba".
Example 3:
Input: s = "aaaaa"
Output: 15
Constraints:
1 <= s.length <= 105
s
consists 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 solve this problem using brute force, we will check every single possible substring within the main string. We'll then count only those substrings that start and end with the same character. It's like checking every nook and cranny to find the ones that match our criteria.
Here's how the algorithm would work step-by-step:
def number_of_same_end_substrings_brute_force(input_string):
number_of_same_end_substrings = 0
string_length = len(input_string)
for start_index in range(string_length):
# Iterate through all possible starting positions
for end_index in range(start_index, string_length):
substring = input_string[start_index:end_index+1]
# We need to check if the substring is at least one character long
if len(substring) > 0:
if substring[0] == substring[-1]:
# Count the substrings that have the same start and end character
number_of_same_end_substrings += 1
return number_of_same_end_substrings
The most efficient way to find these substrings is to count how many times each character appears in the string. Then, we can use a simple formula to calculate the number of substrings that start and end with the same character. This avoids checking every possible substring.
Here's how the algorithm would work step-by-step:
def number_of_same_end_substrings(input_string):
character_counts = {}
for character in input_string:
if character in character_counts:
character_counts[character] += 1
else:
character_counts[character] = 1
total_substrings = 0
# Iterate through counts of each character
for character in character_counts:
count_of_character = character_counts[character]
# Use formula n * (n + 1) / 2 to calc substrings
substrings_with_char =
count_of_character * (count_of_character + 1) // 2
total_substrings += substrings_with_char
# The total number of same-end substrings
return total_substrings
Case | How to Handle |
---|---|
Null or empty input string | Return 0, as there are no substrings in an empty string. |
String with only one character | Return 1, since the entire string itself is a same-end substring. |
String with all identical characters (e.g., 'aaaa') | Handle efficiently by counting all substrings; the number of same-end substrings is N*(N+1)/2 where N is the length of the string. |
String with no matching same-end characters | The algorithm should return 0, signifying that there are no substrings that satisfy the condition (other than the whole string, if applicable). |
Very long string to test efficiency. | Ensure the solution avoids excessive memory allocation or quadratic time complexity for long strings. |
String with non-alphanumeric characters. | The algorithm should handle these characters correctly without causing errors, by using appropriate string comparison methods. |
String with unicode characters. | The algorithm should handle unicode characters correctly ensuring accurate indexing and substring comparisons. |
Case-sensitivity of the characters (e.g., 'a' vs. 'A'). | Specify in the problem whether matching characters are case-sensitive or case-insensitive, and implement the appropriate comparison accordingly. |