Skip to content

Instantly share code, notes, and snippets.

@Ifihan
Created April 18, 2025 22:47
Show Gist options
  • Save Ifihan/c10f141cd553add840c4b7639d9c3c11 to your computer and use it in GitHub Desktop.
Save Ifihan/c10f141cd553add840c4b7639d9c3c11 to your computer and use it in GitHub Desktop.
Count and Say

Question

Approach

I started with the base case: "1". Then for each step from 2 to n, I generated the next term by reading the previous string and counting consecutive identical digits. For each run of characters, I appended the count followed by the digit to build the next term. I repeated this until I reached the nth term.

Implementation

Complexities

  • Time: O(n * m)
  • Space: O(m) where m is the length of the final result string at n
image
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment