muntean.jenea
BAN USERI think the idea is to use hash
if key exists then we remove it from hash else we add it to hash , as result the hash will remain just with those that are unique and is O(n)
Thanks, fixed, 64 is because use from [1-n]
- muntean.jenea September 26, 2013the output is mirrored
- muntean.jenea September 25, 2013public class ExcelColumn {
public static String excelColumnName(int num){
if(num==0)
return "";
if (num<=26)
return Character.toString((char) (num+64));
else {
int firstpart=num/26;
int end=num%26;
end=end==0?26:end;
if (firstpart >26)
return excelColumnName((num-end)/26)+Character.toString((char) (end+64));
else
return Character.toString((char) ((num-end)/26+64))+Character.toString((char) (end+64));
}
}
public static void main(String[] args) {
for (int i=24;i<30;i++){
System.out.println(excelColumnName(i));
}
System.out.println(excelColumnName(746));
}
}
X
Y
Z
AA
AB
AC
ABR
public class StringConverting {
public static String encodeStr(String str) {
int charposition = 0;
int cursor = 0;
StringBuilder result = new StringBuilder();
for (int i = 0; i < str.length(); i++) {
if (str.charAt(charposition) != str.charAt(cursor)) {
result.append(cursor - charposition);
result.append(str.charAt(charposition));
charposition = cursor;
}
cursor++;
}
result.append(cursor - charposition);
result.append(str.charAt(charposition));
return result.toString();
}
public static void main(String[] args) {
String str = "aaaabbcdddddddddzzzooo";
System.out.println(encodeStr(str));
str = "aaaa";
System.out.println(encodeStr(str));
str = "abcdefg";
System.out.println(encodeStr(str));
}
4a2b1c9d3z3o
4a
1a1b1c1d1e1f1g
public class Coins {
public static int countCoins(int sum, int[] arr,
HashMap<Integer, Integer> map) {
if (sum == 0)
return 0;
else if (sum < 0)
return -1;
else {
int minim = Integer.MAX_VALUE;
int minimOnPath = Integer.MAX_VALUE;
for (int i = 0; i < arr.length; i++) {
if (map.containsKey(sum - arr[i]))
minimOnPath = 1 + map.get(sum - arr[i]);
else {
minimOnPath = 1 + countCoins(sum - arr[i], arr, map);
map.put(sum - arr[i], minimOnPath - 1);
}
if (minimOnPath > 0)
minim = Math.min(minim, minimOnPath);
}
if (minim != Integer.MAX_VALUE)
return minim;
else
return -1;
}
}
public static void main(String[] args) {
HashMap<Integer, Integer> mapValueCoins = new HashMap<>();
int[] coins = { 1, 3, 7 };
int sum = 22;
System.out.println(countCoins(sum, coins, mapValueCoins));
mapValueCoins = new HashMap<>();
int[] coins2 = { 5, 5, 5, 5, 5 };
sum = 11;
System.out.println(countCoins(sum, coins2, mapValueCoins));
mapValueCoins = new HashMap<>();
int[] coins3 = { 5, 5, 5, 5, 75 };
sum = 75;
System.out.println(countCoins(sum, coins3, mapValueCoins));
}
}
this solution work for n unique numbers and rest are twice.
- muntean.jenea October 01, 2013