用于检查给定字符串是否可以由其他两个字符串或其排列组成的 Python3 程序
给定一个字符串str和一个字符串数组arr[] ,任务是检查给定的字符串是否可以由数组中的任何字符串对或其排列组成。
例子:
Input: str = “amazon”, arr[] = {“loa”, “azo”, “ft”, “amn”, “lka”}
Output: Yes
The chosen strings are “amn” and “azo”
which can be rearranged as “amazon”.
Input: str = “geeksforgeeks”, arr[] = {“geeks”, “geek”, “for”}
Output: No
下面是上述方法的实现:
Python3
# Python3 implementation of the approach
# Function that returns true if str can be
# generated from any permutation of the
# two strings selected from the given vector
def isPossible(v, string ) :
char_list = list(string)
# Sort the given string
char_list.sort()
# Select two strings at a time from given vector
for i in range(len(v)-1) :
for j in range(len(v)) :
# Get the concatenated string
temp = v[i] + v[j];
# Sort the resultant string
temp_list = list(temp)
temp_list.sort()
# If the resultant string is equal
# to the given string str
if (temp_list == char_list) :
return True;
# No valid pair found
return False;
# Driver code
if __name__ == "__main__" :
string = "amazon";
v = [ "fds", "oxq", "zoa", "epw", "amn" ];
if (isPossible(v, string)):
print("Yes");
else :
print("No");
# This code is contributed by AnkitRai01
Python3
# Python 3 implementation of the approach
MAX = 26
# Function to sort the given string
# using counting sort
def countingsort(s):
# Array to store the count of each character
count = [0 for i in range(MAX)]
for i in range(len(s)):
count[ord(s[i]) - ord('a')] += 1
index = 0
# Insert characters in the string
# in increasing order
for i in range(MAX):
j = 0
while (j < count[i]):
s = s.replace(s[index],chr(97+i))
index += 1
j += 1
# Function that returns true if str can be
# generated from any permutation of the
# two strings selected from the given vector
def isPossible(v, str1):
# Sort the given string
countingsort(str1);
# Select two strings at a time from given vector
for i in range(len(v)-1):
for j in range(i + 1,len(v),1):
# Get the concatenated string
temp = v[i] + v[j]
# Sort the resultant string
countingsort(temp)
# If the resultant string is equal
# to the given string str
if (temp == str1):
return False
# No valid pair found
return True
# Driver code
if __name__ == '__main__':
str1 = "amazon"
v = ["fds", "oxq", "zoa", "epw", "amn"]
if (isPossible(v, str1)):
print("Yes")
else:
print("No")
# This code is contributed by
# Surendra_Gangwar
方法2:可以使用计数排序来减少上述方法的运行时间。计数排序使用一个表来存储每个字符的计数。我们有 26 个字母,因此我们制作了一个大小为 26 的数组来存储字符串中每个字符的计数。然后将字符按升序排列,得到排序后的字符串。
下面是上述方法的实现:
Python3
# Python 3 implementation of the approach
MAX = 26
# Function to sort the given string
# using counting sort
def countingsort(s):
# Array to store the count of each character
count = [0 for i in range(MAX)]
for i in range(len(s)):
count[ord(s[i]) - ord('a')] += 1
index = 0
# Insert characters in the string
# in increasing order
for i in range(MAX):
j = 0
while (j < count[i]):
s = s.replace(s[index],chr(97+i))
index += 1
j += 1
# Function that returns true if str can be
# generated from any permutation of the
# two strings selected from the given vector
def isPossible(v, str1):
# Sort the given string
countingsort(str1);
# Select two strings at a time from given vector
for i in range(len(v)-1):
for j in range(i + 1,len(v),1):
# Get the concatenated string
temp = v[i] + v[j]
# Sort the resultant string
countingsort(temp)
# If the resultant string is equal
# to the given string str
if (temp == str1):
return False
# No valid pair found
return True
# Driver code
if __name__ == '__main__':
str1 = "amazon"
v = ["fds", "oxq", "zoa", "epw", "amn"]
if (isPossible(v, str1)):
print("Yes")
else:
print("No")
# This code is contributed by
# Surendra_Gangwar
请参阅有关检查给定字符串是否可以由其他两个字符串或其排列组成的完整文章以获取更多详细信息!