Email Chain Count - FreeCodeCamp #135 Daily Challenge

1 min

📨 Problem Statement

Given a string representing the subject of an email, determine how many times it was forwarded or replied to. We count as forwarded or replied if the string contains any of these prefixes (case-insensitive):

  • FWD:
  • FW:
  • RE:

🔎 Initial Analysis

The challenge is to identify and count how many times the typical reply or forward prefixes appear in an email subject.

Test Cases

SubjectOutputExplanation
RE: Meeting Tomorrow1Replied once
FWD: RE: Fw: Project Update3Two forwards, one reply
Project Update0No prefixes
fWd: rE: fW: Important Notice3Case-insensitive

🛠️ Solution Development

Approach

We’ll use a regular expression to search for the prefixes, leveraging case-insensitivity and global search.

Commented Code

function emailChainCount(subject) {
  // Search for FWD:, FW: or RE: prefixes, case-insensitive
  const regex = /(FWD:|FW:|RE:)/gi
  // Find all matches
  const matches = subject.match(regex)
  // Return the number of matches, or 0 if none
  return matches ? matches.length : 0
}

📊 Complexity Analysis

  • Time: O(n)O(n), where nn is the length of the string.
  • Space: O(k)O(k), where kk is the number of matches (typically small).

⚠️ Edge Cases and Considerations

  • Prefixes with spaces between letters and colon (e.g., FW :) do not count.
  • Nested or repeated prefixes (RE: RE: RE:) are all counted.
  • Prefixes in any position of the subject are valid.
  • Incomplete prefixes or with other characters (FW, RE-) do not count.
  • Counting is case-insensitive.

💡 Reflections and Learnings

  • Regular expressions for pattern matching.
  • Case-insensitivity with the i flag.
  • Native JavaScript string methods.

📚 Resources