Change Gender of a given String


Difficulty Level Easy
Frequently asked in Adobe Factset
String

Problem Statement

In the “Change Gender of a given String” problem we have given a string “s”. Write a program that will toggle all the gender-specific words in the input string.

Input Format

The first and only one line containing a sentence or string with spaces “s”.

Output Format

Print the final updated sentence after toggle all the gender-specific words in the input string/sentence.

Constraints

  • 1<=|s|<=10^6
  • s[i] must be a lower case English alphabet or space ” “.

Example

He is my son
She is my daughter

Algorithm

1. Create a hash mapĀ  m1 that maps contain all the female words to male words.

2. Create another hash map m2 that maps contain all the male words to female words.

3. Traverse the string, for every word:

  • Check if the word is a gender-specific word or not, if it is a gender-specific word replace the word with the mapped word(counterpart word)

4. Print the new sentence.

Implementation

C++ Program for Change Gender of a given String

#include <bits/stdc++.h>
using namespace std;

int main()
{
  unordered_map<string,string> m1;
  m1["batman"]="batwoman";
  m1["boy"]="girl";
  m1["boyfriend"]="girlfriend";
  m1["father"]="mother";
  m1["husband"]="wife";
  m1["he"]="she";
  m1["his"]="her";
  m1["male"]="female";
  m1["man"]="woman";
  m1["Mr"]="Ms";
  m1["sir"]="madam";
  m1["son"]="daughter";
  m1["uncle"]="aunt";
  unordered_map<string,string> m2;
  for(auto u: m1)
  {
      m2[u.second]=u.first;
  }
  string s;
  getline(cin, s);
  string temp="";
  int n=s.length();
  for(int i=0;i<n;i++)
  {
      if(s[i]!=' ')
      {
          temp+=s[i];
      }
      else
      {
          if(m1[temp].size()>0)
          {
              cout<<m1[temp]<<" ";
          }
          else if(m2[temp].size()>0)
          {
              cout<<m2[temp]<<" ";
          }
          else
          {
              cout<<temp<<" ";
          }
          temp="";
      }
  }
  if(m1[temp].size()>0)
  {
      cout<<m1[temp]<<" ";
  }
  else if(m2[temp].size()>0)
  {
      cout<<m2[temp]<<" ";
  }
  else
  {
      cout<<temp<<" ";
  }
  temp="";
  return 0;
}

Java Program for Change Gender of a given String

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
class sum
{
    public static void main(String[] args)
    {
        Scanner sr = new Scanner(System.in);
        String s = sr.nextLine();
  int n = s.length();
        Map<String, String> m1 = new HashMap<String, String>();
  m1.put("batman", "batwoman");
  m1.put("boy","girl");
  m1.put("boyfriend","girlboyfriend");
  m1.put("father","mother");
  m1.put("husband","wife");
  m1.put("he","she");
  m1.put("his","her");
  m1.put("male","female");
  m1.put("man","woman");
  m1.put("Mr","Ms");
  m1.put("sir","madam");
  m1.put("son","daughter");
  m1.put("uncle","aunt");
        Map<String, String> m2 = new HashMap<String, String>();
  System.out.println(m1.keySet());
        System.out.println(m1.values());
        int sz=m1.size();
        for(Map.Entry<String, String> entry : m1.entrySet()) 
        {
            m2.put(entry.getKey(), entry.getValue());
        }
        String temp="";
  for(int i=0;i<n;i++)
  {
      if(s.charAt(i)!=' ')
      {
          temp+=s.charAt(i);
      }
      else
      {
                boolean flag1= m1.containsKey(temp);
                boolean flag2= m2.containsKey(temp);
          if(flag1)
          {
              System.out.print(m1.get(temp)+" ");
          }
          else if(flag2)
          {
              System.out.print(m2.get(temp)+" ");
          }
          else
          {
              System.out.print(temp+" ");
          }
          temp="";
      }
  }
        boolean flag1= m1.containsKey(temp);
        boolean flag2= m2.containsKey(temp);
  if(flag1)
        {
            System.out.print(m1.get(temp)+" ");
        }
        else if(flag2)
        {
            System.out.print(m2.get(temp)+" ");
        }
        else
        {
            System.out.print(temp+" ");
        }
    }
}
did you know that the female gender had a guardian
did you know that the male gender had a guardian

Complexity Analysis

Time Complexity

O(n) where n is the size of the given string “s”. Here we check one by one char and if its a gender-specific word then replace it with its opposite gender.

Space Complexity

O(n) because we firstly store a word into string then perform the desired operation.

Array Interview Questions
Graph Interview Questions
LinkedList Interview Questions
String Interview Questions
Tree Interview Questions
Core Java Interview Questions