TCS CodeVita Mock Test Coding Question 2
Coding Question 2
In this article, we will discuss about the TCS CodeVita Coding Question which is asked in the TCS placement test along with a detailed solution. This will help you excel your coding problems.

TCS CodeVita Coding Question
Problem Description
You are in process of developing a new language with the help of alphabet’s, number and special characters. You can decide your own sequence of these characters (alphabets, numbers, and special characters) while creating a new language.
Suppose, you have created a sequence as something like “Ikahgfswetimncx96345@#) (” and if the user enters any string like “Philacodist 2021” then your code should generate a new word with the help your new sequence by considering below rules:
- If the character of the string does not present in your sequence, then it should eliminate the character from the new word.
- If the character present in your sequence, then it should print as it is but you have to make sure it follows the same sequence, it means in the above example if the second character ‘h’ is present in the sequence but its position will not come second as fifth character ‘a’ will arrive before it as in our language sequence does matter.
Assumption:
- Your sequence should not have duplicate characters, if contains then should display “New language Error”
- Alphabets are not case sensitive
- If string contains duplicate characters, then there will no change in position, it will remain as their places.
Space is not a character, so include it in the same place as that in the string that to be translated. (Refer Examples section for more clarity).
Constraints
1<= S <=100
1<= C <=100
Input
First line contains a string denoting the sequence of characters of your language(S)
Second line contains a string denoting the sequence of characters to be transformed to your language(C)
Output
Print the converted string to your language, if possible, else print “New Language Error”
Time Limit (secs)
1
Examples
Example 1
Input
palskdjfieuryt93516247oh
Philacodist 2021
Output
palsdiitoh 122
#include<bits/stdc++.h> using namespace std; int main() { std::string languageSequence; std::getline(std::cin, languageSequence); std::string inputString; std::getline(std::cin, inputString); std::unordered_mapcharPosition; for (int i = 0; i < languageSequence.length(); ++i) { charPosition[languageSequence[i]] = i; } std::string result; for (char c : inputString) { if (std::isspace(c)) { result += c; } else { char charLower = std::tolower(c); if (charPosition.find(charLower) != charPosition.end()) { int charPos = charPosition[charLower]; result += languageSequence[charPos]; } } } bool hasDuplicates = std::unordered_set(result.begin(), result.end()).size() != result.length(); if (!hasDuplicates) { std::cout << result << std::endl; } else { std::cout << "New Language Error" << std::endl; } return 0; }
import java.util.HashMap; import java.util.Map; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); String languageSequence = scanner.nextLine().trim().toLowerCase(); String inputString = scanner.nextLine().trim(); MapcharPosition = new HashMap<>(); for (int i = 0; i < languageSequence.length(); i++) { charPosition.put(languageSequence.charAt(i), i); } StringBuilder result = new StringBuilder(); for (char c : inputString.toCharArray()) { if (Character.isWhitespace(c)) { result.append(c); } else { char charLower = Character.toLowerCase(c); if (charPosition.containsKey(charLower)) { int charPos = charPosition.get(charLower); result.append(languageSequence.charAt(charPos)); } else { } } } boolean hasDuplicates = result.chars().distinct().count() != result.length(); if (!hasDuplicates) { System.out.println(result.toString()); } else { System.out.println("New Language Error"); } } }
language_sequence = input().strip().lower() input_string = input().strip() char_position = {char: index for index, char in enumerate(language_sequence)} result = [] for char in input_string: if char.isspace(): result.append(char) else: char_lower = char.lower() if char_lower in char_position: char_pos = char_position[char_lower] result.append(language_sequence[char_pos]) has_duplicates = len(result) != len(set(result)) if not has_duplicates: print("".join(result)) else: print("New Language Error")
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
Login/Signup to comment