Log in
updated 11:29 AM UTC, May 4, 2016

Check if String has all unique chars.

 

Here is a commonly asked Interview question.

"Implement an algorithm to check if string has all unique chars. Please dont use any extra datastructures other than arrays."

here is a program which does exactly that and which is self explanatory:

package com.example;

public class StringUtilities {

	public static boolean isStringUniqueChars(String word) {

		boolean ret = true;
		int hashArray[] = new int[256];

		for (char e : word.toCharArray()) {

			if (hashArray[e] != 0) {
				ret = false;
				break;
			}
			hashArray[e] += 1;
		}

		return ret;
	}

	public static void main(String[] args) {
		String test = "abcdefghijklmnopqrstuvwxyz";
		System.out.println("Is string \"" + test
				+ "\" a unique chars string = "
				+ (isStringUniqueChars(test) ? "Yes" : "No"));

	}

}

Output:

Is string "abcdefghijklmnopqrstuvwxyz." a unique chars string = Yes


Powered by Bullraider.com
Follow Us on Twitter
Find Us on Facebook
Follow Us on Google
Follow Us on Pinterest