patternjavaMinor
Check string having unique character or not without additional data structure
Viewed 0 times
uniquewithouthavingcharacterdatastructurechecknotstringadditional
Problem
Here is my code without using additional data structures. Assuming all the characters are alphabetical and character checks are case-insensitive.
But it takes \$O(n^2)\$. Is there better way to do it, or some improvement?
To save complexity, I am using the other way, i.e. using an array.
Let me know your comments on this one as well.
public static boolean isRepeated(String s) {
if (s == null) {
return true;
}
int length = s.length();
for (int i = 0; i < length; i++) {
if (!checkChar(i, s.toUpperCase())) {// making all character to upper case.
return false;
}
}
return true;
}
private static boolean checkChar(int i, String s) {
char charAtI = s.charAt(i);
//check all the character before that index;
for (int k = 0; k < i; k++) {
if (s.charAt(k) == charAtI)
return false;
}
//check all the character after that index;
for (int l = i + 1; l < s.length(); l++) {
if (s.charAt(l) == charAtI)
return false;
}
return true;
}But it takes \$O(n^2)\$. Is there better way to do it, or some improvement?
To save complexity, I am using the other way, i.e. using an array.
public static boolean isRepeatedUsingArray(String s) {
int[] charArray = new int[26];
for(char c:s.toUpperCase().toCharArray()) {
if(charArray[(int)c - 65] == 1) {
return false;
}
charArray[(int)c - 65] = 1;
}
return true;
}Let me know your comments on this one as well.
Solution
- Function name seems to be misleading. It returns true if no repeating character is found, so I find
isNonRepeatedUsingArrayto be more appropriate.
- Basic input checks - when I began trying your function, I set up a few tests (included in the final code) and some of them crashed. Your code should handle at least basic input values such as null, empty string, string with spaces.
public static boolean isNonRepeatedUsingArray(String inputStr) {
if(inputStr == null || inputStr.isEmpty())
return false;
String s = inputStr.replaceAll(" ", "").toUpperCase();
// other character stripping may be put here- Checking for duplicates - The algorithm itself can be improved by thinking in terms of set. I am not very familiar with Java language and I have used
HashMapdata structure, but the bottom idea is the following:
if current element is already in the set, it is a duplicate. Else add it to the set.
This solution also has the advantage that avoids hardcoding the number of allowed characters (26).
Map charDict = new HashMap();
for (int i = 0; i < s.length(); i++) {
Character c = s.charAt(i);
if (charDict.containsKey(c))
return false;
charDict.put(c, true);
}HashMap operations have a complexity of
O(1), so function's overall complexity is O(n), where n is string length.[edit]
Based on
Bobby's correct observation, a HashSet is a better choice (elements only, no key and values):HashSet charSet = new HashSet();
for (int i = 0; i < s.length(); i++) {
Character c = s.charAt(i);
if (charSet.contains(c))
return false;
charSet.add(c);
}- Tests - it is a good habit to accompany a function with a test suite. It is particularly efficient when the code changes much (regressive testing) and helps to cover scenarios that are usually missed when developing (developers have a tendency to think in terms of making it work, not making it fail).
private static void printTestResult(String s) {
boolean testRes = isNonRepeatedUsingArray(s);
System.out.println("Repeating result for " + s + " is " + testRes);
}
public static void main(String []args){
printTestResult("a");
printTestResult("abcde");
printTestResult("some random text");
printTestResult("repeating");
printTestResult(null);
}The final code
import java.util.*;
public class HelloWorld{
public static boolean isNonRepeatedUsingArray(String inputStr) {
if(inputStr == null || inputStr.isEmpty())
return false;
String s = inputStr.replaceAll(" ", "").toUpperCase();
Map charDict = new HashMap();
for (int i = 0; i < s.length(); i++) {
Character c = s.charAt(i);
if (charDict.containsKey(c))
return false;
charDict.put(c, true);
}
return true;
}
private static void printTestResult(String s) {
boolean testRes = isNonRepeatedUsingArray(s);
System.out.println("Repeating result for " + s + " is " + testRes);
}
public static void main(String []args){
printTestResult("a");
printTestResult("abcde");
printTestResult("some random text");
printTestResult("repeating");
printTestResult(null);
}
}Code Snippets
public static boolean isNonRepeatedUsingArray(String inputStr) {
if(inputStr == null || inputStr.isEmpty())
return false;
String s = inputStr.replaceAll(" ", "").toUpperCase();
// other character stripping may be put hereMap<Character, Boolean> charDict = new HashMap<Character, Boolean>();
for (int i = 0; i < s.length(); i++) {
Character c = s.charAt(i);
if (charDict.containsKey(c))
return false;
charDict.put(c, true);
}HashSet<Character> charSet = new HashSet<Character>();
for (int i = 0; i < s.length(); i++) {
Character c = s.charAt(i);
if (charSet.contains(c))
return false;
charSet.add(c);
}private static void printTestResult(String s) {
boolean testRes = isNonRepeatedUsingArray(s);
System.out.println("Repeating result for " + s + " is " + testRes);
}
public static void main(String []args){
printTestResult("a");
printTestResult("abcde");
printTestResult("some random text");
printTestResult("repeating");
printTestResult(null);
}import java.util.*;
public class HelloWorld{
public static boolean isNonRepeatedUsingArray(String inputStr) {
if(inputStr == null || inputStr.isEmpty())
return false;
String s = inputStr.replaceAll(" ", "").toUpperCase();
Map<Character, Boolean> charDict = new HashMap<Character, Boolean>();
for (int i = 0; i < s.length(); i++) {
Character c = s.charAt(i);
if (charDict.containsKey(c))
return false;
charDict.put(c, true);
}
return true;
}
private static void printTestResult(String s) {
boolean testRes = isNonRepeatedUsingArray(s);
System.out.println("Repeating result for " + s + " is " + testRes);
}
public static void main(String []args){
printTestResult("a");
printTestResult("abcde");
printTestResult("some random text");
printTestResult("repeating");
printTestResult(null);
}
}Context
StackExchange Code Review Q#120143, answer score: 3
Revisions (0)
No revisions yet.