Day 9 of LeetCode Challenge

Cloud and DevOps Engineer with hands-on expertise in AWS, CI/CD pipelines, Docker, Kubernetes, and Monitoring tools. Adept at building and automating scalable, fault-tolerant cloud infrastructures, and consistently improving system performance, security, and reliability in dynamic environments.
Problem 1: Word Pattern
Link to the problem: https://leetcode.com/problems/word-pattern/description/

import java.util.HashMap;
class Solution {
public boolean wordPattern(String pattern, String s) {
String[] words = s.split(" ");
if (pattern.length() != words.length) {
return false;
}
HashMap<Character, String> charToWord = new HashMap<>();
HashMap<String, Character> wordToChar = new HashMap<>();
for (int i = 0; i < pattern.length(); i++) {
char c = pattern.charAt(i);
String word = words[i];
if (charToWord.containsKey(c)) {
if (!charToWord.get(c).equals(word)) return false;
}else if (wordToChar.containsKey(word)) {
if (wordToChar.get(word) != c)
return false;
}else {
charToWord.put(c, word);
wordToChar.put(word, c);
}
}
return true;
}
}
Problem 2: Longest Harmonious Subsequence
Link to the problem: https://leetcode.com/problems/longest-harmonious-subsequence/description/

import java.util.HashMap;
class Solution {
public int findLHS(int[] nums) {
HashMap<Integer, Integer> map = new HashMap<>();
int ans = 0;
for(int i:nums){
if(!map.containsKey(i))
map.put(i, 1);
else
map.put(i, map.get(i)+1);
}
Integer[] arr = map.keySet().toArray(new Integer[map.size()]);
for(int i=0; i<arr.length; i++){
int num1 = 0;
int num2 = 0;
if(map.get(arr[i]-1)!=null)
num1 = map.get(arr[i])+map.get(arr[i]-1);
if(map.get(arr[i]+1)!=null)
num2 = map.get(arr[i])+map.get(arr[i]+1);
ans = ans>num1?ans:num1;
ans = ans>num2?ans:num2;
}
return ans;
}
}
Problem 3: Fair Candy Swap
Link to the problem: https://leetcode.com/problems/fair-candy-swap/

class Solution {
public int[] fairCandySwap(int[] aliceSizes, int[] bobSizes) {
int len1 = aliceSizes.length;
int len2 = bobSizes.length;
int num1 = 0; int num2 = 0;
int[] ans = new int[2];
ans[0] = 0;
ans[1] = 0;
for(int i=0; i<(len1<len2?len1:len2); i++){
num1 += aliceSizes[i];
num2 += bobSizes[i];
}
if(len1<len2 && len1!=len2){
for(int i=len1; i<len2; i++)
num2 += bobSizes[i];
}else if(len1>len2 && len1!=len2){
for(int i=len2; i<len1; i++)
num1 += aliceSizes[i];
}
System.out.println(num1+" "+num2);
if(num1!=num2){
ans = func(ans, aliceSizes, bobSizes, num1, num2);
}
return ans;
}
private int[] func(int[] ans, int[] aliceSizes, int[] bobSizes, int num1, int num2){
for(int i=0; i<aliceSizes.length; i++){
for(int j=0; j<bobSizes.length; j++){
if(num1-aliceSizes[i]+bobSizes[j] == num2-bobSizes[j]+aliceSizes[i]){
ans[0] = aliceSizes[i];
ans[1] = bobSizes[j];
return ans;
}
}
}
return ans;
}
}
Problem 4: Most Common Word
Link to the problem: https://leetcode.com/problems/most-common-word/description/

import java.util.HashMap;
class Solution {
public String mostCommonWord(String paragraph, String[] banned) {
HashMap<String, Integer> map = new HashMap<>();
String word = "";
int max = 0;
String ans = "";
for(char ch : paragraph.toCharArray()){
if(ch==' ' || ch=='.' || ch==',' || ch=='!' || ch=='?' || ch==';' || ch=="'".charAt(0)){
boolean bool = false;
word = word.toLowerCase();
for(String str : banned){
if(str.equals(word))
bool = true;
}
if(!bool && !word.equals("")){
if(map.containsKey(word)){
map.put(word, map.get(word)+1);
if(max<map.get(word)){
max = map.get(word);
ans = word;
}
}
else{
map.put(word, 1);
if(max<map.get(word)){
max = map.get(word);
ans = word;
}
}
}
word = "";
}else
word = word.concat(Character.toString(ch));
}
if(!word.equals("")){
boolean bool = false;
for(String str : banned){
if(str.equals(word))
bool = true;
}
if(!bool && !word.equals("")){
word = word.toLowerCase();
if(map.containsKey(word)){
map.put(word, map.get(word)+1);
if(max<map.get(word)){
max = map.get(word);
ans = word;
}
}
else{
map.put(word, 1);
if(max<map.get(word)){
max = map.get(word);
ans = word;
}
}
}
}
System.out.println(map);
return ans;
}
}
Problem 5: Permutation difference between 2 strings
Link to the problem: https://leetcode.com/problems/permutation-difference-between-two-strings/

class Solution {
public int findPermutationDifference(String s, String t) {
int ans = 0;
for(int i = 0; i<s.length(); i++){
ans += i>t.indexOf(s.charAt(i))?i-t.indexOf(s.charAt(i)):t.indexOf(s.charAt(i))-i;
}
return ans;
}
}




