Kodekraftt
/Blog
/Find Common Characters Between Two Strings In Java Logic Examples And Program
Join Early AccessContact UsPrivacy Policy
Java BasicsOOPsDSA with JavaQuizzesInterview Preparation

© 2026 KodeKraftt. All rights reserved.

Build smarter. Learn more. Innovate better.

Find Common Characters Between Two Strings in Java — Logic, Examples and Program

Learn how to find common characters between two strings in Java using simple logic, nested loops, and without using advanced data structures.

Find Common Characters Between Two Strings in Java

Two strings may share some characters in common. Finding these common characters is a frequently asked Java string interview question.

In this program, we will compare two strings and print all characters that appear in both strings.

Understanding the Problem

Given two strings:

  • Traverse characters of the first string.
  • Compare each character with characters of the second string.
  • If a character matches, it is a common character.

Example 1

Input: str1 = "java", str2 = "javascript"

Output: j a v

Example 2

Input: str1 = "abcd", str2 = "xyz"

Output: No common characters

Logic Explanation

  1. Use two nested loops.
  2. Pick one character from the first string.
  3. Compare it with every character of the second string.
  4. If both characters are equal, print it.

Java Program to Find Common Characters


import java.util.Scanner;

public class CommonCharacters {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        System.out.print("Enter first string: ");
        String str1 = sc.nextLine();

        System.out.print("Enter second string: ");
        String str2 = sc.nextLine();

        boolean found = false;

        System.out.print("Common characters: ");

        for (int i = 0; i < str1.length(); i++) {
            for (int j = 0; j < str2.length(); j++) {

                if (str1.charAt(i) == str2.charAt(j)) {
                    System.out.print(str1.charAt(i) + " ");
                    found = true;
                    break;
                }
            }
        }

        if (!found) {
            System.out.print("None");
        }
    }
}

Sample Output


Enter first string: java
Enter second string: javascript
Common characters: j a v

Important Notes

  • Comparison is case-sensitive.
  • Duplicate common characters may be printed.
  • Time complexity is O(n × m).

Practice Challenges

  1. Remove duplicate common characters.
  2. Ignore case sensitivity.
  3. Use HashSet to optimize the solution.

This problem strengthens your understanding of nested loops and character comparison in Java strings.

You might also like

Check if Two Strings Are Rotations of Each Other in Java — Definition, Examples and Program

Learn how to check whether two strings are rotations of each other in Java using logical string comparison without using complex libraries.

Find the Shortest Word in a Sentence in Java — Definition, Examples and Program

Learn how to find the shortest word in a sentence in Java using a simple character-by-character approach without using split() or advanced string methods.

Abundant Number in Java — Definition, Examples and Program

Learn what an abundant number is in Java with definition, examples, and a clean program to check whether a number is abundant or not.

Concatenate Two Strings in Java Without Using + Operator — Examples and Program

Learn how to concatenate two strings in Java without using the + operator. This blog explains the logic with examples, a Java program, and practice challenges.

Check if a String is Empty or Null in Java — Definition, Examples and Program

Learn how to check whether a string is null or empty in Java. This blog explains the difference, common mistakes, examples, and a safe Java program with practice challenges.

Convert String to Uppercase in Java Without Using String Library

Learn how to convert a string to uppercase in Java without using built-in String methods. This blog explains the ASCII-based logic with examples, a Java program, and practice challenges.