Implement an algorithm to determine if a string has all unique characters without using any additional data structure.
/**
* Implement an algorithm to determine if a string has all unique characters.
* What if you cannot use additional data structures?
*/
package com.rohan.string.unique;
/**
* @author ROHAN
*
*/
public class UniqueString {
public static void main(String[] args) {
String str = "Rohanr";
if (determineUniqueCharacter(str))
System.out.println("String characters are unique");
else
System.out.println("Sring characters are having duplicate");
}
public static boolean determineUniqueCharacter(String string) {
for (int i = 0; i < string.length() - 1; i++) {
for (int j = i + 1; j < string.length(); j++) {
if (string.charAt(i) == string.charAt(j)) {
return false;
}
}
}
return true;
}
}
Comments
Post a Comment