Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #Brute Force approach
- """
- Starting from "1", we build each term by reading the previous string and grouping consecutive identical characters.
- While scanning from left to right, we compare each character with its previous one; if they match we increase the count, and when they differ we append the count and the previous character to form the next sequence.
- """
- class Solution:
- def countAndSay(self, n: int) -> str:
- res = "1"
- for _ in range(n - 1):
- temp = ""
- count = 1
- for i in range(1, len(res)):
- if res[i] == res[i - 1]:
- count += 1
- else:
- temp += str(count) + res[i - 1]
- count = 1
- # Add last group
- temp += str(count) + res[-1]
- res = temp
- return res
Advertisement
Add Comment
Please, Sign In to add comment