├── DAY8 ├── DAY9 ├── Day 1 ├── Day ├── DAY4 ├── DAY6 ├── DAY3 ├── README.md ├── Day 2 ├── DAY5 └── DAY7 /DAY8: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /DAY9: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /Day 1: -------------------------------------------------------------------------------- 1 | //finding the index of the first unique character in a string 2 | 3 | class Solution { 4 | public int firstUniqChar(String s) { 5 | HashMap mp = new HashMap<>(); 6 | 7 | for (char a : s.toCharArray()) { 8 | mp.put(a, mp.getOrDefault(a, 0) + 1); 9 | } 10 | 11 | for (int i = 0; i < s.length(); i++) { 12 | if (mp.get(s.charAt(i)) == 1) { 13 | return i; 14 | } 15 | } 16 | 17 | return -1; 18 | } 19 | } 20 | 21 | -------------------------------------------------------------------------------- /Day: -------------------------------------------------------------------------------- 1 | //Given an array nums containing n distinct numbers in the range [0, n], return the only number in the range that is missing from the array. 2 | class Solution { 3 | public int missingNumber(int[] nums) { 4 | int n = nums.length; 5 | int[] v = new int[n+1]; 6 | Arrays.fill(v, -1); 7 | for(int i = 0; i < nums.length; i++) { 8 | v[nums[i]] = nums[i]; 9 | } 10 | for(int i = 0; i < v.length; i++) { 11 | if(v[i] == -1) return i; 12 | } 13 | return 0; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /DAY4: -------------------------------------------------------------------------------- 1 | //Given an integer n, return the least number of perfect square numbers that sum to n. 2 | 3 | //A perfect square is an integer that is the square of an integer; in other words, it is the product of some integer with itself. For example, 1, 4, 9, and 16 are perfect squares while 3 and 11 are not. 4 | 5 | class Solution { 6 | public int numSquares(int n) { 7 | int[] dp = new int[n + 1]; 8 | Arrays.fill(dp, Integer.MAX_VALUE); 9 | dp[0] = 0; 10 | for (int i = 1; i <= n; ++i) { 11 | int min_val = Integer.MAX_VALUE; 12 | for (int j = 1; j * j <= i; ++j) { 13 | min_val = Math.min(min_val, dp[i - j * j] + 1); 14 | } 15 | dp[i] = min_val; 16 | } 17 | return dp[n]; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /DAY6: -------------------------------------------------------------------------------- 1 | //Given a string s, return the number of palindromic substrings in it. 2 | public class Solution { 3 | int count1 = 0; 4 | boolean isPalindrome(String s, int i, int j) { 5 | while (i <= j) { 6 | if (s.charAt(i) != s.charAt(j)) { 7 | count1++; 8 | return false; 9 | } 10 | i++; 11 | j--; 12 | } 13 | return true; 14 | } 15 | int countSubstrings(String s) { 16 | int count = 0; 17 | for (int i = 0; i < s.length(); i++) { 18 | for (int j = i; j < s.length(); j++) { 19 | if (isPalindrome(s, i, j)) { 20 | count++; 21 | if (count1 == 2) { 22 | count1 = 0; 23 | break; 24 | } 25 | } 26 | } 27 | } 28 | return count; 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /DAY3: -------------------------------------------------------------------------------- 1 | //Given a string s, sort it in decreasing order based on the frequency of the characters. The frequency of a character is the number of times it appears in the string. 2 | 3 | //Return the sorted string. If there are multiple answers, return any of them. 4 | 5 | 6 | 7 | class Solution { 8 | public String frequencySort(String s) { 9 | Map hm = new HashMap<>(); 10 | 11 | for (char c : s.toCharArray()) { 12 | hm.put(c, hm.getOrDefault(c, 0) + 1); 13 | } 14 | 15 | PriorityQueue> pq = new PriorityQueue<>( 16 | (a, b) -> b.getValue() - a.getValue() 17 | ); 18 | 19 | pq.addAll(hm.entrySet()); 20 | 21 | StringBuilder result = new StringBuilder(); 22 | while (!pq.isEmpty()) { 23 | Map.Entry entry = pq.poll(); 24 | result.append(String.valueOf(entry.getKey()).repeat(entry.getValue())); 25 | } 26 | 27 | return result.toString(); 28 | } 29 | } 30 | 31 | 32 | 33 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # 100DaysOfCode Challenge 2 | Welcome to my 100DaysOfCode challenge!
3 | Start Date: 5 February 2024
4 | End Date: 15 May 2024 5 | ## What is the 100DaysOfCode Challenge? 6 | The #100DaysOfCode challenge is a commitment to code for at least one hour every day for the next 100 days. Whether you're a beginner or a seasoned developer, this challenge is a fantastic way to enhance your coding skills, build a coding habit, and connect with a community of like-minded individuals. 7 | ## Why am I taking on this challenge? 8 | I've decided to embark on this journey to: 9 | - Improve Skills: Sharpen my programming skills and deepen my understanding of various technologies. 10 | - Build Consistency: Cultivate a daily coding habit to stay consistent and make progress. 11 | - Community Engagement: Connect with fellow developers, share experiences, and support each other on this coding adventure. 12 | ## Rules of the Challenge 13 | 1. Code for at least one hour every day. 14 | 1. Document progress in this repository to track my coding journey. 15 | 1. Stay accountable and engage with the coding community. 16 | -------------------------------------------------------------------------------- /Day 2: -------------------------------------------------------------------------------- 1 | // Question 2 | // 49. Group Anagrams 3 | // Given an array of strings strs, group the anagrams together. You can return the answer in any order. 4 | // An Anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once. 5 | 6 | // Example 1: 7 | // Input: strs = ["eat","tea","tan","ate","nat","bat"] 8 | // Output: [["bat"],["nat","tan"],["ate","eat","tea"]] 9 | 10 | // Example 2: 11 | // Input: strs = [""] 12 | // Output: [[""]] 13 | 14 | // Example 3: 15 | // Input: strs = ["a"] 16 | // Output: [["a"]] 17 | 18 | // Constraints: 19 | 20 | // 1 <= strs.length <= 104 21 | // 0 <= strs[i].length <= 100 22 | // strs[i] consists of lowercase English letters. 23 | // Solution 24 | import java.util.*; 25 | 26 | public class Solution { 27 | public List> groupAnagrams(String[] strs) { 28 | Map> map = new HashMap<>(); 29 | for (String str : strs) { 30 | char[] charArray = str.toCharArray(); 31 | Arrays.sort(charArray); 32 | String sortedStr = new String(charArray); 33 | if (!map.containsKey(sortedStr)) { 34 | map.put(sortedStr, new ArrayList<>()); 35 | } 36 | map.get(sortedStr).add(str); 37 | } 38 | return new ArrayList<>(map.values()); 39 | } 40 | } 41 | 42 | -------------------------------------------------------------------------------- /DAY5: -------------------------------------------------------------------------------- 1 | /*Given a set of distinct positive integers nums, return the largest subset answer such that every pair (answer[i], answer[j]) of elements in this subset satisfies: 2 | 3 | answer[i] % answer[j] == 0, or 4 | answer[j] % answer[i] == 0 5 | If there are multiple solutions, return any of them.*/ 6 | class Solution { 7 | public List largestDivisibleSubset(int[] nums) { 8 | int n = nums.length; 9 | int[] dp = new int[n]; 10 | Arrays.fill(dp, 1); 11 | Arrays.sort(nums); 12 | 13 | int maxSize = 1, maxIndex = 0; 14 | for (int i = 1; i < n; i++) { 15 | for (int j = 0; j < i; j++) { 16 | if (nums[i] % nums[j] == 0) { 17 | dp[i] = Math.max(dp[i], dp[j] + 1); 18 | if (dp[i] > maxSize) { 19 | maxSize = dp[i]; 20 | maxIndex = i; 21 | } 22 | } 23 | } 24 | } 25 | 26 | List result = new ArrayList<>(); 27 | int num = nums[maxIndex]; 28 | for (int i = maxIndex; i >= 0; i--) { 29 | if (num % nums[i] == 0 && dp[i] == maxSize) { 30 | result.add(nums[i]); 31 | num = nums[i]; 32 | maxSize--; 33 | } 34 | } 35 | 36 | return result; 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /DAY7: -------------------------------------------------------------------------------- 1 | /*You are given a rows x cols matrix grid representing a field of cherries where grid[i][j] represents the number of cherries that you can collect from the (i, j) cell. 2 | 3 | You have two robots that can collect cherries for you: 4 | 5 | Robot #1 is located at the top-left corner (0, 0), and 6 | Robot #2 is located at the top-right corner (0, cols - 1). 7 | Return the maximum number of cherries collection using both robots by following the rules below: 8 | 9 | From a cell (i, j), robots can move to cell (i + 1, j - 1), (i + 1, j), or (i + 1, j + 1). 10 | When any robot passes through a cell, It picks up all cherries, and the cell becomes an empty cell. 11 | When both robots stay in the same cell, only one takes the cherries. 12 | Both robots cannot move outside of the grid at any moment. 13 | Both robots should reach the bottom row in grid.*/ 14 | class Solution { 15 | public int cherryPickup(int[][] grid) { 16 | int n = grid.length; 17 | int m = grid[0].length; 18 | 19 | // Create 3D DP table with initial values of 0 20 | int[][][] dp = new int[n][m][m]; 21 | 22 | // Set the starting point value (top-left and top-right corner) 23 | int cherries = 0; 24 | dp[0][0][m - 1] = grid[0][0] + grid[0][m - 1]; // Add cherries from both robots 25 | 26 | // Iterate through each row from second onwards 27 | for (int i = 1; i < n; ++i) { 28 | // Iterate through each column for robot 1 29 | for (int j = 0; j < m; ++j) { 30 | // Iterate through each column for robot 2 31 | for (int k = 0; k < m; ++k) { 32 | // Skip invalid states: 33 | // - Both robots in the same row (j > i) 34 | // - Robot 2 left of robot 1 (k < m - i - 1) 35 | // - Robot 1 further right than robot 2 (j > k) 36 | if (j > i || k < m - i - 1 || j > k) continue; 37 | // Base case: no moves possible, use previous state 38 | dp[i][j][k] = dp[i - 1][j][k]; 39 | // Explore moves for robot 1: 40 | // - Up-diagonal with robot 2 at same position 41 | if (j - 1 >= 0) { 42 | dp[i][j][k] = Math.max(dp[i][j][k], dp[i - 1][j - 1][k]); 43 | } 44 | // - Up-diagonal with robot 2 one step left/right 45 | if (j - 1 >= 0 && k - 1 >= 0) 46 | dp[i][j][k] = Math.max(dp[i][j][k], dp[i - 1][j - 1][k - 1]); 47 | if (j - 1 >= 0 && k + 1 < m) 48 | dp[i][j][k] = Math.max(dp[i][j][k], dp[i - 1][j - 1][k + 1]); 49 | // Explore moves for robot 2: 50 | // - Up-diagonal with robot 1 at same position 51 | if (j + 1 < m) { 52 | dp[i][j][k] = Math.max(dp[i][j][k], dp[i - 1][j + 1][k]); 53 | } 54 | // - Up-diagonal with robot 1 one step left/right 55 | if (j + 1 < m && k - 1 >= 0) 56 | dp[i][j][k] = Math.max(dp[i][j][k], dp[i - 1][j + 1][k - 1]); 57 | if (j + 1 < m && k + 1 < m) 58 | dp[i][j][k] = Math.max(dp[i][j][k], dp[i - 1][j + 1][k + 1]); 59 | // Explore horizontal moves for both robots: 60 | // - Both robots move left 61 | if (k - 1 >= 0) 62 | dp[i][j][k] = Math.max(dp[i][j][k], dp[i - 1][j][k - 1]); 63 | // - Both robots move right 64 | if (k + 1 < m) 65 | dp[i][j][k] = Math.max(dp[i][j][k], dp[i - 1][j][k + 1]); 66 | // Add cherries only if robots are in different positions 67 | if (j != k) { 68 | dp[i][j][k] += grid[i][j] + grid[i][k]; 69 | } else { 70 | dp[i][j][k] += grid[i][j]; // Only one robot picks if they land in the same cell 71 | } 72 | // Update maximum cherries collected so far 73 | cherries = Math.max(cherries, dp[i][j][k]); 74 | } 75 | } 76 | } 77 | 78 | return cherries; 79 | } 80 | } 81 | --------------------------------------------------------------------------------