w3resource

Java Exercises: Check a string follows a specified pattern

Java Basic: Exercise-199 with Solution

Write a Java program to check a string follows a given pattern.

Example pattern:
Given pattern = "xyyx", str = "red black black red", return true.
Given pattern = "xyyx", str = "red black black green", return false.
Given pattern = "xxxx", str = "red black black red", return false.
Given pattern = "xxxx", str = "red red red red", return true.

Sample Solution:

Java Code:

import java.util.*;
public class Solution {
 public static void main(String[] args) {
  String str = "red black black red";
  //String str = "red red red red"; 
  String pattern = "xyxx";
  //String pattern = "xxxx";
  System.out.print("Is the string and pattern matched? " + word_Pattern_Match(pattern, str));
 }

 public static boolean word_Pattern_Match(String pattern, String str) {
  char[] word_pattern = pattern.toCharArray();
  String[] words = str.split(" ");

  Map < Character, String > map = new HashMap < > ();
  Set < String > set = new HashSet < > ();

  for (int i = 0; i < word_pattern.length; i++) {
   if (map.containsKey(word_pattern[i])) {
    if (!map.get(word_pattern[i]).equals(words[i])) {
     return false;
    }
    continue;
   }

   if (set.contains(words[i])) {
    return false;
   }
   map.put(word_pattern[i], words[i]);
   set.add(words[i]);
  }
  return true;
 }
}

Sample Output:

Is the string and pattern matched? false

Pictorial Presentation:

Java exercises: Check a string follows a specified pattern
Java exercises: Check a string follows a specified pattern
Java exercises: Check a string follows a specified pattern
Java exercises: Check a string follows a specified pattern

Flowchart:

Flowchart: Java exercises: Check a string follows a specified pattern

Java Code Editor:

Company:  Uber Dropbox

Contribute your code and comments through Disqus.

Previous: Write a Java program to get the position of a given prime number.
Next: Write a Java program to remove duplicate letters and arrange in lexicographical order from a given string which contains only lowercase letters.

What is the difficulty level of this exercise?



Java: Tips of the Day

Array vs ArrayLists:

The main difference between these two is that an Array is of fixed size so once you have created an Array you cannot change it but the ArrayList is not of fixed size. You can create instances of ArrayLists without specifying its size. So if you create such instances of an ArrayList without specifying its size Java will create an instance of an ArrayList of default size.

Once an ArrayList is full it re-sizes itself. In fact, an ArrayList is internally supported by an array. So when an ArrayList is resized it will slow down its performance a bit as the contents of the old Array must be copied to a new Array.

At the same time, it's compulsory to specify the size of an Array directly or indirectly while creating it. And also Arrays can store both primitives and objects while ArrayLists only can store objects.

Ref: https://bit.ly/3o8L2KH