Open In App

Ticket Encoding Sequence

You are working at a ticketing company that generates unique ticket codes for various events. You have given a number N and your task is to print the Nth ticket code. The ticket codes are generated based on a specific encoding sequence. The encoding sequence follows the recursive formula as described below:

Examples:



Input: 4
Output: 311A
Explanation:

  • Ticket #1: “A”
  • Ticket #2: Use “A” => one A => “1A”
  • Ticket #3: Use “1A” => one 1 + one A => “111A”
  • Ticket #4: Use “111A” => three 1 + one A => “311A”

Input: 3
Output: 111A
Explanation:



  • Ticket #1: “A”
  • Ticket #2: Use “A” => one A => “1A”
  • Ticket #3: Use “1A” => one 1 + one A => “111A”

Approach: To solve the problem follow the below idea:

The approach uses a simple iteration to generate the Nth ticket code based on the encoding rules. It starts with the initial ticket code “A” and, for each subsequent ticket (from 2 to N), iterates through the characters in the previous ticket code. It counts consecutive characters and appends the count and character to a new string whenever the character changes.

Steps of this approach:

Below is the code implementation of the above idea:




// C++ code for the above approach:
#include <iostream>
#include <string>
 
using namespace std;
 
// Function to generate the ticket code
// for a given N
string generateTicketCode(int N)
{
    // Initialize the first ticket code as "A"
    string ticketCode = "A";
 
    for (int i = 2; i <= N; i++) {
        // Initialize a new ticket code
        string newTicketCode = "";
 
        // Initialize the current character
        char currentChar = ticketCode[0];
        // Initialize the character count
        int charCount = 0;
 
        // Loop through the characters in the
        // previous ticket code
        for (int j = 0; j < ticketCode.length(); j++) {
            if (ticketCode[j] == currentChar) {
                // Increment character count if
                // the character is the same
                charCount++;
            }
            else {
                // If a different character is
                // encountered, add the character
                // count and current character
                // to the new ticket code
                newTicketCode
                    += to_string(charCount) + currentChar;
 
                // Update the current character
                currentChar = ticketCode[j];
                // Reset character count to 1
                charCount = 1;
            }
        }
 
        // Add the character count and current
        // character to the new ticket code
        // for the last character group
        newTicketCode += to_string(charCount) + currentChar;
        // Update the ticket code for
        // the next iteration
        ticketCode = newTicketCode;
    }
 
    return ticketCode;
}
 
// Drivers code
int main()
{
    int N = 4;
 
    // Generate the ticket code for N
    string ticketCode = generateTicketCode(N);
 
    // Output the ticket code
    cout << ticketCode << endl;
 
    return 0;
}




public class TicketCodeGenerator {
 
    // Function to generate the ticket code for a given N
    static String generateTicketCode(int N) {
        // Initialize the first ticket code as "A"
        StringBuilder ticketCode = new StringBuilder("A");
 
        for (int i = 2; i <= N; i++) {
            // Initialize a new ticket code
            StringBuilder newTicketCode = new StringBuilder();
 
            // Initialize the current character
            char currentChar = ticketCode.charAt(0);
            // Initialize the character count
            int charCount = 0;
 
            // Loop through the characters in the previous ticket code
            for (int j = 0; j < ticketCode.length(); j++) {
                if (ticketCode.charAt(j) == currentChar) {
                    // Increment character count if the character is the same
                    charCount++;
                } else {
                    // If a different character is encountered, add the character
                    // count and current character to the new ticket code
                    newTicketCode.append(charCount).append(currentChar);
 
                    // Update the current character
                    currentChar = ticketCode.charAt(j);
                    // Reset character count to 1
                    charCount = 1;
                }
            }
 
            // Add the character count and current character to the new ticket code
            // for the last character group
            newTicketCode.append(charCount).append(currentChar);
            // Update the ticket code for the next iteration
            ticketCode = newTicketCode;
        }
 
        return ticketCode.toString();
    }
 
    // Driver code
    public static void main(String[] args) {
        int N = 4;
 
        // Generate the ticket code for N
        String ticketCode = generateTicketCode(N);
 
        // Output the ticket code
        System.out.println(ticketCode);
    }
}




