Ninja's Gift

Hard
0/120
Average time to solve is 45m
profile
Contributed by
5 upvotes
Asked in companies
MicrosoftNagarro Software

Problem statement

Ninja is going to visit her friend Mico. He is thinking to gift her a string. But he came to know that Mico only likes the string 'S'. Ninja already has a string 'K'. Now, Ninja can change the string 'K' in some other string by choosing any character in one move and change all its occurrence with any other lowercase English character. He can do this several times.

Help Ninja to find if he can change his string 'K' to string 'S', which Mico likes.

Note:
Both 'S' and 'K' contain only lowercase English characters.
For Example:
K = "aabb" and S = "bbcc"
Now Ninja can do the following changes:
- Change ‘b’ to ‘c’ -> “aacc”
- Change ‘a’ to ‘b’ -> “bbcc”

Hence Ninja can give Mico the desired gift.
Detailed explanation ( Input/output format, Notes, Images )
Input Format
The first line of input contains an integer ‘T’ denoting the number of test cases to run. Then the test case follows.

The first line of each test case contains a string 'S'.

The next line of each test case contains a string 'K'.
Output Format
For each test case, if Ninja can convert K into S, return “True” else return “False”.
Note:
You don’t need to take any input or print anything. This already has been taken care of. Just implement the function.
Constraints:
1 <= T <= 5
1 <= |K|, |S| <= 10^5

Time Limit: 1 second
Sample Input 1:
2
aabcc
ccdaa
ninjas
coding
Sample Output 1:
True
False
Explanation For Sample Output 1:
Test case 1:

Here K=ccdaa and S=aabcc. Here we can change K into S by following conversions

Change ‘d’ to ‘b’ -> ccbaa

Change ‘a’ to ‘e’ -> ccbee

Change ‘c’ to ‘a’ -> aabee

Change ‘e’ to ‘c’-> aabcc

Test case 2:

It is not possible to change K to S by any number of conversions.
Sample Input 2
2
code
dope
acbz
acbza
Sample Output 2
True
False
Hint

 Think of mapping each character in ‘K’ to the character in ‘S’ at corresponding positions in ‘S’.

Approaches (1)
Hashing Approach

Algorithm:

 

  • First, check all the corner cases.
  • If K == S, then return true.
  • If the length of K != length of S, then return false.
  • Then check if S contains all lowercase characters. This can be checked by initializing an array of size 26. Iterate over the length of string S, and for each character increase the value at the corresponding index.
  • Then initialize an integer count = 0, and iterate through the length of the array.
  • If the value at the index is greater than or equal to 1, then increase the count by 1.
  • If the count is equal to 26, then return false.
  • Then initialize an unordered map, character to character let say, store.
  • Iterate i from 0 to length of string
    • If for char at i in K, there is no key value in store then update store[K[i]]=S[i].
    • Else if store[K[i]]!=S[i] then return false.
  • In all other cases return true.
Time Complexity

O(N), where ‘N’ is the size of string ‘K’.

 

Here we are iterating over the length of string ‘K’. Hence the complexity is O(N).

Space Complexity

O(1)

 

Constant space is used.

Code Solution
(100% EXP penalty)
Ninja's Gift
Full screen
Console