www.fatihkabakci.com

Personal Website and Computer Science TUR EN

Determine Unique Characters in String

Last update: 10/18/2018 1:58:00 AM

Written by:Fatih KABAKCI

The question is to determine if a string has all unique characters. First solution easily comes up to mind is to implement brute force solution of this problem. Get each character in an upper loop, then compare rest of other characters in an inner loop. Let's look at the the first solution which its complexity is O(n2) in below.

Brute Force

/**
 * 
 * @author fkabakci
 * Find out if a string has all unique characters -- Brute Force O(n2) solution
 */
class Solution {
	public static boolean isUnique(String str) {
		for(int i = 0; i < str.length(); i++) {
			for(int j = i + 1; j < str.length(); j++) {
				if(str.charAt(i) == str.charAt(j))
					return false;
			}
		}
		return true;
	}

	public static void main(String[] args) {
		String computer = "Computer";
		String printer = "Printer";

		System.out.println(isUnique(computer));
		System.out.println(isUnique(printer));
	}
}

O(n) Solution

We know that all printable ASCII characters are up to 127. So all possible characters are ordered till ASCII 127. Therefore, while we get each character, we could have a boolean buffer array that stores those characters whether they appeared or not as true and false values. The indices of this buffer array uses the ASCII code of the characters in the string.

/**
 * 
 * @author fkabakci
 * Find out if a string has all unique characters -- O(n) solution
 */
class Solution {
	public static boolean isUnique(String str) {
		int length = str.length();
		// ASCII
		if (length > 128) {
			return false;
		}
		boolean[] flag = new boolean[128];
		for (int i = 0; i < length; i++) {
			int code = str.codePointAt(i);
			if (flag[code])
				return false;
			flag[code] = true;
		}
		return true;
	}

	public static void main(String[] args) {
		String computer = "Computer";
		String printer = "Printer";

		System.out.println(isUnique(computer));
		System.out.println(isUnique(printer));
	}
}

Now, we have reached out the O(n) solution by just iterating the array once.

There has been no comment yet

Name:


Question/Comment
   Please verify the image




The Topics in Computer Science

Search this site for





 

Software & Algorithms

icon

In mathematics and computer science, an algorithm is a step-by-step procedure for calculations. Algorithms are used for calculation, data processing, and automated reasoning.

Programming Languages

icon

A programming language is a formal constructed language designed to communicate instructions to a machine, particularly a computer. It can be used to create programs to control the behavior of a machine. Java,C, C++,C#

Database

icon

A database is an organized collection of data. The data are typically organized to model aspects of reality in a way that supports processes requiring information.

Hardware

icon

Computer hardware is the collection of physical elements that constitutes a computer system. Computer hardware refers to the physical parts or components of a computer such as the monitor, memory, cpu.

Web Technologies

icon

Web development is a broad term for the work involved in developing a web site for the Internet or an intranet. Html,Css,JavaScript,ASP.Net,PHP are one of the most popular technologies. J2EE,Servlet, JSP,JSF, ASP

Mobile Technologies

icon

Mobile application development is the process by which application software is developed for low-power handheld devices, such as personal digital assistants, enterprise digital assistants or mobile phones. J2ME

Network

icon

A computer network or data network is a telecommunications network that allows computers to exchange data. In computer networks, networked computing devices pass data to each other along data connections.

Operating Systems

icon

An operating system is software that manages computer hardware and software resources and provides common services for computer programs. The OS is an essential component of the system software in a computer system. Linux,Windows

Computer Science

icon

Computer science is the scientific and practical approach to computation and its applications.A computer scientist specializes in the theory of computation and the design of computational systems.