How can I see if an array in Java contains all the elements of another array? [closed]
Write a boolean function that takes two unordered char arrays as parameters. The size of the first array is guaranteed to be less than or equal to the size of the second array. The function returns true if every element in the first array is contained in the second.
Results:
Array One Array Two Return
"a" "a" True
"aa" "ab" False
"cbb" "abbc" True
"abbccdd" "abbcccdd" True
EDIT Here's my attempt so far:
public static Boolean cmprStr( String s1, String s2 )
{
for(int i = 0; i < s1.length(); i++ )
{
if( !s2.contains( String.valueOf( s1.charAt(i) ) ) )
{
return false;
}
}
return true;
}
Here are four steps that might help you to solve these kind of problems.
- First understand what the problem is.
- Identify functions and variables
- Think on how would you do that in real life
- Code it.
The last part is the easiest one.
As for step 3:
Let's say you have a box with:
A = [a,b,b,c,c,d,d]
And other with:
B = [a,b,b,c,c,c,d,d]
How would you go ( in real life ) if you want to know if all the elements in A exist in B?
Well you:
- Take the first element ( a )
- Look for it in in B
- If it exists, you're right on track ( OK = true ).
- If it doesn't you end with OK = false
- Repeat until you finish with all the elements.
As absurd as this may look, this is the first step to code.
Now take each step and create a pseudo-code for it ( not real Java code )
//1. Take the first element ( a )
e = A[0]
//2. Look for it in in B
for each x in B do
if x == b found = true
end
found = false
//3. If it exists, you're right on track ( OK = true ).
if found == true ? OK = true continue...
//4. If it doesn't you end with OK = false
else OK = false
//5. Repeat until you finish with all the elements.
go to 1.- using A[1]
Check the value of "OK" at the end and that will be your answer.
Once you have this part correct and complete ( notice my pseudo-code may be wrong, you have to check it for your self ) then you're in position to code and that'll be very straightforward.
Later, when you have completely understood this process, you may skip the part of writing down the algorithm and you'll be ready for what Andrew Lazarus mention, you can search for better algorithms to optimize your search.
But, try to solve it this way first.
Good luck
Is a homework tag missing?
Because of the way that repetitions are handled, I think you should convert both arrays into Map<Character, Integer>
with counts. (Actually, if you know the input is a char
between 'a' and 'z', it is better to use an array of int
for performance, but I will leave optimization up to you.) Once this step is done, you just go through the smaller array and check that the count is ≤ the corresponding count in the larger one.
You have to count each character it both array. If every character in array two has a higher count than its counterpart in array one then return true, else return false.
Finally did it!!!
import java.util.Arrays;
class stringClass
{
public static void main(String args[])
{
char s1[] = { 'a', 'b', 'b', 'c', 'c', 'd', 'd' };
char s2[] = { 'a', 'b', 'b', 'c', 'c', 'c', 'd', 'd' };
Boolean ret = cmprStr( s1, s2 );
System.out.println( ret );
}
public static Boolean cmprStr( char[] s1, char[] s2 )
{
char subS2[] = new char[s1.length];
int cnt = 0;
Arrays.sort( s1 );
Arrays.sort( s2 );
for( int i = 0; i < s1.length; i++ )
{
for( int j = 0; j < s2.length; j++ )
{
if( s1[i] == s2[j] )
{
subS2[cnt++] = s1[i];
s2[j] = ' ';
break;
}
}
}
if( Arrays.equals( s1, subS2 ) )
{
return true;
}
return false;
}
}
精彩评论