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
Please enter at least three characters to search
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

Because the DevOps movement has redefined engineering responsibilities, SREs now have to become stewards of observability strategy.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Related

  • DataWeave: Play With Dates (Part 1)
  • Tired of Messy Code? Master the Art of Writing Clean Codebases
  • The Long Road to Java Virtual Threads
  • Exploring Exciting New Features in Java 17 With Examples

Trending

  • Rust, WASM, and Edge: Next-Level Performance
  • Enforcing Architecture With ArchUnit in Java
  • Monolith: The Good, The Bad and The Ugly
  • Event-Driven Microservices: How Kafka and RabbitMQ Power Scalable Systems
  1. DZone
  2. Data Engineering
  3. Data
  4. Decide if a String Has Duplicates

Decide if a String Has Duplicates

Check out this post and decide if a string has duplicates.

By 
Zoltan Raffai user avatar
Zoltan Raffai
·
May. 31, 19 · Tutorial
Likes (4)
Comment
Save
Tweet
Share
22.7K Views

Join the DZone community and get the full member experience.

Join For Free

It worth knowing that I started to solve problems with the Cracking the Coding Interview book, which I wrote about under the Resources menu.

Suddenly, I had the idea to show you some real world algorithmic problems from this book and their solution.

Hope you will enjoy this journey with me. Mostly, the solutions will be from the book. Try to solve the problem first, then see the solution. I have tried to do them as well as the first step.

Let us see our first example.

#1 Is Unique

Implement an algorithm to determine if a string has all unique characters. What if you cannot use additional data structures?

It is a tricky one for who did not do that kind of stuff before. An average programmer would do a for cycle store the character to an array and compare every single one to the previous ones. It works, but there are better solutions.

You should first ask your interviewer if the string is an ASCII string or a Unicode string. Asking this question will show an eye for detail and a solid foundation in computer science. We’ll assume for simplicity the character set is ASCII. If this assumption is not valid, we would need to increase the storage size.

One solution is to create an array of boolean values, where the flag at index i indicates whether character I in the alphabet is contained in the string. The second time you see this character you can immediately return false.

We can also immediately return false if the string length exceeds the number of unique characters in the alphabet. After all, you can’t form a string of 280 unique characters out of a 128-character alphabet.

Solution

boolean isUniqueChars(String str) {
 if (str.length() > 128) return false;

   boolean[] char_set = new boolean[128];
   for (int i= 0; i < str.length(); i++) {
     int val= str.charAt(i);
     if (char_set[val]) {//Already found this char in string
       return false;
      }
    char_set[val] = true;
   }
  return true;
}


Let us see a more advanced one.

The time complexity for this code isO( n ), where n is the length of the string. The space complexity is O(l ). (You could also argue the time complexity is 0(1), since the for loop will never iterate through more than 128 characters.) If you didn’t want to assume the character set is fixed, you could express the complexity as O( c) space and O(min ( c, n)) or O( c) time, where c is the size of the character set.

We can reduce our space usage by a factor of eight by using a bit vector. We will assume, in the below code, that the string only uses the lowercase letters a through z. This will allow us to use just a single int.

boolean isUniqueChars(String str) {
  int checker= 0;
  for (int i= 0; i < str.length(); i++) {
     int val= str.charAt(i) - 'a';
    if ((checker & (1 << val)) > 0) {
      return false;
    }
    checker I= (1 << val);
  }
 return true;
} 


If we can’t use additional data structures, we can do the following:

1. Compare every character of the string to every other character of the string. This will take 0( n 2) time and 0(1) space.

2. If we are allowed to modify the input string, we could sort the string in O(n log(n)) time and then linearly check the string for neighboring characters that are identical. Careful, though: many sorting algorithms take up extra space.

These solutions are not as optimal in some respects but might be better, depending on the constraints of the problem.

Let us talk about this task in the comments below. If you have any questions, please let me know.

Data Types Strings

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

Opinions expressed by DZone contributors are their own.

Related

  • DataWeave: Play With Dates (Part 1)
  • Tired of Messy Code? Master the Art of Writing Clean Codebases
  • The Long Road to Java Virtual Threads
  • Exploring Exciting New Features in Java 17 With Examples

Partner Resources

×

Comments
Oops! Something Went Wrong

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:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!