鉴于两个字符串的大小N str1和M尺寸.The任务的STR2是寻找是否有可能撰写STR2使用STR1这样的只有字符str1中的每一个字符,可以使用任意数量的时间。
注意:小写字母和大写字母应该被认为是不同的。
例子:
Input: str1 = “The quick brown fox jumps”, str2 = “the fox was quicker than the dog”
Output: No
Explanation:
In str2, there is d character, which is not present in str1. So, it is impossible to make the string str2 from str1
Input: str1 = “we all love geeksforgeeks”, str2 = “we all love geeks”
Output: Yes
天真的方法:最简单的方法是在str1 中搜索str2 的每个字符。如果找到所有字符,则打印“是”。否则打印“否”。
时间复杂度: O(N*M)
辅助空间: O(1)
有效的方法:这个想法是在count[]数组中标记str1的所有字符的存在。然后遍历str2并检查str2的字符是否存在于str1 中。
- 创建一个大小为 256(ASCII字符总数)的数组count[]并将其设置为零。
- 然后迭代str1并使count[str[i]] = 1来标记每个字符的出现。
- 迭代str2并使用count[]数组检查该字符是否存在于str1 中。
下面是上述方法的实现:
CPP
// C++ implementation of
// the above approach
#include
using namespace std;
// Function to check if
// str2 can be made by characters
// of str1 or not
void isPossible(string str1, string str2)
{
// To store the
// occurrence of every
// character
int arr[256] = { 0 };
// Length of the two
// strings
int l1 = str1.size();
int l2 = str2.size();
int i, j;
// Assume that it is
// possible to compose the
// string str2 from str1
bool possible = true;
// Iterate over str1
for (i = 0; i < l1; i++) {
// Store the presence of every
// character
arr[str1[i]] = 1;
}
// Iterate over str2
for (i = 0; i < l2; i++) {
// Ignore the spaces
if (str2[i] != ' ') {
// Check for the presence
// of character in str1
if (arr[str2[i]] == 1)
continue;
else {
possible = false;
break;
}
}
}
// If it is possible to make
// str2 from str1
if (possible) {
cout << "Yes" << endl;
}
else {
cout << "No" << endl;
}
}
// Driver Code
int main()
{
// Given strings
string str1 = "we all love geeksforgeeks";
string str2 = "we all love geeks";
// Function Call
isPossible(str1, str2);
return 0;
}
Java
// Java implementation of
// the above approach
import java.util.Arrays;
class GFG {
// Function to check if
// str2 can be made by characters
// of str1 or not
public static void isPossible(String str1, String str2) {
// To store the
// occurrence of every
// character
int arr[] = new int[256];
Arrays.fill(arr, 0);
// Length of the two
// strings
int l1 = str1.length();
int l2 = str2.length();
int i, j;
// Assume that it is
// possible to compose the
// string str2 from str1
boolean possible = true;
// Iterate over str1
for (i = 0; i < l1; i++) {
// Store the presence of every
// character
arr[str1.charAt(i)] = 1;
}
// Iterate over str2
for (i = 0; i < l2; i++) {
// Ignore the spaces
if (str2.charAt(i) != ' ') {
// Check for the presence
// of character in str1
if (arr[str2.charAt(i)] == 1)
continue;
else {
possible = false;
break;
}
}
}
// If it is possible to make
// str2 from str1
if (possible) {
System.out.println("Yes");
} else {
System.out.println("No");
}
}
// Driver Code
public static void main(String args[])
{
// Given strings
String str1 = "we all love geeksforgeeks";
String str2 = "we all love geeks";
// Function Call
isPossible(str1, str2);
}
}
// This code is contributed by saurabh_jaiswal.
C#
// C# implementation of
// the above approach
using System;
using System.Collections.Generic;
class GFG{
// Function to check if
// str2 can be made by characters
// of str1 or not
static void isPossible(string str1, string str2)
{
// To store the
// occurrence of every
// character
int []arr = new int[256];
Array.Clear(arr,0,256);
// Length of the two
// strings
int l1 = str1.Length;
int l2 = str2.Length;
int i;
// Assume that it is
// possible to compose the
// string str2 from str1
bool possible = true;
// Iterate over str1
for (i = 0; i < l1; i++) {
// Store the presence of every
// character
arr[str1[i]] = 1;
}
// Iterate over str2
for (i = 0; i < l2; i++) {
// Ignore the spaces
if (str2[i] != ' ') {
// Check for the presence
// of character in str1
if (arr[str2[i]] == 1)
continue;
else {
possible = false;
break;
}
}
}
// If it is possible to make
// str2 from str1
if (possible) {
Console.Write("Yes");
}
else {
Console.Write("No");
}
}
// Driver Code
public static void Main()
{
// Given strings
string str1 = "we all love geeksforgeeks";
string str2 = "we all love geeks";
// Function Call
isPossible(str1, str2);
}
}
// This code is contributed by ipg2016107.
Python3
# Python3 implementation of
# the above approach
# Function to check if
# str2 can be made by characters
# of str1 or not
def isPossible(str1, str2):
# To store the
# occurrence of every
# character
arr = {}
# Length of the two
# strings
l1 = len(str1)
l2 = len(str2)
# Assume that it is
# possible to compose the
# string str2 from str1
possible = True
# Iterate over str1
for i in range(l1):
# Store the presence or every element
arr[str1[i]] = 1
# Iterate over str2
for i in range(l2):
# Ignore the spaces
if str2[i] != ' ':
# Check for the presence
# of character in str1
if arr[str2[i]] == 1:
continue
else:
possible = False
break
# If it is possible to make
# str2 from str1
if possible:
print("Yes")
else:
print("No")
# Driver code
str1 = "we all love geeksforgeeks"
str2 = "we all love geeks"
# Function call.
isPossible(str1, str2)
# This code is contributed by Parth Manchanda
Javascript
Yes
时间复杂度: O(N+M)
辅助空间: O(1)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。