# Solving Leetcode Interviews in Seconds with AI: Sum of Scores of Built Strings


	# Introduction
	In this blog post, we will explore how to solve the LeetCode problem "2223" using AI. LeetCode is a popular platform for preparing for coding interviews, and with the help of AI tools like [Chatmagic](https://www.chatmagic.app), we can generate solutions quickly and efficiently - helping you pass the interviews and get the job offer without having to study for months.

	# Problem Statement
	> You are building a string s of length n one character at a time, prepending each new character to the front of the string. The strings are labeled from 1 to n, where the string with length i is labeled si.  For example, for s = "abaca", s1 == "a", s2 == "ca", s3 == "aca", etc.  The score of si is the length of the longest common prefix between si and sn (Note that s == sn). Given the final string s, return the sum of the score of every si.   Example 1:  Input: s = "babab" Output: 9 Explanation: For s1 == "b", the longest common prefix is "b" which has a score of 1. For s2 == "ab", there is no common prefix so the score is 0. For s3 == "bab", the longest common prefix is "bab" which has a score of 3. For s4 == "abab", there is no common prefix so the score is 0. For s5 == "babab", the longest common prefix is "babab" which has a score of 5. The sum of the scores is 1 + 0 + 3 + 0 + 5 = 9, so we return 9. Example 2:  Input: s = "azbazbzaz" Output: 14 Explanation:  For s2 == "az", the longest common prefix is "az" which has a score of 2. For s6 == "azbzaz", the longest common prefix is "azb" which has a score of 3. For s9 == "azbazbzaz", the longest common prefix is "azbazbzaz" which has a score of 9. For all other si, the score is 0. The sum of the scores is 2 + 3 + 9 = 14, so we return 14.    Constraints:  1 <= s.length <= 105 s consists of lowercase English letters.  

	# Explanation
	*   **Reverse the String:** Since we are prepending characters, reverse the original string to simplify prefix comparisons.
*   **Iterate and Compare Prefixes:** Iterate through the reversed string. In each iteration, extract the current prefix `si` and compare it with the full reversed string `sn` to find the longest common prefix.
*   **Sum the Scores:** Accumulate the lengths of the longest common prefixes to compute the final sum.

*   **Runtime Complexity:** O(n^2), where n is the length of the string.
*   **Storage Complexity:** O(n), where n is the length of the string (due to reversed string).

	
	# Code
	```python
	def sum_of_prefix_scores(s: str) -> int:
    """
    Calculates the sum of the score of every si.
    The score of si is the length of the longest common prefix between si and sn.

    Args:
        s: The final string.

    Returns:
        The sum of the score of every si.
    """
    n = len(s)
    reversed_s = s[::-1]  # Reverse the string

    total_score = 0
    for i in range(1, n + 1):
        prefix_i = reversed_s[:i]
        score = 0
        for j in range(min(len(prefix_i), len(reversed_s))):
            if prefix_i[j] == reversed_s[j]:
                score += 1
            else:
                break
        total_score += score

    return total_score
	```
			
