DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Commonly Occurring Errors in Microsoft Graph Integrations and How To Troubleshoot Them (Part 4)
  • Build a Java Microservice With AuraDB Free
  • Mastering Concurrency: An In-Depth Guide to Java's ExecutorService
  • Optimizing Java Applications: Parallel Processing and Result Aggregation Techniques

Trending

  • Concourse CI/CD Pipeline: Webhook Triggers
  • Emerging Data Architectures: The Future of Data Management
  • Medallion Architecture: Efficient Batch and Stream Processing Data Pipelines With Azure Databricks and Delta Lake
  • Why High-Performance AI/ML Is Essential in Modern Cybersecurity
  1. DZone
  2. Data Engineering
  3. AI/ML
  4. Permutation Check in Java

Permutation Check in Java

Check out this post to learn more about solving permutation check problems in Java.

By 
Zoltan Raffai user avatar
Zoltan Raffai
·
Jun. 17, 19 · Presentation
Likes (9)
Comment
Save
Tweet
Share
15.3K Views

Join the DZone community and get the full member experience.

Join For Free

Here is a new Algorithm problem continuing our season. If you missed the first article, check it here: Decide if a String has duplicates.

Our next problem description is the following:

Check Permutation: Given two strings, write a method to decide if one is a permutation of the other.

Here, we should ask the following questions:

  • Is the permutation comparison case sensitive?
    • That means: dog equal to Dog?
  • We should ask if whitespace characters are significant?
  • What is the character coding?

Our assumption is that our comparison is case sensitive, whitespaces are significant, and we use ASCII.

Solution #1

Think about the difference between two strings and whether they are the permutations of each other. The solution is simple: They have the same character count and only the character order differs.

If you find this statement, the task becomes quite straightforward.

We just need to compare the sorted versions of the strings.

        private static final String perm1 = "abcdefga";
private static final String perm2 = "abcdfega";


private String sort(String s) {

char[] content = s.toCharArray();
java.util.Arrays.sort(content);
return new String(content);
}

public Boolean getIsPermutations() {
if(perm1.length() != perm2.length()) return false;
return sort(perm1).equals(sort(perm2));
}


Although the above example is very clean and simple, performance is very important, so we may need to implement it in a different way.

Solution #2

We can implement an algorithm through the definition of "two words with the same character counts."

The below algorithm counts how many times each character appears, and then, it compares the two arrays.

boolean permutation(String s, String t) {
  if (s.length() != t.length()) {
    return false;
  }
  int[] letters = new int[128];// Assumption that it is in ASCII, you should always care about it.
  char[] s_array = s.toCharArray();
  for (char c : s_array) { // count number of each char in s.
    letters[c]++;
  }
  for (int i= 0; i < t.length(); i++) {
    int c = (int) t.charAt(i);
    letters[c]--;
    if (letters[c] < 0) {
      return false;
    }
  }
return true; 
}


Notes

Hope you enjoyed my latest programming puzzle. I recommend you to try to solve them before going through the solution.

And feel free to share your ideas in the comments below.

This problem is from the Cracking the Coding Interview book, which I wrote about under the Resources menu.

Java (programming language) Strings Algorithm Comparison (grammar) Coding (social sciences) ASCII Data Types Task (computing) Book

Published at DZone with permission of Zoltan Raffai, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Commonly Occurring Errors in Microsoft Graph Integrations and How To Troubleshoot Them (Part 4)
  • Build a Java Microservice With AuraDB Free
  • Mastering Concurrency: An In-Depth Guide to Java's ExecutorService
  • Optimizing Java Applications: Parallel Processing and Result Aggregation Techniques

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: