Tuesday 23 May 2023

Checking if Two Strings Have a Common Substring in Java

In many programming scenarios, it becomes crucial to determine whether two strings have a common substring. This task is particularly relevant in various applications like text processing, search engines, and data analysis. In this article, we will explore how to efficiently check for a common substring between two strings using the Java programming language, along with examples to illustrate the concepts.

Approach:

One approach to solving this problem is by leveraging the power of sets and characters. We can iterate through each character in the first string and add it to a set. Then, for each character in the second string, we check if it is already present in the set. If a character is found in the set, it means there is a common substring between the two strings.

Here is the step-by-step algorithm:

1. Create an empty set to store characters.
           
2. Iterate through each character in the first string:
          - Add the character to the set.

3. Iterate through each character in the second string:
          - Check if the character is present in the set.
                  - If found, there is a common substring.

4. Return the result indicating whether a common substring was found.

Java Implementation:

Let's now implement the above algorithm in Java and demonstrate how to check for a common substring between two strings:

java
import java.util.HashSet; import java.util.Set; public class CommonSubstringChecker { public static boolean hasCommonSubstring(String str1, String str2) { Set<Character> characters = new HashSet<>(); // Add characters from the first string to the set for (char ch : str1.toCharArray()) { characters.add(ch); } // Check if characters from the second string are present in the set for (char ch : str2.toCharArray()) { if (characters.contains(ch)) { return true; // Common substring found } } return false; // No common substring found } public static void main(String[] args) { String str1 = "Hello"; String str2 = "World"; boolean hasCommonSubstring = hasCommonSubstring(str1, str2); if (hasCommonSubstring) { System.out.println("The strings have a common substring."); } else { System.out.println("The strings do not have a common substring."); } } }
Output:

output
The strings have a common substring.


In the example above, we check whether the strings "Hello" and "World" have a common substring. Since both strings contain the character 'o', they indeed have a common substring.

You can test this implementation by trying different pairs of strings and observe whether they have common substrings or not.

Conclusion: 
Determining whether two strings have a common substring is a common requirement in many programming tasks. By utilizing sets and characters, we can efficiently solve this problem in Java. The algorithm presented in this article allows us to determine the presence of a common substring between two strings accurately.

No comments:

Post a Comment

Seven front-end development trends in 2023-2024

With the increasing prevalence of apps in the digital landscape , the role of front-end developers remains essential. While apps aim to ove...

Popular Posts