Hugh
BAN USERprivate boolean areSameBySwap(String st1, String st2) {
if (st1.length() != st2.length()) return false;
int count = 0;
int prev = -1;
int cur = -1;
for (int i = 0; i < st1.length(); i++) {
if (st1.charAt(i) != st2.charAt(i)) {
count++;
if (count > 2) return false;
prev = cur;
cur = i;
}
}
return count == 2 &&
st1.charAt(prev) == st2.charAt(cur) &&
st1.charAt(cur) == st2.charAt(prev);
}
recursion:
private List<String> allBinaryStrings(int k) {
char[] arr = new char[k];
List<String> res = new ArrayList<>();
StringBuilder initString = new StringBuilder();
for (int i = 0; i < k; i++){
initString.append("0");
}
allBinaryStringHelper(res, arr, 0, initString.toString());
return res;
}
private void allBinaryStringHelper(List<String> res, char[] arr, int index, String initString) {
if (index >= arr.length) {
StringBuilder t = new StringBuilder();
for (char c : arr) {
t.append(c);
}
res.add(t.toString());
return;
}
char current = initString.charAt(index);
char[] charSet = new char[]{'0', '1'};
if (current == '0') {
for (char aCharSet : charSet) {
arr[index] = aCharSet;
allBinaryStringHelper(res, arr, index + 1, initString);
}
}
if (current == '1') {
for (char aCharSet : charSet) {
arr[index] = aCharSet;
allBinaryStringHelper(res, arr, index + 1, initString);
}
}
}
I don't think you need to use Stack:
private int[] howManyMoreDays2(int[] array) {
// last element is always 'nothing'
int[] res = new int[array.length];
res[array.length - 1] = -1;
int start = 0;
int i;
while (start < array.length) {
for (i = start + 1; i < array.length - 1; i++) {
if (array[start] < array[i]) {
res[start] = i - start;
break;
}
}
if (i == array.length - 1) {
res[start] = -1;
}
start++;
}
return res;
}
This problem is too simple or I am missing something:
private int longestChain(List<String[]> list) {
HashMap<String, String> map = new HashMap<>();
for (String[] array : list) {
map.put(array[0], array[1]);
}
int max = 0;
int count = 0;
Set<String> keys = map.keySet();
for (String k : keys) {
count = 1;
String val = map.get(k);
while(map.containsKey(val)) {
count++;
val = map.get(val);
}
max = Math.max(count, max);
}
return max;
}
I don't think you'd need a new array to keep track of the bIndex. Here's a simpler solution:
private List<int[]> getSmallestSums(int[] a, int[] b, int k) {
List<int[]> res = new ArrayList<>();
res.add(new int[]{a[0], b[0]});
int aIndex = 0;
int bIndex = 0;
while(res.size() < k) {
if(a[aIndex + 1] < b[bIndex + 1]) {
aIndex++;
}
else {
aIndex = 0;
bIndex += 1;
}
res.add(new int[]{a[aIndex], b[bIndex]});
}
return res;
}
private List<Set<String>> uniqueCharSet(List<String> list) {
HashMap<Set<Character>, Set<String>> map = new HashMap<>();
for (String word : list) {
Set<Character> set = new HashSet<>();
for (char c : word.toCharArray()) {
set.add(c);
}
if (map.get(set) != null) {
Set<String> tmpset = map.get(set);
tmpset.add(word);
map.put(set, tmpset);
} else {
Set<String> tmpset = new HashSet<>();
tmpset.add(word);
map.put(set, tmpset);
}
}
List<Set<String>> res = new ArrayList<>();
Set<Set<Character>> keySet = map.keySet();
for (Set<Character> k : keySet) {
Set<String> s = map.get(k);
res.add(map.get(k));
}
return res;
}
Easy Java solution:
private int largestNumberDivBy3_noRecursion(List<Integer> list) {
if (list.isEmpty()) {
return 0;
}
// sort the numbers
Collections.sort(list);
Collections.reverse(list);
StringBuilder sb = new StringBuilder();
for (int i : list) {
sb.append(i);
}
int end = sb.length() - 1;
StringBuilder word = sb;
while (sb.length() > 0) {
int num = Integer.parseInt(sb.toString());
if (num % 3 == 0) return num;
if (end < 0) {
// remove the last char and reset the 'end'
word = word.deleteCharAt(word.length() - 1);
end = word.length() - 1;
}
sb = new StringBuilder(word);
sb = sb.deleteCharAt(end);
end --;
}
return 0;
}
A cleaner implementation:
- Hugh May 29, 2018