Posts Uncommon characters (geeksforgeeks - SDE Sheet)
Post
Cancel

Uncommon characters (geeksforgeeks - SDE Sheet)

PROBLEM DESCRIPTION

Given two strings A and B consisting of lowercase english characters. Find the characters that are not common in the two strings.

Return the string in sorted order.

geeksforgeeks

SOLUTION

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
class Solution
{
    String UncommonChars(String A, String B)
    {

        int[] f1 = new int[26];
        for(int i=0; i<A.length(); i++){
            f1[A.charAt(i)-'a'] = 1;
        }

        int[] f2 = new int[26];
        for(int i=0; i<B.length(); i++){
            f2[B.charAt(i)-'a'] = 1;
        }

        StringBuffer sb = new StringBuffer();

        for(int i=0; i<26; i++){

            if((f1[i]^f2[i]) == 1){
                sb.append(String.valueOf((char)(i+'a')));
            }

        }

        return sb.toString().equals("")?"-1":sb.toString();

    }
}
This post is licensed under CC BY 4.0 by the author.