Interview Question
Country: United States
Interview Type: In-Person
Could you explain it.I did it for other uniqueness of element in an array.
But I forget concept .Please tell me
int checker = 0;
// readt each char one by one
for (int i = 0; i < str.length(); ++i) {
// get unique ASCII value for current char
int val = str.charAt(i) - 'a';
// if bit is not on for that "val"th place
if ((checker & (1 << val)) > 0)
return false;
// Turn on the bit at val place and or to checker
checker |= (1 << val);
}
return true;
// Checker is a bit map. We have one to one mapping between "n"th bit and "n"th char
// Just turning on that bit if char present .
Please let me know if it still unclear
In 7 byte ascii there could be 128 different characters hence you would require 128 bits. I guess using a simple it would result in overflow.
void findUniqueCharactersInString() {
char[] s = "Write a program to find the unique char in the ascii string.".toCharArray();
int i = 0;
boolean[] chars = new boolean[512];
while (i < s.length) {
int j = i + 1;
while (j < s.length && !chars[s[i]]) {
if (s[i] == s[j]) {
break;
}
j++;
}
if (j == s.length) {
System.out.println(s[i] + " ");
}
chars[s[i]] = true;
i++;
}
}
As the string is ASCII we can easily use a bitmap to count the occurrence of each character. But as the question asks for uniqueness, counting is not required and one bit per element should be sufficient. Anyways here is the code for general count based bitmap solution
long[] bitmap = new long[256];
public void printUniqChar(char[] s) {
for(char c:s)
bitmap[c]++;
for(int i=0; i<bitmap.length; i++)
if(bitmap[i] == 1)
System.out.printf("UNIQUE = %c\n", i);
}
#include<iostream>
#include <map>
using namespace std;
int main(){
char arr[]="hello";
map<char,int> mymap;
map<char,int>::iterator it;
int i=0;
while(arr[i]){
if(!mymap.count(arr[i]))
mymap[arr[i]]=0;
else
mymap[arr[i]]=1;
i++;
}
for (it=mymap.begin(); it != mymap.end(); it++ )
cout << (*it).first << " => " << (*it).second << endl;
for(int i=0;i<10;i++){
if(!mymap.find(arr[i])->second)
cout<<"unique first number is:"<<arr[i]<<endl;
//break;
}
}
I've been working on the algorithm and here's what I noticed that would also work. It makes the algorithm easier to understand when you exercise it by hand:
public static boolean isUniqueChars(String str) {
if (str.length() > 26) { // Only 26 characters
return false;
}
int checker = 0;
for (int i = 0; i < str.length(); i++) {
int val = str.charAt(i) - 'a';
int newValue = Math.pow(2, val) // int newValue = 1 << val
if ((checker & newValue) > 0) return false;
checker += newValue // checker |= newValue
}
return true;
When we get the value of `val` (0-25), we could either shift 1 to the right by the value of `val`, or we could use the power of 2s.
Also, for as long as the `((checker & newValue) > 0)` is false, the new checker value is generated when we sum up the old checker value and the `newValue`.
public static boolean isUniqueChars(String str) {
- limca500ml January 23, 2013int checker = 0;
for (int i = 0; i < str.length(); ++i) {
int val = str.charAt(i) - 'a';
if ((checker & (1 << val)) > 0)
return false;
checker |= (1 << val);
}
return true;
}