Table of contents
1.
Introduction
2.
Problem Statement
3.
Approach
4.
FAQs
4.1.
Why do we use a StringBuilder in the above approach?StringBuilder is a mutable object of String. This means that we can update them. If we use String objects (immutable objects), then updating them would take O(N) time.
4.2.
What is the time and space complexity of the approach used?The time complexity is O(N) and the space complexity is O(1).
5.
Conclusion
Last Updated: Mar 27, 2024
Easy

The Constellation Problem - TCS CodeVita

Author NISHANT RANA
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

As we know, TCS hosts a yearly coding contest named TCS codevita. The great programmers take part in this contest. This contest can help you get great job opportunities at TCS. Getting a good rank at TCS codevita will also boost your resume.

In this blog, we will be solving one of the questions of this contest.

Problem Statement

Three characters { #, *, . } represents a constellation of stars and galaxies in space. Each galaxy is demarcated by # characters. There can be one or many stars in a given galaxy. Stars can only be in the shape of vowels { A, E, I, O, U }. A collection of * in the shape of the vowels is a star. A star is contained in a 3x3 block. Stars cannot be overlapping. The dot(.) character denotes empty space.

Given 3xN matrix comprising of { #, *, . } character, find the galaxy and stars within them.

Note: Please pay attention to how vowel A is denoted in a 3x3 block in the examples section below.

 

Constraints

3 <= N <= 10^5

 

Input

Input consists of a single integer N denoting the number of columns.

 

Output

The output contains vowels (stars) in order of their occurrence within the given galaxy. The galaxy itself is represented by the # character.

 

Time Limit

1 sec
 

Example

Input

18
*.*#***#***#***.*.
*.*#*.*#.*.#******
***#***#***#****.*

Output

U#O#I#EA

Explanation

As it can be seen that the stars make the image of the alphabets U, O, I, E, and A respectively.

Approach

As we can see in the sample example above, all the vowels are represented by a 3 * 3 grid. If you look closely, ‘A’, ‘U’, and ‘O’ have the unique 2nd column from all other vowels. Hence, we can identify them based on their second column.

Now, we are left with only ‘I’ and ‘E” which have the same 2nd column. We can compare them based on the first column. Please see the above example in which we can see that both ‘I’ and ‘E’ have different first columns.
 

Refer to the below implementation of the above approach:-

import java.util.*;
import java.io.*; 
  
public class Main {
	public static void main (String[] args) {
	    Scanner sc = new Scanner(System.in); 
		int n = sc.nextInt();
		char a[][] = new char[3][n];
		for(int i=0;i<3;i++) {
			a[i] = sc.next().toCharArray();
		}
		StringBuilder res = new StringBuilder();
		int i = 0;
		while(i<n) {
			if(a[0][i] == '#') {
				res.append("#");
				i++;
			}
			else {
				if(a[0][i+1]=='*' && a[1][i+1]=='*' && a[2][i+1]=='.') {
					res.append("A");
				}
				else if(a[0][i+1]=='.' && a[1][i+1]=='.' && a[2][i+1]=='*') {
					res.append("U");
				}
				else if(a[0][i+1]=='*' && a[1][i+1]=='.' && a[2][i+1]=='*') {
					res.append("O");
				}
				else {
					if(a[0][i]=='*' && a[1][i]=='.' && a[2][i]=='*') {
						res.append("I");
					}
					else {
						res.append("E");
					}
				}
				i += 3;
			}
		}
		System.out.println(res);
	}
}

Time Complexity: O(N)

We are simply running a while loop of O(N) time.

Space Complexity: O(1)

We are not using any auxiliary space.

Check out this article to find out everything about TCS NQT.

FAQs

Why do we use a StringBuilder in the above approach?
StringBuilder is a mutable object of String. This means that we can update them. If we use String objects (immutable objects), then updating them would take O(N) time.

What is the time and space complexity of the approach used?
The time complexity is O(N) and the space complexity is O(1).

Conclusion

In this article, we have extensively discussed the following things:

  1. We first discussed the problem statement of a TCS codevita contest.
  2. Then we discussed the approach and implementation to this TCS codevita problem.

Check out TCS Interview Experience to learn about TCS’s hiring process.

We hope that this blog has helped you enhance your knowledge regarding such coding problems from TCS codevita or any coding contest and if you would like to learn more, check out our articles here. You can attempt this problem here.

Do upvote our blog to help other ninjas grow. Happy Coding!







 

Live masterclass