Skip to content

Instantly share code, notes, and snippets.

View vrat28's full-sized avatar
💭
do { programming() } while !success

Varun vrat28

💭
do { programming() } while !success
View GitHub Profile
@vrat28
vrat28 / max_product_word_lengths.py
Created May 28, 2021 01:15
Max Product of word lengths (Python)
from collections import defaultdict
class Solution:
def maxProduct(self, words: List[str]) -> int:
hashmap = defaultdict(int)
bit_number = lambda ch : ord(ch) - ord('a')
for word in words:
bitmask = 0
for ch in word:
# add bit number bit_number in bitmask
@vrat28
vrat28 / max_product_word_lengths.java
Created May 28, 2021 01:14
Max Products Word Length (Java)
class Solution {
public int bitNumber(char ch){
return (int)ch - (int)'a';
}
public int maxProduct(String[] words) {
Map<Integer, Integer> hashmap = new HashMap();
int bitmask = 0, bitNum = 0;
for (String word : words) {
@vrat28
vrat28 / max_product_word_lengths.swift
Created May 28, 2021 01:09
Maximum Product word lengths (Swift)
class Solution {
func maxProduct(_ words: [String]) -> Int {
var maxLength = 0
var bitMap = [Int:Int]()
for word in words{
let bitMask = getBitMask(word)
// Multiple words can have same bitmasks (a , aaaaaa).
// We need to keep the max length only
if let currentLength = bitMap[bitMask] {
bitMap[bitMask] = max(currentLength, word.count)
class Solution {
func minPartitions(_ n: String) -> Int {
var maxCharacter = 0
for c in n {
maxCharacter = max(maxCharacter,Int(String(c),radix:10)!)
}
return maxCharacter
}
}
class Solution:
def minPartitions(self, n: str) -> int:
return int(max(n))
@vrat28
vrat28 / partitioning_deci_binary_numbers.java
Created May 26, 2021 17:37
Partitioning Deci-binary numbers
class Solution {
public int minPartitions(String n) {
int maximum = 0;
for (char c : n.toCharArray()) {
maximum = Math.max(maximum, c - '0');
}
return maximum;
}
}
@vrat28
vrat28 / reverse_polish_notation.swift
Created May 25, 2021 09:57
Evaluate Reverse Polish Notation
class Solution {
func evalRPN(_ tokens: [String]) -> Int {
var stack = [Int]()
var operators = "+-/*"
for token in tokens {
if operators.contains(token) {
var result = 0
let second = stack.popLast()!
let first = stack.popLast()!
switch token {
@vrat28
vrat28 / reverse_polish_notation.py
Created May 25, 2021 09:51
Reverse Polish Notation (Python)
def evalRPN(self, tokens):
stack = []
for token in tokens:
if token not in "+-/*":
stack.append(int(token))
continue
@vrat28
vrat28 / reverse_polish_notation.java
Created May 25, 2021 09:50
Reverse Polish Notation (Java -Lambdas)
class Solution {
private static final Map<String, BiFunction<Integer, Integer, Integer>> OPERATIONS = new HashMap<>();
// Ensure this only gets done once for ALL test cases.
static {
OPERATIONS.put("+", (a, b) -> a + b);
OPERATIONS.put("-", (a, b) -> a - b);
OPERATIONS.put("*", (a, b) -> a * b);
OPERATIONS.put("/", (a, b) -> a / b);
@vrat28
vrat28 / to_lower_case.swift
Created May 24, 2021 09:02
To Lower Case
class Solution {
func toLowerCase(_ s: String) -> String {
var result = ""
for c in s {
var letter = c
if c >= "A", c <= "Z" {
letter = toLower(c)
}
result += "\(letter)"
}