Change gender of string i.e toggle all the gender-specific words in the input string.
Input: “she is my sister” Output: “he is my brother”. There are two gender-specific words in this sentence:“she” and “sister”. After toggling gender specific words to their respective counterparts - “he” and “brother” : Gender specific words of the string are now changed.
- Maintain a hash-map which maps all the “female” words to the “male” words and all the “male” words to “female” ones.
- Then for each word in the string, we check whether this is a gender-specific word or not. If it is then we swap this word with its counterpart word. Else we don’t swap this word.
- All the words gets concatenated in a new string, which at the end is printed and is our required string.
Time Complexity: O(N^2), where N is the length of the string, as the ‘+’/ ‘append’ operator of the string can take upto O(N) time and assuming that lookup in the dictionary takes O(1) worse case time.
Auxiliary Space: Apart from the dictionary that maps all the words to its counterpart, we declare O(N) space for the new string, where N is the length of input string.
Scope for Improvement:
- We can add more words and their counterparts in the dictionary to increase the accuracy of the program. For example, we can add – “actor , actress” , “god , goddess” to our dictionary.
- A text file of words of all female and male words can also be imported.
- The program can be modified to become case insensitive.
This article is contributed by Rachit Belwariar . If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Change the given string according to the given conditions
- Change string to a new character set
- Zip function in Python to change to a new character set
- Check if the bracket sequence can be balanced with at most one change in the position of a bracket
- Given a string and an integer k, find the kth sub-string when all the sub-strings are sorted according to the given condition
- Restore original String from given Encrypted String by the given operations
- Check if a string can become empty by recursively deleting a given sub-string
- String containing first letter of every word in a given string with spaces
- Check if a given string is sum-string
- Lexicographically smallest string whose hamming distance from given string is exactly K
- Find if a string starts and ends with another given string
- Lexicographically smallest string formed by appending a character from the first K characters of a given string
- Find a palindromic string B such that given String A is a subsequense of B
- Minimum length of string having all permutation of given string.
- Count occurrences of a string that can be constructed from another given string
- Minimum number of given operations required to convert a string to another string
- Print the longest prefix of the given string which is also the suffix of the same string
- Queries to find the last non-repeating character in the sub-string of a given string
- Rearrange given string to maximize the occurrence of string t
- Longest string in an array which matches with prefix of the given string
Improved By : shubham_singh