
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Print Common Characters of Two Strings in Alphabetical Order
In this article, we will learn to print common characters of two strings in alphabetical order using Java. The program uses arrays to count how often each letter appears in both strings and then compares these counts to identify the common characters.
Problem Statement
Write a Java program to print common characters of two strings in alphabetical order ?
Input
my_str_1 = "itsasample"
my_str_2 = "thisisasample"
Output
The common characters between the two strings in alphabetical order is :
aaeilmpsst
Steps to print common characters of two strings in alphabetical order
Following are the steps to print common characters of two strings in alphabetical order ?
- First, we will import all the necessary classes from java.io and java.util package.
- After that, we will define a class Demo with the method common_chars.
- Create two arrays of size 26 to track letter frequencies for each string.
- Loop through both strings using for loop to populate the arrays with letter counts.
- Compare the arrays and print the common characters based on their frequency.
- In the main method, call common_chars with two predefined strings.
Java code to print common characters of two strings
Below is the Java code to print common characters of two strings in alphabetical order, the code is as follows ?
import java.io.*; import java.util.*; public class Demo { static void common_chars(String str_1, String str_2) { int[] array_1 = new int[26]; int[] array_2 = new int[26]; int str_len_1 = str_1.length(); int str_len_2 = str_2.length(); for (int i = 0; i < str_len_1; i++) array_1[str_1.charAt(i) - 'a'] += 1; for (int i = 0; i < str_len_2; i++) array_2[str_2.charAt(i) - 'a'] += 1; for (int i = 0; i < 26; i++) { if (array_1[i] != 0 && array_2[i] != 0) { for (int j = 0; j < Math.min(array_1[i], array_2[i]); j++) System.out.print(((char)(i + 'a'))); } } } public static void main(String[] args) throws IOException { String my_str_1 = "itsasample"; String my_str_2 = "thisisasample"; System.out.println("The common characters between the two strings in alphabetical order are:"); common_chars(my_str_1, my_str_2); } }
Output
The common characters between the two strings in alphabetical order is : aaeilmpsst
Code Explanation
A class named Demo contains a function called ?common_chars', which declares two integer arrays of size 26 (indicating the 26 alphabets in English). Their lengths are stored in two different variables.
The arrays are iterated over and at the index of difference between ASCII of ?a' and ASCII of every character. ASCII of the character ?a' is subtracted from every character's ASCII value and incremented by 1. This will fill up only those values in the array that are common. The minimum number of characters from both arrays is computed and printed on the console. In the main function, the two strings are defined and the function is called by passing these two strings as parameters.