

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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
Java program to find all close matches of input string from a list
To find all close matches of input string from a list, the Java code is as follows −
Example
import java.io.*; import java.util.*; public class Demo{ static String string_encoding(String str){ HashMap<Character, Integer> my_map = new HashMap<>(); String result = ""; int i = 0; char ch; for (int j = 0; j < str.length(); j++) { ch = str.charAt(j); if (!my_map.containsKey(ch)) my_map.put(ch, i++); result += my_map.get(ch); } return result; } static void match_words( String[] my_arr, String my_pattern){ int len = my_pattern.length(); String hash_val = string_encoding(my_pattern); for (String word : my_arr){ if (word.length() == len && string_encoding(word).equals(hash_val)) System.out.print(word + " "); } } public static void main(String args[]){ String[] my_arr = { "mno", "aabb", "pqr", "xxyy", "mmnn" }; String my_pattern = "ddcc"; System.out.println("The patterns similar to ddcc in the array are :"); match_words(my_arr, my_pattern); } }
Output
The patterns similar to ddcc in the array are : aabb xxyy mmnn
A class named Demo contains a function called ‘string_encoding’. This function creates a hashmap and iterates over the string to check if there are any patterns that match the string in question.
Another function named 'match_words' is defined that calls the 'string_encoding' function and checks to see if there is any match in the arrays with the pattern of the string that is given as a reference. If found, the word is returned. In the main function, the string array is defined along with a pattern. The 'match_words' function is called on this pattern. The relevant output is displayed on the console.
- Related Questions & Answers
- Python program to find all close matches of input string from a list
- Find all close matches of input string from a list in Python
- Getting the list of all the matches Java regular expressions
- Java Program To Find all the Subsets of a String
- Java Program to Remove All Whitespaces from a String
- Java Program to check if none of the string in the list matches the condition
- Java Program to remove all white spaces from a String.
- Java program to find all duplicate characters in a string
- Program to find list of all possible combinations of letters of a given string s in Python
- Java Program to find all angles of a triangle
- Java Program to Convert a List of String to Comma Separated String
- Removing all collections whose name matches a string in MongoDB
- Java program to remove all the white spaces from a given string
- Java Program to Print all unique words of a String
- How to remove all elements from a Java List?
Advertisements