javaarraysstringmultidimensional-array

Common characters in n strings


I m trying to make a function that prints the number of characters common in given n strings. (note that characters may be used multiple times)

I am struggling to perform this operation on n strings However I did it for 2 strings without any characters repeated more than once.

I have posted my code.

public class CommonChars {

    public static void main(String[] args) {
        String str1 = "abcd";
        String str2 = "bcde";
        StringBuffer sb = new StringBuffer();
        // get unique chars from both the strings

        str1 = uniqueChar(str1);
        str2 = uniqueChar(str2);
        int count = 0;
        int str1Len = str1.length();
        int str2Len = str2.length();

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

            for (int j = 0; j < str2Len; j++) {

                // found match stop the loop
                if (str1.charAt(i) == str2.charAt(j)) {
                    count++;
                    sb.append(str1.charAt(i));
                    break;
                }
            }

        }   
        System.out.println("Common Chars Count : " + count + "\nCommon Chars :" + 
        sb.toString());
    }


    public static String uniqueChar(String inputString) {
        String outputstr="",temp="";
        for(int i=0;i<inputstr.length();i++) {
            if(temp.indexOf(inputstr.charAt(i))<0) {
                temp+=inputstr.charAt(i);
            }
        }
        System.out.println("completed");
        return temp;
    }

}
3 
abcaa
bcbd
bgc
3

their may be chances that a same character can be present multiple times in a string and you are not supposed to eliminate those characters instead check the no. of times they are repeated in other strings. for eg

 3
 abacd
 aaxyz
 aatre

output should be 2

it will be better if i get solution in java


Solution

  • You have to convert all Strings to Set of Characters and retain all from the first one. Below solution has many places which could be optimised but you should understand general idea.

    import java.util.Arrays;
    import java.util.Collection;
    import java.util.Collections;
    import java.util.HashSet;
    import java.util.List;
    import java.util.Set;
    
    public class Main {
    
        public static void main(String[] args) {
    
            List<String> input = Arrays.asList("jonas", "ton", "bonny");
    
            System.out.println(findCommonCharsFor(input));
        }
    
        public static Collection<Character> findCommonCharsFor(List<String> strings) {
            if (strings == null || strings.isEmpty()) {
                return Collections.emptyList();
            }
    
            Set<Character> commonChars = convertStringToSetOfChars(strings.get(0));
            strings.stream().skip(1).forEach(s -> commonChars.retainAll(convertStringToSetOfChars(s)));
    
            return commonChars;
        }
    
        private static Set<Character> convertStringToSetOfChars(String string) {
            if (string == null || string.isEmpty()) {
                return Collections.emptySet();
            }
    
            Set<Character> set = new HashSet<>(string.length() + 10);
            for (char c : string.toCharArray()) {
                set.add(c);
            }
    
            return set;
        }
    }
    

    Above code prints:

    [n, o]