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:
- The ticket code for the first ticket (ticket #1) is “A.”
- Now, to generate rth ticket you have to take an r-1 th ticket and create a new encoding by writing down the frequency followed by a digit.
- for example Ticket#(r-1) = “1121A” (Two 1, One 2, One 1, One A) so Ticket#(r) = “2112111A”
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:
- Initialize the ticket code as “A” for the first ticket (N = 1).
- Initialize an empty string newTicketCode to store the updated ticket code.
- Initialize a variable currentChar with the first character of the previous ticket code (initially, “A”).
- Initialize a variable charCount to keep track of consecutive characters and set it to 0.
- Iterate through the characters of the previous ticket code.
- For each character, check if it is the same as currentChar. If it is, increment charCount.
- If the character is different from currentChar, append the value of charCount and currentChar to the newTicketCode, effectively encoding the consecutive characters.
- Update currentChar to the new character and reset charCount to 1 for the new character.
- After iterating through the entire previous ticket code, append the last set of consecutive characters to newTicketCode.
- Update the ticket code for the current ticket to be the value of newTicketCode.
- After completing the loop for N iterations, you will have the Nth ticket code.
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 |
311A
Time Complexity: O(N)
Auxiliary Space: O(1)