Xem thảo luận
Cải thiện bài viết
Lưu bài viết
Xem thảo luận
Cải thiện bài viết
Lưu bài viết
Đọc
Examples:
Input : string1 : geeks string2 : forgeeks Output : eegks Explanation: The letters that are common between the two strings are e[2 times], g[1 time], k[1 time] and s[1 time]. Hence the lexicographical output is "eegks" Input : string1 : hhhhhello string2 : gfghhmh Output : hhh
Bàn luận
- Đưa ra hai chuỗi, in tất cả các ký tự phổ biến theo thứ tự từ vựng. Nếu không có chữ cái chung, in -1. Tất cả các chữ cái là chữ thường. & Nbsp;Counter[str] method, which contains characters of string as key and their frequencies as value.
- Vấn đề này có giải pháp hiện tại Vui lòng giới thiệu các ký tự phổ biến của hai chuỗi trong liên kết thứ tự bảng chữ cái. Chúng tôi sẽ giải quyết vấn đề này trong Python bằng cách sử dụng mô -đun thuộc tính và bộ sưu tập giao nhau. Cách tiếp cận là đơn giản,intersection [ a&b ] property.
- Chuyển đổi cả hai chuỗi thành kiểu dữ liệu từ điển bằng phương thức bộ đếm [STR], chứa các ký tự của chuỗi là khóa và tần số của chúng là giá trị.
- Bây giờ tìm các phần tử phổ biến giữa hai chuỗi sử dụng thuộc tính giao lộ [A & B].elements[] method of counter dictionary to expand list of keys by their frequency number of times.
- Kết quả cũng sẽ là một từ điển truy cập có các yếu tố phổ biến là các khóa và tần số phổ biến của chúng là giá trị.
Implementation:
Python3
Sử dụng các phần tử [] Phương thức từ điển truy cập để mở rộng danh sách các khóa theo số lần tần số của chúng.
Sắp xếp danh sách và kết hợp từng ký tự của danh sách đầu ra không có khoảng trống để in chuỗi kết quả.
from
collections
import
Counter
def
common[str1,str2]:
eegks0
eegks1
eegks2
eegks3
eegks0
eegks5
eegks2
eegks7
eegks0
eegks9
eegks2
from collections import Counter, defaultdict
from itertools import zip_longest
s1 = 'aebcdee'
s2 = 'aaeedfskm'
# Create a dictionary the value is 'list' and can append char in each 'list'
res = defaultdict[list]
# get count of each char
cnt1 = Counter[s1] # -> {'e': 3, 'a': 1, 'b': 1, 'c': 1, 'd': 1}
cnt2 = Counter[s2] # -> {'a': 2, 'e': 2, 'd': 1, 'f': 1, 's': 1, 'k': 1, 'm': 1}
# for appending chars in one step, we can zip count of chars in two strings,
# so Because maybe two string have different length, we can use 'itertools. zip_longest'
for a,b in zip_longest[cnt1 , cnt2]:
# list[zip_longest[cnt1 , cnt2]] -> [['a', 'a'], ['e', 'e'], ['b', 'd'],
# ['c', 'f'], ['d', 's'], [None, 'k'],
# [None, 'm']]
# Because maybe we have 'none', before 'append' we need to check 'a' and 'b' don't be 'none'
if a: res[a].append[cnt1[a]]
if b: res[b].append[cnt2[b]]
# res -> {'a': [1, 2], 'e': [3, 2], 'b': [1], 'd': [1, 1], 'c': [1], 'f': [1], 's': [1], 'k': [1], 'm': [1]}
# If the length 'list' of each char is larger than one so this char is duplicated and we repeat this char in the result base min of each char in the 'list' of count char of two strings.
out = ''.join[k* min[v] for k,v in res.items[] if len[v]>1]
print[out]
# aeed
1s1 = 'aebcdee'
s2 = 'aaeedfskm'
s3 = 'aaeeezzxx'
res = defaultdict[list]
cnt1 = Counter[s1]
cnt2 = Counter[s2]
cnt3 = Counter[s3]
for a,b,c in zip_longest[cnt1 , cnt2, cnt3]:
if a: res[a].append[cnt1[a]]
if b: res[b].append[cnt2[b]]
if c: res[c].append[cnt3[c]]
out = ''.join[k* min[v] for k,v in res.items[] if len[v]>1]
print[out]
# aeed
0s1 = 'aebcdee'
s2 = 'aaeedfskm'
s3 = 'aaeeezzxx'
res = defaultdict[list]
cnt1 = Counter[s1]
cnt2 = Counter[s2]
cnt3 = Counter[s3]
for a,b,c in zip_longest[cnt1 , cnt2, cnt3]:
if a: res[a].append[cnt1[a]]
if b: res[b].append[cnt2[b]]
if c: res[c].append[cnt3[c]]
out = ''.join[k* min[v] for k,v in res.items[] if len[v]>1]
print[out]
# aeed
7____10
from collections import Counter, defaultdict
from itertools import zip_longest
s1 = 'aebcdee'
s2 = 'aaeedfskm'
# Create a dictionary the value is 'list' and can append char in each 'list'
res = defaultdict[list]
# get count of each char
cnt1 = Counter[s1] # -> {'e': 3, 'a': 1, 'b': 1, 'c': 1, 'd': 1}
cnt2 = Counter[s2] # -> {'a': 2, 'e': 2, 'd': 1, 'f': 1, 's': 1, 'k': 1, 'm': 1}
# for appending chars in one step, we can zip count of chars in two strings,
# so Because maybe two string have different length, we can use 'itertools. zip_longest'
for a,b in zip_longest[cnt1 , cnt2]:
# list[zip_longest[cnt1 , cnt2]] -> [['a', 'a'], ['e', 'e'], ['b', 'd'],
# ['c', 'f'], ['d', 's'], [None, 'k'],
# [None, 'm']]
# Because maybe we have 'none', before 'append' we need to check 'a' and 'b' don't be 'none'
if a: res[a].append[cnt1[a]]
if b: res[b].append[cnt2[b]]
# res -> {'a': [1, 2], 'e': [3, 2], 'b': [1], 'd': [1, 1], 'c': [1], 'f': [1], 's': [1], 'k': [1], 'm': [1]}
# If the length 'list' of each char is larger than one so this char is duplicated and we repeat this char in the result base min of each char in the 'list' of count char of two strings.
out = ''.join[k* min[v] for k,v in res.items[] if len[v]>1]
print[out]
# aeed
3 from collections import Counter, defaultdict
from itertools import zip_longest
s1 = 'aebcdee'
s2 = 'aaeedfskm'
# Create a dictionary the value is 'list' and can append char in each 'list'
res = defaultdict[list]
# get count of each char
cnt1 = Counter[s1] # -> {'e': 3, 'a': 1, 'b': 1, 'c': 1, 'd': 1}
cnt2 = Counter[s2] # -> {'a': 2, 'e': 2, 'd': 1, 'f': 1, 's': 1, 'k': 1, 'm': 1}
# for appending chars in one step, we can zip count of chars in two strings,
# so Because maybe two string have different length, we can use 'itertools. zip_longest'
for a,b in zip_longest[cnt1 , cnt2]:
# list[zip_longest[cnt1 , cnt2]] -> [['a', 'a'], ['e', 'e'], ['b', 'd'],
# ['c', 'f'], ['d', 's'], [None, 'k'],
# [None, 'm']]
# Because maybe we have 'none', before 'append' we need to check 'a' and 'b' don't be 'none'
if a: res[a].append[cnt1[a]]
if b: res[b].append[cnt2[b]]
# res -> {'a': [1, 2], 'e': [3, 2], 'b': [1], 'd': [1, 1], 'c': [1], 'f': [1], 's': [1], 'k': [1], 'm': [1]}
# If the length 'list' of each char is larger than one so this char is duplicated and we repeat this char in the result base min of each char in the 'list' of count char of two strings.
out = ''.join[k* min[v] for k,v in res.items[] if len[v]>1]
print[out]
# aeed
4from collections import Counter, defaultdict
from itertools import zip_longest
s1 = 'aebcdee'
s2 = 'aaeedfskm'
# Create a dictionary the value is 'list' and can append char in each 'list'
res = defaultdict[list]
# get count of each char
cnt1 = Counter[s1] # -> {'e': 3, 'a': 1, 'b': 1, 'c': 1, 'd': 1}
cnt2 = Counter[s2] # -> {'a': 2, 'e': 2, 'd': 1, 'f': 1, 's': 1, 'k': 1, 'm': 1}
# for appending chars in one step, we can zip count of chars in two strings,
# so Because maybe two string have different length, we can use 'itertools. zip_longest'
for a,b in zip_longest[cnt1 , cnt2]:
# list[zip_longest[cnt1 , cnt2]] -> [['a', 'a'], ['e', 'e'], ['b', 'd'],
# ['c', 'f'], ['d', 's'], [None, 'k'],
# [None, 'm']]
# Because maybe we have 'none', before 'append' we need to check 'a' and 'b' don't be 'none'
if a: res[a].append[cnt1[a]]
if b: res[b].append[cnt2[b]]
# res -> {'a': [1, 2], 'e': [3, 2], 'b': [1], 'd': [1, 1], 'c': [1], 'f': [1], 's': [1], 'k': [1], 'm': [1]}
# If the length 'list' of each char is larger than one so this char is duplicated and we repeat this char in the result base min of each char in the 'list' of count char of two strings.
out = ''.join[k* min[v] for k,v in res.items[] if len[v]>1]
print[out]
# aeed
5eegks2
eegks2
from collections import Counter, defaultdict
from itertools import zip_longest
s1 = 'aebcdee'
s2 = 'aaeedfskm'
# Create a dictionary the value is 'list' and can append char in each 'list'
res = defaultdict[list]
# get count of each char
cnt1 = Counter[s1] # -> {'e': 3, 'a': 1, 'b': 1, 'c': 1, 'd': 1}
cnt2 = Counter[s2] # -> {'a': 2, 'e': 2, 'd': 1, 'f': 1, 's': 1, 'k': 1, 'm': 1}
# for appending chars in one step, we can zip count of chars in two strings,
# so Because maybe two string have different length, we can use 'itertools. zip_longest'
for a,b in zip_longest[cnt1 , cnt2]:
# list[zip_longest[cnt1 , cnt2]] -> [['a', 'a'], ['e', 'e'], ['b', 'd'],
# ['c', 'f'], ['d', 's'], [None, 'k'],
# [None, 'm']]
# Because maybe we have 'none', before 'append' we need to check 'a' and 'b' don't be 'none'
if a: res[a].append[cnt1[a]]
if b: res[b].append[cnt2[b]]
# res -> {'a': [1, 2], 'e': [3, 2], 'b': [1], 'd': [1, 1], 'c': [1], 'f': [1], 's': [1], 'k': [1], 'm': [1]}
# If the length 'list' of each char is larger than one so this char is duplicated and we repeat this char in the result base min of each char in the 'list' of count char of two strings.
out = ''.join[k* min[v] for k,v in res.items[] if len[v]>1]
print[out]
# aeed
8from collections import Counter, defaultdict
from itertools import zip_longest
s1 = 'aebcdee'
s2 = 'aaeedfskm'
# Create a dictionary the value is 'list' and can append char in each 'list'
res = defaultdict[list]
# get count of each char
cnt1 = Counter[s1] # -> {'e': 3, 'a': 1, 'b': 1, 'c': 1, 'd': 1}
cnt2 = Counter[s2] # -> {'a': 2, 'e': 2, 'd': 1, 'f': 1, 's': 1, 'k': 1, 'm': 1}
# for appending chars in one step, we can zip count of chars in two strings,
# so Because maybe two string have different length, we can use 'itertools. zip_longest'
for a,b in zip_longest[cnt1 , cnt2]:
# list[zip_longest[cnt1 , cnt2]] -> [['a', 'a'], ['e', 'e'], ['b', 'd'],
# ['c', 'f'], ['d', 's'], [None, 'k'],
# [None, 'm']]
# Because maybe we have 'none', before 'append' we need to check 'a' and 'b' don't be 'none'
if a: res[a].append[cnt1[a]]
if b: res[b].append[cnt2[b]]
# res -> {'a': [1, 2], 'e': [3, 2], 'b': [1], 'd': [1, 1], 'c': [1], 'f': [1], 's': [1], 'k': [1], 'm': [1]}
# If the length 'list' of each char is larger than one so this char is duplicated and we repeat this char in the result base min of each char in the 'list' of count char of two strings.
out = ''.join[k* min[v] for k,v in res.items[] if len[v]>1]
print[out]
# aeed
9s1 = 'aebcdee'
s2 = 'aaeedfskm'
s3 = 'aaeeezzxx'
res = defaultdict[list]
cnt1 = Counter[s1]
cnt2 = Counter[s2]
cnt3 = Counter[s3]
for a,b,c in zip_longest[cnt1 , cnt2, cnt3]:
if a: res[a].append[cnt1[a]]
if b: res[b].append[cnt2[b]]
if c: res[c].append[cnt3[c]]
out = ''.join[k* min[v] for k,v in res.items[] if len[v]>1]
print[out]
# aeed
0s1 = 'aebcdee'
s2 = 'aaeedfskm'
s3 = 'aaeeezzxx'
res = defaultdict[list]
cnt1 = Counter[s1]
cnt2 = Counter[s2]
cnt3 = Counter[s3]
for a,b,c in zip_longest[cnt1 , cnt2, cnt3]:
if a: res[a].append[cnt1[a]]
if b: res[b].append[cnt2[b]]
if c: res[c].append[cnt3[c]]
out = ''.join[k* min[v] for k,v in res.items[] if len[v]>1]
print[out]
# aeed
1 s1 = 'aebcdee'
s2 = 'aaeedfskm'
s3 = 'aaeeezzxx'
res = defaultdict[list]
cnt1 = Counter[s1]
cnt2 = Counter[s2]
cnt3 = Counter[s3]
for a,b,c in zip_longest[cnt1 , cnt2, cnt3]:
if a: res[a].append[cnt1[a]]
if b: res[b].append[cnt2[b]]
if c: res[c].append[cnt3[c]]
out = ''.join[k* min[v] for k,v in res.items[] if len[v]>1]
print[out]
# aeed
2s1 = 'aebcdee'
s2 = 'aaeedfskm'
s3 = 'aaeeezzxx'
res = defaultdict[list]
cnt1 = Counter[s1]
cnt2 = Counter[s2]
cnt3 = Counter[s3]
for a,b,c in zip_longest[cnt1 , cnt2, cnt3]:
if a: res[a].append[cnt1[a]]
if b: res[b].append[cnt2[b]]
if c: res[c].append[cnt3[c]]
out = ''.join[k* min[v] for k,v in res.items[] if len[v]>1]
print[out]
# aeed
3s1 = 'aebcdee'
s2 = 'aaeedfskm'
s3 = 'aaeeezzxx'
res = defaultdict[list]
cnt1 = Counter[s1]
cnt2 = Counter[s2]
cnt3 = Counter[s3]
for a,b,c in zip_longest[cnt1 , cnt2, cnt3]:
if a: res[a].append[cnt1[a]]
if b: res[b].append[cnt2[b]]
if c: res[c].append[cnt3[c]]
out = ''.join[k* min[v] for k,v in res.items[] if len[v]>1]
print[out]
# aeed
4s1 = 'aebcdee'
s2 = 'aaeedfskm'
s3 = 'aaeeezzxx'
res = defaultdict[list]
cnt1 = Counter[s1]
cnt2 = Counter[s2]
cnt3 = Counter[s3]
for a,b,c in zip_longest[cnt1 , cnt2, cnt3]:
if a: res[a].append[cnt1[a]]
if b: res[b].append[cnt2[b]]
if c: res[c].append[cnt3[c]]
out = ''.join[k* min[v] for k,v in res.items[] if len[v]>1]
print[out]
# aeed
5____10
s1 = 'aebcdee'
s2 = 'aaeedfskm'
s3 = 'aaeeezzxx'
res = defaultdict[list]
cnt1 = Counter[s1]
cnt2 = Counter[s2]
cnt3 = Counter[s3]
for a,b,c in zip_longest[cnt1 , cnt2, cnt3]:
if a: res[a].append[cnt1[a]]
if b: res[b].append[cnt2[b]]
if c: res[c].append[cnt3[c]]
out = ''.join[k* min[v] for k,v in res.items[] if len[v]>1]
print[out]
# aeed
9eegks2
from
1from
2____10
s1 = 'aebcdee'
s2 = 'aaeedfskm'
s3 = 'aaeeezzxx'
res = defaultdict[list]
cnt1 = Counter[s1]
cnt2 = Counter[s2]
cnt3 = Counter[s3]
for a,b,c in zip_longest[cnt1 , cnt2, cnt3]:
if a: res[a].append[cnt1[a]]
if b: res[b].append[cnt2[b]]
if c: res[c].append[cnt3[c]]
out = ''.join[k* min[v] for k,v in res.items[] if len[v]>1]
print[out]
# aeed
9eegks2
from
6from
7eegks0
s1 = 'aebcdee'
s2 = 'aaeedfskm'
s3 = 'aaeeezzxx'
res = defaultdict[list]
cnt1 = Counter[s1]
cnt2 = Counter[s2]
cnt3 = Counter[s3]
for a,b,c in zip_longest[cnt1 , cnt2, cnt3]:
if a: res[a].append[cnt1[a]]
if b: res[b].append[cnt2[b]]
if c: res[c].append[cnt3[c]]
out = ''.join[k* min[v] for k,v in res.items[] if len[v]>1]
print[out]
# aeed
1 collections
0from collections import Counter, defaultdict
from itertools import zip_longest
s1 = 'aebcdee'
s2 = 'aaeedfskm'
# Create a dictionary the value is 'list' and can append char in each 'list'
res = defaultdict[list]
# get count of each char
cnt1 = Counter[s1] # -> {'e': 3, 'a': 1, 'b': 1, 'c': 1, 'd': 1}
cnt2 = Counter[s2] # -> {'a': 2, 'e': 2, 'd': 1, 'f': 1, 's': 1, 'k': 1, 'm': 1}
# for appending chars in one step, we can zip count of chars in two strings,
# so Because maybe two string have different length, we can use 'itertools. zip_longest'
for a,b in zip_longest[cnt1 , cnt2]:
# list[zip_longest[cnt1 , cnt2]] -> [['a', 'a'], ['e', 'e'], ['b', 'd'],
# ['c', 'f'], ['d', 's'], [None, 'k'],
# [None, 'm']]
# Because maybe we have 'none', before 'append' we need to check 'a' and 'b' don't be 'none'
if a: res[a].append[cnt1[a]]
if b: res[b].append[cnt2[b]]
# res -> {'a': [1, 2], 'e': [3, 2], 'b': [1], 'd': [1, 1], 'c': [1], 'f': [1], 's': [1], 'k': [1], 'm': [1]}
# If the length 'list' of each char is larger than one so this char is duplicated and we repeat this char in the result base min of each char in the 'list' of count char of two strings.
out = ''.join[k* min[v] for k,v in res.items[] if len[v]>1]
print[out]
# aeed
3 collections
2eegks2
eegks2
collections
5from collections import Counter, defaultdict
from itertools import zip_longest
s1 = 'aebcdee'
s2 = 'aaeedfskm'
# Create a dictionary the value is 'list' and can append char in each 'list'
res = defaultdict[list]
# get count of each char
cnt1 = Counter[s1] # -> {'e': 3, 'a': 1, 'b': 1, 'c': 1, 'd': 1}
cnt2 = Counter[s2] # -> {'a': 2, 'e': 2, 'd': 1, 'f': 1, 's': 1, 'k': 1, 'm': 1}
# for appending chars in one step, we can zip count of chars in two strings,
# so Because maybe two string have different length, we can use 'itertools. zip_longest'
for a,b in zip_longest[cnt1 , cnt2]:
# list[zip_longest[cnt1 , cnt2]] -> [['a', 'a'], ['e', 'e'], ['b', 'd'],
# ['c', 'f'], ['d', 's'], [None, 'k'],
# [None, 'm']]
# Because maybe we have 'none', before 'append' we need to check 'a' and 'b' don't be 'none'
if a: res[a].append[cnt1[a]]
if b: res[b].append[cnt2[b]]
# res -> {'a': [1, 2], 'e': [3, 2], 'b': [1], 'd': [1, 1], 'c': [1], 'f': [1], 's': [1], 'k': [1], 'm': [1]}
# If the length 'list' of each char is larger than one so this char is duplicated and we repeat this char in the result base min of each char in the 'list' of count char of two strings.
out = ''.join[k* min[v] for k,v in res.items[] if len[v]>1]
print[out]
# aeed
9eegks0
import
6Output:
eegks
eegks
0collections
8eegks
2 import
0
Auxiliary Space : O[n]
Bạn có thể sử dụng import
7 và đếm từng char trong hai chuỗi và nếu mỗi char tồn tại trong hai chuỗi bằng cách sử dụng import
8 của from
1 và tạo một Counter
0 mới bằng cách tham gia chúng.
from collections import Counter, defaultdict
from itertools import zip_longest
s1 = 'aebcdee'
s2 = 'aaeedfskm'
# Create a dictionary the value is 'list' and can append char in each 'list'
res = defaultdict[list]
# get count of each char
cnt1 = Counter[s1] # -> {'e': 3, 'a': 1, 'b': 1, 'c': 1, 'd': 1}
cnt2 = Counter[s2] # -> {'a': 2, 'e': 2, 'd': 1, 'f': 1, 's': 1, 'k': 1, 'm': 1}
# for appending chars in one step, we can zip count of chars in two strings,
# so Because maybe two string have different length, we can use 'itertools. zip_longest'
for a,b in zip_longest[cnt1 , cnt2]:
# list[zip_longest[cnt1 , cnt2]] -> [['a', 'a'], ['e', 'e'], ['b', 'd'],
# ['c', 'f'], ['d', 's'], [None, 'k'],
# [None, 'm']]
# Because maybe we have 'none', before 'append' we need to check 'a' and 'b' don't be 'none'
if a: res[a].append[cnt1[a]]
if b: res[b].append[cnt2[b]]
# res -> {'a': [1, 2], 'e': [3, 2], 'b': [1], 'd': [1, 1], 'c': [1], 'f': [1], 's': [1], 'k': [1], 'm': [1]}
# If the length 'list' of each char is larger than one so this char is duplicated and we repeat this char in the result base min of each char in the 'list' of count char of two strings.
out = ''.join[k* min[v] for k,v in res.items[] if len[v]>1]
print[out]
# aeed
Chúng ta có thể sử dụng phương pháp này cho nhiều chuỗi, như ba chuỗi.
s1 = 'aebcdee'
s2 = 'aaeedfskm'
s3 = 'aaeeezzxx'
res = defaultdict[list]
cnt1 = Counter[s1]
cnt2 = Counter[s2]
cnt3 = Counter[s3]
for a,b,c in zip_longest[cnt1 , cnt2, cnt3]:
if a: res[a].append[cnt1[a]]
if b: res[b].append[cnt2[b]]
if c: res[c].append[cnt3[c]]
out = ''.join[k* min[v] for k,v in res.items[] if len[v]>1]
print[out]
# aeed
Làm thế nào để bạn tìm thấy phần chung của hai chuỗi?
Đối với mỗi ký tự trong chuỗi 1, chúng tôi tăng chỉ số vector của ký tự đó, ví dụ: v [s1 [i]-'a'] ++, đối với mỗi ký tự của chuỗi 2, chúng tôi kiểm tra vectơ cho các ký tự chung nếu v [s2 [i]-'A']> 0 sau đó đặt cờ = true và v [s2 [i]-'a']-sao cho một ký tự của chuỗi 2 được so sánh chỉ với một ký tự của chuỗi 1.if v[s2[i]-'a'] > 0 then set flag = true and v[s2[i]-'a']– such that one character of string 2 is compared with only one character of string 1.
Làm cách nào để kiểm tra xem hai chuỗi có cùng một ký tự trong Python không?
So sánh chuỗi bằng cách sử dụng == trong python hàm == so sánh các giá trị của hai chuỗi và trả về nếu chúng bằng hoặc không.Nếu các chuỗi bằng nhau, nó sẽ trả về đúng, nếu không nó sẽ trả về sai.The == function compares the values of two strings and returns if they are equal or not. If the strings are equal, it returns True, otherwise it returns False.
Làm thế nào để bạn tìm thấy yếu tố không phổ biến trong hai chuỗi trong Python?
Không phổ biến [S1, S2] / * S1 và S2 là hai chuỗi * / Bước 1: Chuyển đổi cả hai chuỗi thành SET ST1 và ST2.Bước 2: Sử dụng giao điểm của hai bộ và nhận các ký tự chung.Bước 3: Bây giờ tách ra các ký tự trong mỗi chuỗi không phổ biến trong cả hai chuỗi.Step 1: Convert both string into set st1 and st2. Step 2: use the intersection of two sets and get common characters. Step 3: now separate out characters in each string which are not common in both string.