DEV Community

Abhinav Yadav
Abhinav Yadav

Posted on

Leetcode Solution #5

1. Unique Email Addresses

The Problem

Start by reading the description of the problem,

Every valid email consists of a local name and a domain name, separated by the '@' sign. Besides lowercase letters, the email may contain one or more '.' or '+'.

For example, in "alice@leetcode.com", "alice" is the local name, and "leetcode.com" is the domain name.
If you add periods '.' between some characters in the local name part of an email address, mail sent there will be forwarded to the same address without dots in the local name. Note that this rule does not apply to domain names.

For example, "alice.z@leetcode.com" and "alicez@leetcode.com" forward to the same email address.
If you add a plus '+' in the local name, everything after the first plus sign will be ignored. This allows certain emails to be filtered. Note that this rule does not apply to domain names.

For example, "m.y+name@email.com" will be forwarded to "my@email.com".
It is possible to use both of these rules at the same time.

Given an array of strings emails where we send one email to each emails[i], return the number of different addresses that actually receive mails.

The description might look big and scary but believe me question is
not that scary when it comes to the implementation, basically the question is saying

  1. An email is divide into two parts the part before @ is called local name, the part after @ is called domain name.

  2. In the question we have to handle following cases:

  • Dots (.) in the local name can be ignored.

  • Everything after the first plus sign (+) in the local name is discarded.

  • The domain name is not altered by dots or plus signs. It is used as is.

3.Two email addresses are considered the same if, after applying the above rules, they are identical.

4.You need to count how many unique addresses there are after applying these rules.

Looks easy isn't it.

Refer the example for better understanding,

Image description

Approach

Our approach for this question is going to be pretty straightforward,

  1. Find @ symbol divide the email in local name and domain name.
  2. Find '+' and '.' sign in local name and process it.
  3. Merge the local and domain name together and give the answer.

Solution

Image description

We have achieved this solution by following ways,

  1. Declare an unordered set unique to store the final answer.

  2. Traverse the string and find the @ symbol.

  3. When you find @ divide the part before @ as local name and part after that as domain name.

  4. Declare a variable processedLocal as string datatype.

  5. Traverse LocalName, if you find '+' symbol break because we have to ignore the values after + and if you find '.' symbol ignore it and add the rest of string in processedLocal variable.

  6. Declare a string final where we give merged email of processedLocal, @ and domain Name.

  7. Insert the final into the set unique.

  8. Return the size of unique.

2. Remove Element

The Problem

Start by reading the description of problem

Given an integer array nums and an integer val, remove all occurrences of val in nums in-place. The order of the elements may be changed. Then return the number of elements in nums which are not equal to val.

Consider the number of elements in nums which are not equal to val be k, to get accepted, you need to do the following things:

  • Change the array nums such that the first k elements of nums contain the elements which are not equal to val. The remaining elements of nums are not important as well as the size of nums.

  • Return k.

Question is pretty straight forward and easy we just have to remove a given element from an array and return the size of the array after removing it, the only catch is that the positions will change.

Refer the examples for better understanding,

Image description

Approach

The approach to solve this question is short and pretty straight forward,

  1. Iterate the array
  2. Check and move valid elements

Solution

Image description

Steps to achieve this solution is as follows,

  1. Initialise the variable index with 0 to keep track of position where next valid element should be placed.

  2. Iterate through the vector.

  3. Check if current element equal to value if not move it to position indicated by index and then increment the index to update next position.

  4. Return index.

3.Group Anagrams

The Problem

Start by reading the description

Given an array of strings strs, group the anagrams together. You can return the answer in any order.

Well, I this time I guess question is understandable by description only.

Refer to example for better understanding.

Image description

Approach

  1. Use hashmap to map each sorted string.
  2. Sort the string.
  3. Group by sorted string.
  4. Add groups to result.
  5. Return result.

Solution

Image description

  1. Initialise a result vector this will store final grouped anagram.

  2. Initialise a hash map to map sorted string to a vector of original strings that are anagrams of that key.

  3. Loop through each string in input vector.

  4. Sort the strings as it helps in identifying anagrams.

  5. Add original string to vector associated with its sorted version in hashmap. This groups all anagrams together under same key.

  6. Iterate through hashmap and add each group of anagrams to result vector.

  7. Return result.

I hope the solutions I have provided are understandable and explanations are easy to understand.

Thank You

You can connect with me on Linkedin

Top comments (0)