Java ProgramsStringsFind Common Characters Between Two Strings

Find Common Characters Between Two Strings in Java

intermediate·  Strings  ·  String

Problem

A common character is a letter that appears in both strings being compared, regardless of how many times it appears in either one.

Given two strings, find every character that appears in both.

Input
"apple", "maple"
Output
Common characters: a, p, l, e

Java Program

Java
public class CommonCharacters { public static void main(String[] args) { String str1 = "apple"; String str2 = "maple"; boolean[] inSecond = new boolean[26]; for (char c : str2.toCharArray()) { inSecond[c - 'a'] = true; // mark every letter that appears anywhere in str2 } boolean[] printed = new boolean[26]; StringBuilder result = new StringBuilder(); for (char c : str1.toCharArray()) { int index = c - 'a'; if (inSecond[index] && !printed[index]) { // in str2, and not already listed if (result.length() > 0) result.append(", "); result.append(c); printed[index] = true; // avoid listing this letter again if it repeats in str1 } } System.out.println("Common characters: " + result); } }

Output

Common characters: a, p, l, e

Core Logic

Marking which letters appear anywhere in the second string, then scanning the first string in order, finds every letter both strings share.

How It Works
  1. 1A boolean[26] array named inSecond marks which letters appear anywhere in str2.
  2. 2A second boolean[26] array named printed tracks which common letters have already been added to the result, avoiding duplicates.
  3. 3The scan walks str1 in order; a letter is added to the result only if it's marked in inSecond and not yet marked in printed.
  4. 4Adding a letter to the result also marks it in printed, so a repeated letter in str1 — like the second 'p' in "apple" — isn't listed twice.
For "apple" and "maple", the letters 'a', 'p', 'l', and 'e' appear in both, so they're reported in the order they first appear in str1.
💡

Key Point: The result order follows str1's first-appearance order, not str2's — swapping which string is scanned would list the same letters in a different order.

Complexity
Time Complexity: O(n + m)Space Complexity: O(1)

Why: Both strings are scanned once each to mark or check letter presence in fixed 26-element arrays, so the extra memory never grows with the input length.

Key Concepts

boolean arrayASCII indexingfor-each loop

Approach 2: Java 8

Java
import java.util.Set; import java.util.stream.Collectors; public class CommonCharactersStream { public static void main(String[] args) { String str1 = "apple"; String str2 = "maple"; Set<Character> secondChars = str2.chars() .mapToObj(c -> (char) c) .collect(Collectors.toSet()); // Keeps only str1's distinct characters that also appear in str2's set String result = str1.chars() .mapToObj(c -> (char) c) .distinct() .filter(secondChars::contains) .map(String::valueOf) .collect(Collectors.joining(", ")); System.out.println("Common characters: " + result); } }

Output

Common characters: a, p, l, e

Core Logic

Building a Set of the second string's characters lets a stream over the first string keep only the ones present in both, in one filtered pass.

How It Works
  1. 1str2.chars().mapToObj(c -> (char) c).collect(Collectors.toSet()) builds a Set<Character> of every distinct character in str2.
  2. 2str1.chars().mapToObj(c -> (char) c).distinct() turns str1 into a stream of its own distinct characters, in first-appearance order.
  3. 3.filter(secondChars::contains) keeps only the characters that also appear in str2's set.
  4. 4.collect(Collectors.joining(", ")) joins the surviving characters into the final result.
Filtering "apple"'s distinct characters against the set built from "maple" keeps 'a', 'p', 'l', and 'e', matching the manual version.
💡

Key Point: .distinct() on str1's stream does the same job as the manual version's printed array — both exist to stop a repeated letter in str1, like the second 'p', from being listed twice.

Complexity
Time Complexity: O(n + m)Space Complexity: O(1)

Why: Both scans still run in time proportional to the input strings, and the set backing the lookups stays bounded by the fixed alphabet size.

Key Concepts

StreamSetdistinct()filter()

Related Programs