Ozan Eren Bilgen
BAN USERhttp://linkedin.com/in/ozanerenbilgen
The object must be either a NSObject or subclass it. Therefore you cannot store primitive types.
- Ozan Eren Bilgen October 19, 2012If 2 strings are anagram, they contain same number of same characters, as in "tom marvolo riddle" and "i am lord voldemort".
Lowercase hack won't really work due to the localization.
public boolean isAnagram(String s1, String s2)
{
if (s1 == null || s2 == null)
{
return false;
}
int length = s1.length();
if (length != s2.length())
{
return false;
}
HashMap<Character, Integer> map1 = new HashMap<Character, Integer>();
HashMap<Character, Integer> map2 = new HashMap<Character, Integer>();
for (int i = 0; i<length; i++)
{
Character c = s1.charAt(i);
if (map1.containsKey(c))
{
Integer x = map1.get(c);
map1.put(c, x + 1);
}
else
{
map1.put(c, 1);
}
c = s2.charAt(i);
if (map2.containsKey(c))
{
Integer x = map1.get(c);
map2.put(c, x + 1);
}
else
{
map2.put(c, 1);
}
}
if (map1.size() != map2.size())
{
return false;
}
Iterator it = map1.entrySet().iterator();
while (it.hasNext())
{
Map.Entry pairs = (Map.Entry)it.next();
Character key = (Character)pairs.getKey();
if (!map2.containsKey(key) || !map2.get(key).equals(pairs.getValue()))
{
return false;
}
}
return true;
}
Here is what I wrote for integers -- basically same thing.
To calculate the permutation of an array of objects, foreach object in the array, add the object to the permutations of the reminder of the group recursively.
public ArrayList<ArrayList<Integer>> findPermutations(ArrayList<Integer> integers)
{
ArrayList<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>();
if (integers == null || integers.size() == 0)
{
return result;
}
for (int i = 0; i < integers.size(); i++)
{
ArrayList<Integer> subArray = new ArrayList<Integer>();
for (int j = 0; j < integers.size(); j++)
{
if (!integers.get(j).equals(integers.get(i)))
{
subArray.add(integers.get(j));
}
}
ArrayList<ArrayList<Integer>> subResult = this.findPermutations(subArray);
for (ArrayList<Integer> subPerm : subResult)
{
subPerm.add(integers.get(i));
result.add(subPerm);
}
ArrayList<Integer> selfResult = new ArrayList<Integer>();
selfResult.add(integers.get(i));
result.add(selfResult);
}
return result;
}
So for [1, 2, 3, 4] you'll get
[4, 3, 2, 1]
[3, 2, 1]
[3, 4, 2, 1]
[4, 2, 1]
[2, 1]
[4, 2, 3, 1]
[2, 3, 1]
[2, 4, 3, 1]
[4, 3, 1]
[3, 1]
[3, 2, 4, 1]
[2, 4, 1]
[2, 3, 4, 1]
[3, 4, 1]
[4, 1]
[1]
[4, 3, 1, 2]
[3, 1, 2]
[3, 4, 1, 2]
[4, 1, 2]
[1, 2]
[4, 1, 3, 2]
[1, 3, 2]
[1, 4, 3, 2]
[4, 3, 2]
[3, 2]
[3, 1, 4, 2]
[1, 4, 2]
[1, 3, 4, 2]
[3, 4, 2]
[4, 2]
[2]
[4, 2, 1, 3]
[2, 1, 3]
[2, 4, 1, 3]
[4, 1, 3]
[1, 3]
[4, 1, 2, 3]
[1, 2, 3]
[1, 4, 2, 3]
[4, 2, 3]
[2, 3]
[2, 1, 4, 3]
[1, 4, 3]
[1, 2, 4, 3]
[2, 4, 3]
[4, 3]
[3]
[3, 2, 1, 4]
[2, 1, 4]
[2, 3, 1, 4]
[3, 1, 4]
[1, 4]
[3, 1, 2, 4]
[1, 2, 4]
[1, 3, 2, 4]
[3, 2, 4]
[2, 4]
[2, 1, 3, 4]
[1, 3, 4]
[1, 2, 3, 4]
[2, 3, 4]
[3, 4]
[4]
Dynamic binding moves the message-method connection to runtime, as opposed to compile time (see v_table in C). Languages having this feature (e.g. Objective-C) permit the sending of a message to an object that may not respond. While it brings flexibility on development, often causes crashes in runtime. At best you see "warning: 'MyClass' may not respond to -foo" in the compiler output.
- Ozan Eren Bilgen October 19, 2012