From f56944b5aa9d4094c9a340fdd08a26dfe70cd976 Mon Sep 17 00:00:00 2001 From: Parth Shah Date: Fri, 12 Jan 2024 19:44:42 -0800 Subject: [PATCH 1/2] O(n) time to build hashmap and O(26) space for building Hashmap --- ...er of Steps to Make Two Strings Anagram.py | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 1347. Minimum Number of Steps to Make Two Strings Anagram/1347. Minimum Number of Steps to Make Two Strings Anagram.py diff --git a/1347. Minimum Number of Steps to Make Two Strings Anagram/1347. Minimum Number of Steps to Make Two Strings Anagram.py b/1347. Minimum Number of Steps to Make Two Strings Anagram/1347. Minimum Number of Steps to Make Two Strings Anagram.py new file mode 100644 index 0000000..7a06e86 --- /dev/null +++ b/1347. Minimum Number of Steps to Make Two Strings Anagram/1347. Minimum Number of Steps to Make Two Strings Anagram.py @@ -0,0 +1,37 @@ +""" +You are given two strings of the same length s and t. In one step you can choose any character of t and replace it with another character. + +Return the minimum number of steps to make t an anagram of s. + +An Anagram of a string is a string that contains the same characters with a different (or the same) ordering. + + + +Example 1: + +Input: s = "bab", t = "aba" +Output: 1 +Explanation: Replace the first 'a' in t with b, t = "bba" which is anagram of s. +Example 2: + +Input: s = "leetcode", t = "practice" +Output: 5 +Explanation: Replace 'p', 'r', 'a', 'i' and 'c' from t with proper characters to make t anagram of s. +Example 3: + +Input: s = "anagram", t = "mangaar" +Output: 0 +Explanation: "anagram" and "mangaar" are anagrams. + + +Constraints: + +1 <= s.length <= 5 * 104 +s.length == t.length +s and t consist of lowercase English letters only. +""" +class Solution: + def minSteps(self, s: str, t: str) -> int: + counter_s = collections.Counter(s) + counter_t = collections.Counter(t) + return sum([val - counter_t[key] if val > counter_t[key] else 0 for key,val in counter_s.items()]) \ No newline at end of file From 5d5504a1f58df19f5521c87ab556b7702354e076 Mon Sep 17 00:00:00 2001 From: Parth Shah Date: Sat, 13 Jan 2024 19:11:09 -0800 Subject: [PATCH 2/2] O(n) time and O(n) space using hashmap(Counter) --- ...657. Determine if Two Strings Are Close.py | 47 +++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 1657. Determine if Two Strings Are Close/1657. Determine if Two Strings Are Close.py diff --git a/1657. Determine if Two Strings Are Close/1657. Determine if Two Strings Are Close.py b/1657. Determine if Two Strings Are Close/1657. Determine if Two Strings Are Close.py new file mode 100644 index 0000000..34d8039 --- /dev/null +++ b/1657. Determine if Two Strings Are Close/1657. Determine if Two Strings Are Close.py @@ -0,0 +1,47 @@ +""" +Two strings are considered close if you can attain one from the other using the following operations: + +Operation 1: Swap any two existing characters. +For example, abcde -> aecdb +Operation 2: Transform every occurrence of one existing character into another existing character, and do the same with the other character. +For example, aacabb -> bbcbaa (all a's turn into b's, and all b's turn into a's) +You can use the operations on either string as many times as necessary. + +Given two strings, word1 and word2, return true if word1 and word2 are close, and false otherwise. + + + +Example 1: + +Input: word1 = "abc", word2 = "bca" +Output: true +Explanation: You can attain word2 from word1 in 2 operations. +Apply Operation 1: "abc" -> "acb" +Apply Operation 1: "acb" -> "bca" +Example 2: + +Input: word1 = "a", word2 = "aa" +Output: false +Explanation: It is impossible to attain word2 from word1, or vice versa, in any number of operations. +Example 3: + +Input: word1 = "cabbba", word2 = "abbccc" +Output: true +Explanation: You can attain word2 from word1 in 3 operations. +Apply Operation 1: "cabbba" -> "caabbb" +Apply Operation 2: "caabbb" -> "baaccc" +Apply Operation 2: "baaccc" -> "abbccc" + + +Constraints: + +1 <= word1.length, word2.length <= 105 +word1 and word2 contain only lowercase English letters. +""" +class Solution: + def closeStrings(self, word1: str, word2: str) -> bool: + count_word1 = collections.Counter(word1) + count_word2 = collections.Counter(word2) + counter1 = collections.Counter(count_word1.values()) + counter2 = collections.Counter(count_word2.values()) + return len(count_word1.keys() - count_word2.keys()) == 0 and len(counter1 - counter2) == 0 \ No newline at end of file