Alice and messages
Practice
2.8 (13 votes)
Algorithms
String
Basics of string manipulation
Implementation
String algorithms
Sets
Problem
70% Success 2017 Attempts 20 Points 5s Time Limit 256MB Memory 1024 KB Max Code

Alice has K strings, each consisting only of lowercase letters that she wanted to send to her friend. To avoid anyone else from being able to see them, she encodes every string using the following rule:

  • For every letter x, if it is the ith letter of the alphabet starting from the left, replace it with the ith letter starting from the right. For example, the string 'abcd' would be encoded to 'zyxw'.

However, due to a bug, a certain number of messages (possibly none) were not encoded. You are given a list of N distinct strings containing all the original messages as well as the encoded messages. You do not know how many messages Alice originally started with.

Task

Determine the minimum possible value of K denoting the the number of messages Alice initially started with.

Example

Assumptions

  • N = 3
  • Strings are ["aaa", "zzz", "pqrs"]

Approach

As "aaa" and "zzz" are encodings of each other, so Alice could have started with either one of them initially and got the other one as a result of encoding it, or she could have started with both and neither of them got encoded. For "pqrs", as its encoded string does not exist in the final array, so it must have been there initially and did not get encoded. Thus, there are three choices:

  • Alice started with 2 strings ["aaa", "pqrs"] and "aaa" got encoded to "zzz" while "pqrs" did not get encoded.
  • Alice started with 2 strings ["zzz", "pqrs"] and "zzz" got encoded to "aaa" while "pqrs" did not get encoded.
  • Alice started with 3 strings ["aaa", "zzz", "pqrs"] and none of the strings got encoded.

The minimum number of strings Alice could have started with is 2, therefore the answer is 2.

Function description

Complete the function findMessages provided in the editor. This function takes the following 2 parameters and returns the required answer:

  • N: Represents the total number of strings in the final array
  • A: Represents the final array of strings with N elements

Input format

Note: This is the input format that you must use to provide custom input (available above the Compile and Test button).

  • The first line contains an integer N, denoting the number of strings present in the final array.
  • The second line contains N space-separated strings consisting of lowercase letters only.

Output format

Print a single line containing a single integer representing the minimum possible number of strings Alice could have had initially.

Constraints

\(1 \le N \le10^5 \)

\(1 \le |s| \le 10\)

Code snippets (also called starter code/boilerplate code)

This question has code snippets for C, CPP, Java, and Python.

Please login to use the editor

You need to be logged in to access the code editor

Loading...

Please wait while we load the editor

Loading...
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:20
84 votes
Tags:
ApprovedEasyOpen
Points:20
12 votes
Tags:
Ad-HocEasyString Manipulationapproved
Points:20
96 votes
Tags:
AlgorithmsBrute ForceEasyString Manipulationeasy