Amit
BAN USERpublic boolean isIsomorphic(String s, String t) {
if (s.length() != t.length()) return false;
Set<Character> set = new HashSet<Character>();
boolean isomorphic = true;
Map<Character, Character> map = new HashMap<Character, Character>();
for (int i = 0; i < s.length(); ++i) {
if (!map.containsKey(s.charAt(i))) {
if (set.contains(t.charAt(i))) {
isomorphic = false;
break;
} else {
map.put(s.charAt(i), t.charAt(i));
set.add(t.charAt(i));
}
} else {
if (map.get(s.charAt(i)) != t.charAt(i)) {
isomorphic = false;
break;
}
}
}
return isomorphic;
}
public void findXLcolumnByNumber(int number){
List<Character> lst = new ArrayList<Character>();
while (number > 0){
int b = number % 26;
number = number / 26;
b = b == 0 ? 26 : b;
lst.add((char) (b + 64));
}
for (int i = lst.size() - 1; i >= 0; i--)System.out.print(lst.get(i));
System.out.println();
}
hi, this code works for lowercase letters only. I have taken an int value "val" and used it's each bit for storing the presence of a alphabet. So, if I get 'a' in string, I calculate 'a' - 96 = 1 and turn the first bit on. If I find that bit is already on, I do not add it in buffer. if the bit is not on, I do a or operation to turn that on.
- Amit January 13, 2013I think we can use a 32 bit integer for auxilary storage if we consider 26 english alphabet. For more, we can pick 64 bit integer; or multiple integer. The following example shows the idea. It will remove the duplicates, the input string should be in lowercase.
public String removedups(String str) {
StringBuffer buffer = new StringBuffer();
int flag = 0;
for (int i = 0; i < str.length(); i++) {
char ch = str.charAt(i);
int bit = ((int)ch - 96);
int val = 1;
val = val << bit;
if ((flag & val) == 0) {
buffer.append(ch);
flag |= val;
}
}
return buffer.toString();
}
my point was since the number is unsigned int, that means 32 bit. here we have a finite set of numbers.
- Amit August 30, 2015