Cyclone321
Cyclone321

Reputation: 21

How to check to see if the characters of one string are in another string

For example:

string 1 = helloworld string 2 = asdfuvjerhelloworld

this should return true.

Another example: string 1 = helloworld string 2 = lshewodxzr

this should also return true.

So i'm looking how to make a method that will return a boolean that will check to see if the second string has the letters in the first string. In the second example, string2 only had the letter l once, even though the letter l shows up three times in string1 and it still returned true. Also in the second example string2 had letters that weren't in string1, but it still returned true.

Any help on how to code this would be very appreciated!

Upvotes: 0

Views: 2851

Answers (5)

skiwi
skiwi

Reputation: 69379

I actually found an easier way, but leaving my old answer for reference. This also ought to be the most efficient way, and it is still using Java 8:

public boolean containsAllChars(String a, String b) {
    List<Character> aChars = stringToCharacterList(a);
    List<Character> bChars = stringToCharacterList(b);
    return bChars.containsAll(aChars);
}

public List<Character> stringToCharacterList(String input) {
    return input.chars().distinct()
            .mapToObj(i -> (char)i)
            .collect(Collectors.toList());
}

It obtains a list of distinct characters from both strings, and then checks if all of all characters of the first string are contained in the second string.

Upvotes: 2

Karibasappa G C
Karibasappa G C

Reputation: 2732

String s1 = "helloworld";
        String s2 = "lshewodxzr";
        boolean flag=false;
        for(int i=0;i<s1.length();i++){
            for(int j=0;j<s2.length();j++){
                if(s1.charAt(j) != s2.charAt(j)){
                    flag=false;
                }
                else{
                    flag=true;
                    break;
                }
            }
        }
        if(flag){
            System.out.println("s2 has all characters of s1");
        }
        else{
            System.out.println("s2 doesn't have all characters of s1");
        }

Upvotes: 0

Simon Forsberg
Simon Forsberg

Reputation: 13351

I believe this solution is correct. If I have understood your requirements correctly.

This is using Java 8.

public static void main(String[] args) {
    String a = "helloworld";
    String b = "lshewodxzr";

    containsAllChars(a, b);
    containsAllChars(b, a);
}

private static void containsAllChars(String a, String b) {
    boolean result = a.chars().distinct().allMatch(ch -> b.contains(String.valueOf((char) ch)));
    System.out.println(result);
}

Output:

true
false

Upvotes: 5

skiwi
skiwi

Reputation: 69379

With using Java 8 I would do it the following:

public boolean check(final String firstString, final String secondString) {
    //Create a map mapping all characters from the firstString to true/false
    Map<Character, Boolean> map = IntStream.rangeClosed('a', 'z')
            .boxed()
            .collect(Collectors.toMap(
                    i -> (char)(int)i,
                    i -> firstString.chars().anyMatch(j -> j == i)
            ));

    return map.entrySet().stream()
            .filter(entry -> entry.getValue() == true)
            .map(Map.Entry::getKey)
            .allMatch(ch -> secondString.chars().anyMatch(i -> i == ch));
}

Explanation:

  • First you create a Map<Character, Boolean> that for all chars a through z, maps whether it occurs in the first string.
  • Then it obtains a stream from that map, only looks at the characters that were in the first string, maps the stream to a stream of characters, even though it is an IntStream, and then checks whether the second string has that character in it.

Upvotes: 3

Jigar Joshi
Jigar Joshi

Reputation: 240966

sourceString.charArray() > Arrays.sort() > sourceString.contains(keyString) - not too efficient

for longer String generate character > occurrence map for source String and iterate through key String and check and process Map

Upvotes: 0

Related Questions