# Function to generate the ticket code for a given N
def generate_ticket_code(N):
    # Initialize the first ticket code as "A"
    ticket_code = "A"
 
    for i in range(2, N + 1):
        # Initialize a new ticket code
        new_ticket_code = ""
 
        # Initialize the current character
        current_char = ticket_code[0]
        # Initialize the character count
        char_count = 0
 
        # Loop through the characters in the previous ticket code
        for j in range(len(ticket_code)):
            if ticket_code[j] == current_char:
                # Increment character count if the character is the same
                char_count += 1
            else:
                # If a different character is encountered, add the character
                # count and current character to the new ticket code
                new_ticket_code += str(char_count) + current_char
 
                # Update the current character
                current_char = ticket_code[j]
                # Reset character count to 1
                char_count = 1
 
        # Add the character count and current character to the new ticket code
        # for the last character group
        new_ticket_code += str(char_count) + current_char
        # Update the ticket code for the next iteration
        ticket_code = new_ticket_code
 
    return ticket_code
 
 
# Driver code
if __name__ == "__main__":
    N = 4
 
    # Generate the ticket code for N
    ticket_code = generate_ticket_code(N)
 
    # Output the ticket code
    print(ticket_code)




using System;
 
class Program
{
    // Function to generate the ticket code for a given N
    static string GenerateTicketCode(int N)
    {
        // Initialize the first ticket code as "A"
        string ticketCode = "A";
 
        for (int i = 2; i <= N; i++)
        {
            // Initialize a new ticket code
            string newTicketCode = "";
 
            // Initialize the current character
            char currentChar = ticketCode[0];
            // Initialize the character count
            int charCount = 0;
 
            // Loop through the characters in the
            // previous ticket code
            for (int j = 0; j < ticketCode.Length; j++)
            {
                if (ticketCode[j] == currentChar)
                {
                    // Increment character count if
                    // the character is the same
                    charCount++;
                }
                else
                {
                    // If a different character is
                    // encountered, add the character
                    // count and current character
                    // to the new ticket code
                    newTicketCode += charCount.ToString() + currentChar;
 
                    // Update the current character
                    currentChar = ticketCode[j];
                    // Reset character count to 1
                    charCount = 1;
                }
            }
 
            // Add the character count and current
            // character to the new ticket code
            // for the last character group
            newTicketCode += charCount.ToString() + currentChar;
            // Update the ticket code for
            // the next iteration
            ticketCode = newTicketCode;
        }
 
        return ticketCode;
    }
 
    // Drivers code
    static void Main()
    {
        int N = 4;
 
        // Generate the ticket code for N
        string ticketCode = GenerateTicketCode(N);
 
        // Output the ticket code
        Console.WriteLine(ticketCode);
 
        // Wait for user input before closing the console window
        Console.ReadLine();
    }
}




// Javascript code for the above approach
 
// Function to generate the ticket code for a given N
function generate_ticket_code(N) {
    // Initialize the first ticket code as "A"
    let ticket_code = "A"
 
    for (let i = 2; i < N + 1; i++) {
        // Initialize a new ticket code
        new_ticket_code = ""
 
        // Initialize the current character
        current_char = ticket_code[0]
        // Initialize the character count
        char_count = 0
 
        // Loop through the characters in the previous ticket code
        for (let j = 0; j < ticket_code.length; j++) {
            if (ticket_code[j] == current_char) {
                // Increment character count if the character is the same
                char_count += 1
            } else {
                // If a different character is encountered, add the character
                // count and current character to the new ticket code
                new_ticket_code += char_count + current_char
 
                // Update the current character
                current_char = ticket_code[j]
                // Reset character count to 1
                char_count = 1
            }
        }
 
        // Add the character count and current character to the new ticket
        // code for the last character group
        new_ticket_code += char_count + current_char
        // Update the ticket code for the next iteration
        ticket_code = new_ticket_code
    }
 
    return ticket_code
}
 
 
// Driver code
let N = 4
 
// Generate the ticket code for N
ticket_code = generate_ticket_code(N)
 
// Output the ticket code
console.log(ticket_code)
 
// This code is contributed by ragul21

Output
311A







Time Complexity: O(N)
Auxiliary Space: O(1)


Article Tags :