[Interview] Check if String has Unique Characters
Implement an algorithm to determine if a string has all unique characters What if you can not use additional data structure?
Time Complexity: \(O(n)\)
Space Complexity: \(O(n)\)
Here is my Video Explanation
package com.Array_Strings;
/**
* @Question: 1-1. Implement an algorithm to determine if a string has all
* unique characters What if you can not use additional data
* structure?
*/
public class Ans1_1 {
public boolean helper(String str) {
boolean[] map = new boolean[256];
for (int i = 0; i < str.length(); i++) {
int val = str.charAt(i);
if (map[val])
return false;
map[val] = true;
}
return true;
}
}
Complexity Analysis:Time Complexity: \(O(n)\)
Space Complexity: \(O(n)\)
Here is my Video Explanation
Comments
Post a Comment