Page 1 of 1

Comparing two strings

Posted: Tue May 19, 2020 1:15 am
by saregg
I wanted to compare two strings and return true if one string contains all the char of the other

Code: Select all

public boolean canConstruct(String ransomNote, String magazine) {
        char[] ca = magazine.toCharArray();
        List<char[]> ch = new ArrayList<>(Arrays.asList(ca));
        char[] cb = ransomNote.toCharArray();
        List<char[]> de = new ArrayList<>(Arrays.asList(cb));
        return de.containsAll(ch)? true:false;
    	} 
But this always return false, why ?

Re: Comparing two strings

Posted: Tue May 19, 2020 2:34 am
by admin
Check out how containsAll of List works. what does it compare.

de.containsAll simply checks if all the elements of ch are there in de. de doesn't contain chars. It contains an array.