Alice and messages

2.8

13 votes
Algorithms, String, Basics of String Manipulation, Implementation, String Algorithms, Sets
Problem

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

1N105

1|s|10

Code snippets (also called starter code/boilerplate code)

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

Time Limit: 5
Memory Limit: 256
Source Limit:
Explanation

Given

  • N = 5
  • Strings are ["aaa", "hack", "zzz", "abcd", "szxp"]

Approach

There must be a minimum of 3 strings that Alice could have started with to end up with the given final array. Note that "aaa" and "zzz" are encodings of each other, so at least one of them must have been present in the original array. Similarly, at least one of "hack" and "szxp" must have been present. "abcd" does not have its encoding in the final array so it is guaranteed to be present in the original array and did not get encoded.

Here are all the 9 ways that Alice could have ended up with the final array:

  • Alice started with 5 strings: ["aaa", "hack", "zzz", "abcd", "szxp"] and none of them got encoded.
  • Alice started with 4 strings: ["aaa", "hack", "abcd", "szxp"] and only "aaa" got encoded to "zzz".
  • Alice started with 4 strings: ["zzz", "hack", "abcd", "szxp"] and only "zzz" got encoded to "aaa".
  • Alice started with 4 strings: ["zzz", "hack", "abcd", "aaa"] and only "hack" got encoded to "szxp".
  • Alice started with 4 strings: ["zzz", "szxp", "abcd", "aaa"] and only "szxp" got encoded to "hack".
  • Alice started with 3 strings: ["aaa", "szxp", "abcd"] with "aaa" and "szxp" getting encoded to "zzz" and "hack" respectively.
  • Alice started with 3 strings: ["zzz", "szxp", "abcd"] with "zzz" and "szxp" getting encoded to "aaa" and "hack" respectively.
  • Alice started with 3 strings: ["aaa", "hack", "abcd"] with "aaa" and "hack" getting encoded to "zzz" and "szxp" respectively.
  • Alice started with 3 strings: ["zzz", "hack", "abcd"] with "zzz" and "hack" getting encoded to "aaa" and "szxp" respectively.

The minimum number of strings Alice could have started with is 3, so the answer is 3.

Editor Image

?