Like Fibonacci word, a Tribonacci word. is a specific sequence of digits. The Tribonacci word is formed by repeated concatenation in the same way that the Fibonacci word is formed by repeated addition. But unlike the fibonacci word, Tribonacci word is formed by repeated addition of last three terms and it has its first three terms different from each other.
In Tribonacci word, S(0) = 1, S(1) = 12, S(2) = 1213, S(3) = 1213121 ..... where S(n) = S(n-1) + S(n-2) + S(n-3) and + represents the concatenation of strings.
The task is to find nth Tribonacci word for a given number n.
Input : n = 4 Output : S(4) = 1213121121312 Input : n = 3 Output : S(3) = 1213121
Just like in program of Fibonacci word, we use the iterative concept of finding nth Fibonacci word here for finding nth Tribonacci word we can use the iterative concept. So for finding nth Tribonacci word we will take three string Sn_1, Sn_2 and Sn_3 which represent S(n-1), S(n-2) and S(n-3) respectively and on each iteration we will update tmp = Sn_3, Sn_3 = Sn_3 + Sn_2 + Sn_1, Sn_1 = Sn_2 and Sn_2 = tmp in this way we can find nth tribonacci word.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Longest Common Prefix using Word by Word Matching
- C program to find and replace a word in a File by another given word
- C program to Replace a word in a text by another given word
- Fibonacci Word
- Encoding a word into Pig Latin
- Second most repeated word in a sequence
- Minimum Word Break
- Possibility of a word from a given set of characters
- Length Of Last Word in a String
- Next word that does not contain a palindrome and has characters from first k
- Reverse every word of the string except the first and the last character
- Find the word with most anagrams in a given sentence
- Word Break Problem using Backtracking
- Print last character of each word in a string
- Capitalize the first and last character of each word in a string
- Remove the first and last character of each word in a string
- Print the first and last character of each word in a String
- Check if a word is present in a sentence
- Program to find the Encrypted word
- Find the first repeated word in a string
Improved By : Mithun Kumar