import java.util.Hashtable;
import java.util.List;
import java.util.Set;
class Solution {
public String mostCommonWord(String paragraph, String[] banned) {
String paragraphProcessed = paragraph.replace("!", "").replace("?", "").replace("'", "").replace(",", "").replaceAll(";", "").replace(".", "");
String input[] = paragraphProcessed.split(" ");
for (int i = 0; i < input.length; i++)
input[i] = input[i].toLowerCase();
Hashtable f = new Hashtable();
for (String s : input) {
if (f.get(s) == null)
f.put(s, 1);
else
f.replace(s, f.get(s) + 1);
}
for (int i = 0; i < banned.length; i++) {
if (f.get(banned[i]) != null)
f.remove(banned[i]);
}
int mostFrequ = Integer.MIN_VALUE;
String mostFrequString = "";
Set keys = f.keySet();
for (String key : keys) {
if (f.get(key) > mostFrequ) {
mostFrequ = f.get(key);
mostFrequString = key;
}
}
return mostFrequString;
}
}
Category Archives: Computer Science
611. Valid Triangle Number
class Solution {
public int triangleNumber(int[] nums) {
int count = 0;
for (int i = 0; i < nums.length; i++) {
for (int j = i+1; j < nums.length; j++) {
for (int k = j+1; k nums[k] &&
nums[i] + nums[k] > nums[j]&&
nums[j] + nums[k] > nums[i]) {
count++;
}
}
}
}
return count;
}
}
653. Two Sum IV – Input is a BST
653. Two Sum IV – Input is a BST
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public boolean findTarget(TreeNode root, int k) {
ArrayList data = new ArrayList();
traverers(root, data);
for (int i = 0 ; i < data.size() ; i++)
for (int j = 0 ; j < data.size() && i!=j ; j++)
if (data.get(i) + data.get(j) == k)
return true;
return false;
}
private void traverers(TreeNode root, ArrayList data) {
if (root != null) {
data.add(root.val);
traverers(root.left, data);
traverers(root.right, data);
}
}
}
590. N-ary Tree Postorder Traversal
590. N-ary Tree Postorder Traversal
/*
// Definition for a Node.
class Node {
public int val;
public List children;
public Node() {}
public Node(int _val,List _children) {
val = _val;
children = _children;
}
};
*/
class Solution {
public List postorder(Node root) {
List out = new ArrayList();
postorder(root, out);
return out;
}
private void postorder(Node root, List out) {
if( null != root){
for (int i = 0; i < root.children.size(); i++)
postorder(root.children.get(i), out);
out.add(root.val);
}
}
}
788. Rotated Digits
class Solution {
public static int rotatedDigits(int N) {
int good = N;
for (int i = 1; i <= N; i++) {
boolean valid = true;
int current = i;
int converted = i;
inner: {
int m = 1;
while (current != 0) {
int digit = current % 10; // get current digit
// Convert digits regard to the specific rule
if (digit == 2) {
converted -= (digit * m);
converted += (5 * m);
} else if (digit == 5) {
converted -= (digit * m);
converted += (2 * m);
} else if (digit == 6) {
converted -= (digit * m);
converted += (9 * m);
} else if (digit == 9) {
converted -= (digit * m);
converted += (6 * m);
}
if (digit == 3 || digit == 4 || digit == 7) {
valid = false;
good--;
break inner;
}
current /= 10;
m *= 10;// next digit
}
}
if (converted == i && valid)
good--;
}
return good;
}
}
860. Lemonade Change
class Solution {
public static boolean lemonadeChange(int[] bills) {
HashMap d = new HashMap();
d.put(5, 0);
d.put(10, 0);
for (int i = 0; i < bills.length; i++) {
if (bills[i] == 5) {
d.put(5, d.get(5) + 1);
} else if (bills[i] == 10) {
if (d.get(5) <= 0)
return false;
d.put(5, d.get(5) - 1);
d.put(10, d.get(10) + 1);
} else if (bills[i] == 20) {
if ((d.get(10) - 1) < 0) { // no 10's
if (d.get(5) - 3 < 0)
return false;
} else {// there are 10's
if (d.get(5) - 1 = 1 && d.get(5) >= 1) {
d.put(5, d.get(5) - 1);
d.put(10, d.get(10) - 1);
} else if (d.get(5) - 3 >= 0) {
d.put(5, d.get(5) - 3);
}
}
}
return true;
}
}
Multiples of 3 and 5
Answer isĀ 233168 pragmatically.
I did not use the Math to get the answer.
public class Main {
public static int multiple3and5below1000() {
int sum = 0;
for (int i = 3; i < 1000; i++) {
if ((i >= 3 && ((i % 3) == 0) || (i >= 5 && ((i % 5) == 0))))
sum += i; }
return sum; }
public static void main(String[] args) {
System.out.println(multiple3and5below1000()); }
}
448. Find All Numbers Disappeared in an Array
448. Find All Numbers Disappeared in an Array
class Solution {
public List findDisappearedNumbers(int[] nums) {
boolean[] data = new boolean [nums.length];
for ( int i = 0 ; i < nums.length ; i++) {
data[nums[i] - 1 ] = true;
}
List output = new ArrayList();
for ( int i = 0 ; i < data.length ; i++) {
if ( ! data[i] )output.add(i+1);
}
return output;
}
}
690. Employee Importance
/*
// Employee info
class Employee {
// It's the unique id of each node;
// unique id of this employee
public int id;
// the importance value of this employee
public int importance;
// the id of direct subordinates
public List subordinates;
};
*/
class Solution {
public Employee getEmployeeById(List employees, int id1) {
for (int i = 0; i < employees.size(); i++) {
if (employees.get(i).id == id1)
return employees.get(i);
}
return null;
}
public int getImportance(List employees, int id) {
int result = 0;
Employee current = getEmployeeById(employees, id);
result += current.importance;
if (current.subordinates == null || current.subordinates.size() == 0)
return result;
for (int i = 0; i < current.subordinates.size(); i++)
result += getImportance(employees, current.subordinates.get(i));
return result;
}
}
520. Detect Capital
class Solution {
public boolean detectCapitalUse(String word) {
if (Character.isLowerCase(word.charAt(0))) {
if (isAllLowerCase(word.substring(1)))
return true;
} else {
if (isAllLowerCase(word.substring(1)) || isAllUpperCase(word.substring(1)))
return true;
}
return false;
}
public boolean isAllLowerCase(String word) {
for (int i = 0; i < word.length(); i++) {
if (Character.isUpperCase(word.charAt(i)))
return false;
}
return true;
}
public boolean isAllUpperCase(String word) {
for (int i = 0; i < word.length(); i++) {
if (!Character.isUpperCase(word.charAt(i)))
return false;
}
return true;
}
}