Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit deb93df

Browse files
authored
Merge pull request #8 from 149ps/LC-problems
Lc problems
2 parents 3c83a0c + 5d5504a commit deb93df

File tree

2 files changed

+84
-0
lines changed

2 files changed

+84
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
"""
2+
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.
3+
4+
Return the minimum number of steps to make t an anagram of s.
5+
6+
An Anagram of a string is a string that contains the same characters with a different (or the same) ordering.
7+
8+
9+
10+
Example 1:
11+
12+
Input: s = "bab", t = "aba"
13+
Output: 1
14+
Explanation: Replace the first 'a' in t with b, t = "bba" which is anagram of s.
15+
Example 2:
16+
17+
Input: s = "leetcode", t = "practice"
18+
Output: 5
19+
Explanation: Replace 'p', 'r', 'a', 'i' and 'c' from t with proper characters to make t anagram of s.
20+
Example 3:
21+
22+
Input: s = "anagram", t = "mangaar"
23+
Output: 0
24+
Explanation: "anagram" and "mangaar" are anagrams.
25+
26+
27+
Constraints:
28+
29+
1 <= s.length <= 5 * 104
30+
s.length == t.length
31+
s and t consist of lowercase English letters only.
32+
"""
33+
class Solution:
34+
def minSteps(self, s: str, t: str) -> int:
35+
counter_s = collections.Counter(s)
36+
counter_t = collections.Counter(t)
37+
return sum([val - counter_t[key] if val > counter_t[key] else 0 for key,val in counter_s.items()])
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
"""
2+
Two strings are considered close if you can attain one from the other using the following operations:
3+
4+
Operation 1: Swap any two existing characters.
5+
For example, abcde -> aecdb
6+
Operation 2: Transform every occurrence of one existing character into another existing character, and do the same with the other character.
7+
For example, aacabb -> bbcbaa (all a's turn into b's, and all b's turn into a's)
8+
You can use the operations on either string as many times as necessary.
9+
10+
Given two strings, word1 and word2, return true if word1 and word2 are close, and false otherwise.
11+
12+
13+
14+
Example 1:
15+
16+
Input: word1 = "abc", word2 = "bca"
17+
Output: true
18+
Explanation: You can attain word2 from word1 in 2 operations.
19+
Apply Operation 1: "abc" -> "acb"
20+
Apply Operation 1: "acb" -> "bca"
21+
Example 2:
22+
23+
Input: word1 = "a", word2 = "aa"
24+
Output: false
25+
Explanation: It is impossible to attain word2 from word1, or vice versa, in any number of operations.
26+
Example 3:
27+
28+
Input: word1 = "cabbba", word2 = "abbccc"
29+
Output: true
30+
Explanation: You can attain word2 from word1 in 3 operations.
31+
Apply Operation 1: "cabbba" -> "caabbb"
32+
Apply Operation 2: "caabbb" -> "baaccc"
33+
Apply Operation 2: "baaccc" -> "abbccc"
34+
35+
36+
Constraints:
37+
38+
1 <= word1.length, word2.length <= 105
39+
word1 and word2 contain only lowercase English letters.
40+
"""
41+
class Solution:
42+
def closeStrings(self, word1: str, word2: str) -> bool:
43+
count_word1 = collections.Counter(word1)
44+
count_word2 = collections.Counter(word2)
45+
counter1 = collections.Counter(count_word1.values())
46+
counter2 = collections.Counter(count_word2.values())
47+
return len(count_word1.keys() - count_word2.keys()) == 0 and len(counter1 - counter2) == 0

0 commit comments

Comments
 (0)