Problem Statement:
Given a string, find the sum of the alphabetic weight of all substrings of the string that are palindromes. Return the answer modulo (10^9 + 7).
For each palindromic substring, its weight is the sum of its characters' positions in the alphabet. i.e., 'a' = 1, 'b' = 2, ..., 'z' = 26.
Note: A palindrome is a string that reads the same from the first character to the last and from the last character to the first.
Example 1:
Input:
s = "abcbd"
Output:
19
Problem Statement: Write a query to list all wallet addresses that have one or more transactions recorded. The output should display the wallet addresses in alphabetical order, including only those wallets whose IDs appear in the transactions table.
Schema: There are 2 tables:
Table: wallets
id (SMALLINT): Wallet ID
address (VARCHAR(64)): Wallet address
Table: transactions
wallet_id (SMALLINT): Wallet ID (Foreign key referencing wallets.id)
credit (DECIMAL(4,2)): Transaction amount
Problem1 Solution -:
Sum of Palindromic Substrings Weight Solution
Topics Involved / Prerequisites
Two Pointers (Expand Around Center)
Overview
Approach
1. Fast Weight Calculation (Prefix Sums)
Calculating the alphabetical weight of a substring repeatedly by looping through its characters is too slow. Instead, we can create a prefixWeight array. By converting each character into its weight (a=1, b=2, .. ) and keeping a running total, we can find the weight of any substring in O(1) constant time by simply subtracting the prefix value before the start index from the prefix value at the end index.
2. Expand Around Center
A palindrome mirrors around its center. There are (2N - 1) possible centers in a string (each character is a center for odd-length palindromes, and the space between each pair of adjacent characters is a center for even-length palindromes). We loop through all possible centers, expand our left and right pointers outward as long as the characters match, and add the O(1) substring weight to our total sum.
Time Complexity
Problem2 Solution
Wallet Transactions (SQL) Solution
Overview
We need to fetch wallet addresses but strictly filter for those wallets that have actively participated in at least one transaction.
Using an INNER JOIN seamlessly connects the two tables, while the DISTINCT keyword ensures we don't output the same wallet address multiple times.
Sorting the final result alphabetically with ORDER BY ensures the output matches the required formatting constraints.
Approach
1. Connecting the Tables
We only care about wallets that have a corresponding entry in the transactions table. An INNER JOIN on the wallet_id perfectly acts as a filter, automatically dropping any wallets from our result set that have zero transactions.
2. Removing Duplicates and Sorting
If a wallet has made five transactions, the INNER JOIN will initially generate five rows for that wallet address. By selecting DISTINCT w.address, we collapse those duplicates down to a single row. Finally, we append ORDER BY w.address ASC to sort them alphabetically.