Data Structures and Algorithms in Java
This repository is a collection of algorithms, data structures and coding challenges which I've solved over a period of time. The algorithms/solutions are implemented considering efficient Time and Space Complexity approaches. All the solutions are 100% correct and tested against various test cases, unless marked as TODO/InProgress.
This section contains implementation of different Data Structures in Java.
| Data Structure | Implementation |
|---|---|
| Stack Methods Implemented: push(), pop(), peek(), isEmpty() |
View |
| Queue Methods Implemented: offer(), poll(), peek(), isEmpty() |
View |
| Linked List Methods Implemented: appendToTail(), add(), appendToHead(), removeFirst(), removeLast(), clone(), addAll(), reverseBetween(), getMid(), isPalindrome(), reverse(), toString(), mergeWith(), mergeTwoLists(), reverseInKGroups(), hasCycle(), removeKthFromEnd() |
View |
| Tree Methods Implemented: preorderIterative(), inorderIterative(), levelOrder() ... many more |
View |
| Graph Methods Implemented: BFS(), DFS(), Djikstras(), TopologicalSort() |
View |
| Trie Methods Implemented: search(), insert(), startsWith() |
View |
| MaxHeap Methods Implemented: insert(), fetchTop(), doubleSize() |
View |
| MinHeap Methods Implemented: insert(), fetchTop(), doubleSize() |
View |
| HashMap Methods Implemented: put(), add(), containsKey(), remove() |
View |
| SegmentTree Methods Implemented: query(), update() |
View |
| CartesianTree Methods Implemented: buildCartesianTree() |
View |
| BinaryIndexedTree Methods Implemented: build(), query(), queryRange() |
View |
Algorithms Problems Java
| # | Problem | Description |
|---|---|---|
| 0 | Island Perimeter |
View |
| 1 | Reshape Matrix |
View |
| 2 | Next Greater Element 1 |
View |
| 3 | Average of Levels in a Binary Tree |
View |
| 4 | Judge Route Circle |
View |
| 5 | Nim Game |
View |
| 6 | Invert a Binary Tree |
View |
| 7 | Two Sum 4 BST |
View |
| 8 | Find the difference |
View |
| 9 | Merge Two Binary Trees |
View |
| 10 | Sum of 2 Integers |
View |
| 11 | BinaryTree to String |
View |
| 12 | Construct Rectangle |
View |
| 13 | Distribute Candies |
View |
| 14 | Convert BST to Greater Tree |
View |
| 15 | Intersection of 2 Arrays |
View |
| 16 | Minimum Absolute difference in BST |
View |
| 17 | Ransome Note |
View |
| 18 | Excelsheet Column Number |
View |
| 19 | Assign Cookies |
View |
| 20 | Binary Tree Tilt |
View |
| 21 | Sum of left leaves |
View |
| 22 | Minimum index sum of 2 lists |
View |
| 23 | Majority Element |
View |
| 24 | Relative Ranking |
View |
| 25 | Delete Node |
View |
| 26 | Contains Duplicate |
View |
| 27 | Minimum moves to equal array |
View |
| 28 | Longest Palindrome |
View |
| 29 | Max product 3 nos |
View |
| 30 | Student attendance record |
View |
| 31 | Diameter of Binary Tree |
View |
| 32 | Sorted array to BST |
View |
| 33 | Longest Harmonic Subsequence Length |
View |
| 34 | UglyNumber |
View |
| 35 | Symmetric SubTree |
View |
| 36 | PathSum III |
View |
| 37 | Power Of 2 |
View |
| 38 | House Robber I |
View |
| 39 | Level Order Bottomup |
View |
| 40 | Set Mismatch |
View |
| 41 | Intersection Arrays |
View |
| 42 | LongestContinuosIncreasingSubsequence |
View |
| 43 | Power 4 |
View |
| 44 | ReverseOfVowels |
View |
| 45 | image Smoother |
View |
| 46 | Perfect Square |
View |
| 47 | Balanced Binary Tree |
View |
| 48 | Arange Coins |
View |
| 49 | Isomorphic String |
View |
| 50 | Factorial Zeroes |
View |
| 51 | Path Sum |
View |
| 52 | Min Depth |
View |
| 53 | WordPattern |
View |
| 54 | Non Decreasing Array |
View |
| 55 | PsychometricTesting |
View |
| 56 | Sum Square Number |
View |
| 57 | Length Of Last Word |
View |
| 58 | Valid Palindrome II |
View |
| 59 | ExcelSheetColumnTitle |
View |
| 60 | PerfectNumber |
View |
| 61 | FirstBadVersion |
View |
| 62 | Can Place Flowers |
View |
| 63 | Square Root |
View |
| 64 | MaximumAverageSubarrayI |
View |
| 65 | SwapNodesInPairs |
View |
| 66 | Subsets I |
View |
| 67 | Subsets II |
View |
| 68 | Permutations |
View |
| 69 | Permutations II |
View |
| 70 | CombinationSum |
View |
| 71 | ValidSquare |
View |
| 72 | KEmptySlots |
View |
| 73 | LongestAbsolutePath |
View |
| 74 | LongestSubStringWithKDistinctChars |
View |
| 75 | LicenseKeyFormatting |
View |
| 76 | RangeSumQuery2DMutable |
View |
| 77 | MovingAverage |
View |
| 78 | BinaryTreeLongestConsecutiveSequence |
View |
| 79 | SentenceScreenFitting |
View |
| 80 | BombEnemy |
View |
| 81 | ZigZagIterator |
View |
| 82 | DecodeString |
View |
| 83 | PalindromePairs |
View |
| 84 | PowerSetSubStringsWithoutDups |
View |
| 85 | PalindromicSubStrings |
View |
| 86 | RepeatedStringMatch |
View |
| 87 | ShortestDistanceFromAllBuildings |
View |
| 88 | SkyLineProblem |
View |
| 89 | MergeSortedArray |
View |
| 90 | IntegerToEnglishWords |
View |
| 91 | BattleShipsInABoard |
View |
| 92 | LinkedListCycle |
View |
| 93 | PopulatingNextRightPointersI |
View |
| 94 | SearchInRotatedSortedArray |
View |
| 95 | SpiralMatrixNoTouch |
View |
| 96 | InorderSuccessorBST |
View |
| 97 | SortColors |
View |
| 98 | FindMinimumInRotatedSortedArray |
View |
| 99 | AddTwoNumbersII |
View |
| 100 | LowestCommonAncestorBST |
View |
| 101 | ReverseWordsInStringII |
View |
| 102 | MinimumPathSum |
View |
| 103 | DungeonGame |
View |
| 104 | SerializeAndDeserializeBinaryTree |
View |
| 105 | MissingRanges |
View |
| 106 | LongestConsecutiveSequence |
View |
| 107 | ValidTriangleNumber |
View |
| 108 | ContainerWithMostWater |
View |
| 109 | BulbSwitcherI |
View |
| 110 | SplitArrayWithEqualSum |
View |
| 111 | FindTheCeleberity |
View |
| 112 | CompareVersion |
View |
| 113 | SummaryRanges |
View |
| 114 | ThreeSumSmaller |
View |
| 115 | UTF_8Validation |
View |
| 116 | ValidAbbrevation |
View |
| 117 | StrobographicNumberII |
View |
| 118 | BSTIterator |
View |
| 119 | MergeIntervals |
View |
| 120 | FlattenNestedListIterator |
View |
| 121 | EncodeAndDecodeStrings |
View |
| 122 | QueueReconstructionByHeight.java |
View |
| 123 | LongestSubstringWithAtMost2DistinctChars |
View |
| 124 | ConstructBinaryTreeFromInorderAndPostOrderTraversal |
View |
| 125 | ConstructBinaryTreeFromInorderAndPreOrderTraversal |
View |
| 126 | RangeAddition |
View |
| 127 | ClimbStairs |
View |
| 128 | SimplifyPath |
View |
| 129 | StrobogrammaticNumber |
View |
| 130 | WiggleSort |
View |
| 131 | BinaryTreeVerticalOrderTraversal |
View |
| 132 | TrapRainWater |
View |
| 133 | NthDigit |
View |
| 134 | MaximumProductSubarray |
View |
| 135 | IsSubSequence |
View |
| 136 | RangeSumQueryImmutable |
View |
| 137 | PaintHouse |
View |
| 138 | EditDistance |
View |
| 139 | LargestRectangleInHistogram |
View |
| 140 | ShortestUnsortedContinuousSubarray |
View |
| 141 | MaximalRectangle |
View |
| 142 | PerfectSquares |
View |
| 143 | DoubeCheckedSynchronizationSingleton |
View |
| 144 | BurstBalloons |
View |
| 145 | CoinChange |
View |
| 146 | DecodeWays |
View |
| 147 | BestTimeToBuyAndSellStock |
View |
| 148 | BestTimeToBuyAndSellStockWithCooldown |
View |
| 149 | UglyNumberII |
View |
| 150 | IntegerBreak |
View |
| 151 | WildCardMatching |
View |
| 152 | DistinctSubsequence |
View |
| 153 | InterleavingString |
View |
| 154 | WordSearch |
View |
| 155 | SudokuSolver |
View |
| 156 | NQueens |
View |
| 157 | NQueensII |
View |
| 158 | Combinations |
View |
| 159 | RestoreIpAddresses |
View |
| 160 | LongestIncreasingPathInMatrix |
View |
| 161 | JumpGame |
View |
| 162 | SearchForRange |
View |
| 163 | PeekingIterator |
View |
| 164 | LongestWordInDictionaryViaDeleting |
View |
| 165 | GCD |
View |
| 166 | ExtendedEuclideanAlgorithm |
View |
| 167 | WaterJugProblem |
View |
| 168 | NextPermutation |
View |
| 169 | MaximumVacationDays |
View |
| 170 | LongestIncreasingSubSequence |
View |
| 171 | PaintFence |
View |
| 172 | LongestCommonSubsequence |
View |
| 173 | MinimumInsertionForPalindrome |
View |
| 174 | PowerOfThree |
View |
| 175 | SubstringWithConcatenationOfWords |
View |
| 176 | CombinationSumII |
View |
| 177 | StringCompression |
View |
| 178 | SegmentTree_RangeSum |
View |
| 179 | SegmentTree_RangeMin |
View |
| 180 | CountSmallerNumberAfterSelf |
View |
| 181 | RangeSumQuery |
View |
| 182 | MaximumBinaryTree |
View |
| 183 | LargestBSTSubtree |
View |
| 184 | HouseRobberII |
View |
| 185 | TwoKeysKeyboard |
View |
| 186 | MovieRating |
View |
| 187 | ContigousSubarraysWithZeroSum |
View |
| 188 | KStringSizeWith1CharRepeating2ce |
View |
| 189 | FindSegementSize |
View |
| 190 | UniqueBinarySearchTrees |
View |
| 191 | UniqueBinarySearchTreesII |
View |
| 192 | RussianDollEnvelopes |
View |
| 193 | MedianTwoSortedArrays |
View |
| 194 | LonelyPixelI |
View |
| 195 | LonelyPixelII |
View |
| 196 | KDiffPairsArray |
View |
| 197 | TwoSumII |
View |
| 198 | MultiplyStrings |
View |
| 199 | PlusOne |
View |
| 200 | ReverseBits |
View |
| 201 | DetectCapital |
View |
| 202 | FindLargestValueEachTreeRow |
View |
| 203 | HappyNumber |
View |
| 204 | AddBinary |
View |
| 205 | MoveZeroes |
View |
| 206 | PalindromeNumbers |
View |
| 207 | NumberToBase |
View |
| 208 | StrStr |
View |
| 209 | LeastFrequentlyUsedCache |
View |
| 210 | LeastRecentlyUsedCache |
View |
| 211 | FirstUniqueCharacter |
View |
| 212 | ZigZagConversion |
View |
| 213 | FirstMissingPositive |
View |
| 214 | FindDuplicateNumber |
View |
| 215 | ProductArrayExceptSelf |
View |
| 216 | LongestPalindromicSubstring |
View |
| 217 | ReverseInteger |
View |
| 218 | StringToInteger |
View |
| 219 | ValidParanthesis |
View |
| 220 | SurroundedRegions |
View |
| 221 | CountingBits |
View |
| 222 | FindDisappearedNumber |
View |
| 223 | ThreeSum |
View |
| 224 | LetterCombinationsOfPhoneNumber |
View |
| 225 | GroupAnagrams |
View |
| 226 | ValidAnagram |
View |
| 227 | PascalsTriangleII |
View |
| 228 | ThirdMaximumNumber |
View |
| 229 | RegularExpressionMatching |
View |
| 230 | SortByFrequency |
View |
| 231 | LongestSubstringWithAtleastKRepeatingChars |
View |
| 232 | GrayCode |
View |
| 233 | RotateFunction |
View |
| 234 | RepeatedSubstringPattern |
View |
| 235 | NumberOfIslands |
View |
| 236 | LongestPalindromicSubsequence |
View |
| 237 | WordBreak |
View |
| 238 | WordLadder |
View |
| 239 | WordLadderII |
View |
| 240 | WordBreakII |
View |
| 241 | JewelsAndStones |
View |
| 242 | UniqueEmailAddresses |
View |
| 243 | FindAnagramMappings |
View |
| 244 | FlippingAnImage |
View |
| 245 | PeakIndexInAMountainArray |
View |
| 246 | RecentCounter |
View |
| 247 | DeleteColumnsToMakeSorted |
View |
| 248 | LoggerRateLimiter |
View |
| 249 | LeafSimilarTrees |
View |
| 250 | BinaryTreeLevelOrderConstantSpace |
View |
| 251 | MinimumAreaRectangle |
View |
| 252 | MinimumAreaRectangleII |
View |
| 253 | PatchingArray |
View |
| 254 | QuickFind |
View |
| 255 | QuickUnion |
View |
| 256 | NRepeatedElementInSize2NArray |
View |
| 257 | RangeMinimumQuery |
View |
| 258 | ReversePairs |
View |
| 259 | BinarySearch |
View |
| 260 | FindSmallestLetterGreaterThanTarget |
View |
| 261 | ClosestBinarySearchTreeValue |
View |
| 262 | Heaters |
View |
| 263 | SearchInASortedArrayOfUnknownSize |
View |
| 264 | SearchInsertPosition |
View |
| 265 | TwoSum |
View |
| 266 | MaxIncreaseToKeepCitySkyline |
View |
| 267 | ContinuousSubarraySum |
View |
| 268 | LemonadeChange |
View |
| 269 | StringWithoutAAAOrBBB |
View |
| 270 | MinimumAddToMakeParenthesesValid |
View |
| 271 | ScoreAfterFlippingMatrix |
View |
| 272 | PartitionLabels |
View |
| 273 | PalindromePartitioning |
View |
| 274 | CombinationSumIII |
View |
| 275 | MinimumNumberOfRefuelingStops |
View |
| 276 | PalindromePartitioningII |
View |
| 277 | PalindromePermutation |
View |
| 278 | PalindromePermutationII |
View |
| 279 | KClosestPointsToOrigin |
View |
| 280 | BoyerMoore |
View |
| 281 | ConvertBinarySearchTreeToSortedDoublyLinkedList |
View |
| 282 | BeautifulArray |
View |
| 283 | LinkedListRandomNode |
View |
| 284 | RandomPickIndex |
View |
| 285 | SurfaceAreaOf3DShapes |
View |
| 286 | FlipGameII |
View |
| 287 | MaximumDepthOfNaryTree |
View |
| 288 | NaryTreeLevelOrderTraversal |
View |
| 289 | EmployeeImportance |
View |
| 290 | CousinsInBinaryTree |
View |
| 291 | IncreasingOrderSearchTree |
View |
| 292 | DistributeCoinsInBinaryTree |
View |
| 293 | KeysAndRooms |
View |
| 294 | NumberOfConnectedComponentsInAnUndirectedGraph |
View |
| 295 | CloneGraph |
View |
| 296 | WeightedQuickUnionPathCompression |
View |
| 297 | RedundantConnection |
View |
| 298 | MinimumDistanceBetweenBSTNodes |
View |
| 299 | RangeSumOfBST |
View |
| 300 | SplitBST |
View |
| 301 | MyCalendarI |
View |
| 302 | ImplementRand10UsingRand7 |
View |
| 303 | HandOfStraights |
View |
| 304 | CourseSchedule |
View |
| 305 | CourseScheduleII |
View |
| 306 | CoinChangeII |
View |
| 307 | MinimumSizeSubarraySum |
View |
| 308 | RemoveKDigits |
View |
| 309 | Find132pattern |
View |
| 310 | EvaluateReversePolishNotation |
View |
| 311 | AsteroidCollision |
View |
| 312 | VerifyPreorderSequenceInBinarySearchTree |
View |
| 313 | AllNodesDistanceKInBinaryTree |
View |
| 314 | PopulatingNextRightPointersInEachNodeII |
View |
| 315 | FindKthSmallestPairDistance |
View |
| 316 | NumberOfMatchingSubsequences |
View |
| 317 | ShortestWayToFormString |
View |
| 318 | TopologicalSort |
View |
| 319 | VerifyingAnAlienDictionary |
View |
| 320 | AlienDictionary |
View |
| 321 | ConstructBinaryTreeFromPreorderAndPostorderTraversal |
View |
| 322 | WiggleSortII |
View |
| 323 | VerticalOrderTraversalOfABinaryTree |
View |
| 324 | JumpGameII |
View |
| 325 | ShortestPalindrome |
View |
| 326 | GraphMColoringProblem |
View |
| 327 | PossibleBipartition |
View |
| 328 | UniquePaths |
View |
| 329 | MinimumSpanningTreeKruskals |
View |
| 330 | TheEarliestMomentWhenEveryoneBecomeFriends |
View |
| 331 | MaximumSubarray |
View |
| 332 | PopulatingNextRightPointersInEachNode |
View |
| 333 | NetworkDelayTime |
View |
| 334 | CheapestFlightsWithinKStops |
View |
| 335 | InsertIntoACyclicSortedList |
View |
| 336 | DeleteNodesAndReturnForest |
View |
| 337 | GenerateParentheses |
View |
| 338 | CatalanNumber |
View |
| 339 | ReconstructItinerary(EulerTour) |
View |
| 340 | GameOfLife |
View |
| 341 | KnightProbabilityInChessboard |
View |
| 342 | MinimumHeightTrees |
View |
| 343 | BinaryTreeColoringGame |
View |
| 344 | SnapShotArray |
View |
| 345 | CampusBikes |
View |
| 346 | CampusBikesII |
View |
| 347 | DailyTemperatures |
View |
| 348 | GuessNumberHigherOrLowerII |
View |
| 349 | SentenceSimilarityII |
View |
| 350 | CarFleet |
View |
| 351 | SortList |
View |
| 352 | ShortestPathInBinaryMatrix |
View |
| 353 | ClosestLeafInABinaryTree |
View |
| 354 | CountCompleteTreeNodes |
View |
| 355 | FourSum,KSum |
View |
| 356 | DesignSnakeGame |
View |
| 357 | RepeatedDNASequences |
View |
| 358 | MostStonesRemovedWithSameRowOrColumn |
View |
| 359 | SubarraySumEqualsK |
View |
| 360 | SingleElementInASortedArray |
View |
| 361 | PartitionEqualSubsetSum |
View |
| 362 | PartitionToKEqualSumSubsets |
View |
| 363 | BraceExpansion |
View |
| 364 | StreamOfCharacters |
View |
| 365 | ToeplitzMatrix |
View |
| 366 | BullsAndCows |
View |
| 367 | EvaluateDivision |
View |
| 368 | BinaryStringWithSubstringsRepresenting1ToN |
View |
| 369 | FillingBookcaseShelves |
View |
| 370 | SortAPartiallySortedArray |
View |
| 371 | LargestValuesFromLabels |
View |
| 372 | HighFive |
View |
| 373 | MergeSort |
View |
| 374 | DifferentWaysToAddParentheses |
View |
| 375 | QuickSort |
View |
| 376 | UniquePathsII |
View |
| 377 | MeetingRooms |
View |
| 378 | MeetingRoomsII |
View |
| 379 | TopKFrequentElements |
View |
| 380 | GasStation |
View |
| 381 | MyCalendarII |
View |
| 382 | AddTwoNumbers |
View |
| 383 | MergeTwoSortedLists |
View |
| 384 | MergeKSortedLists |
View |
| 385 | CopyListWithRandomPointer |
View |
| 386 | SpiralMatrix |
View |
| 387 | ReverseNodesInKGroup |
View |
| 388 | TaskScheduler |
View |
| 389 | DivideTwoIntegers |
View |
| 390 | ConstructBinarySearchTreeFromPreorderTraversal |
View |
| 391 | LowestCommonAncestorOfDeepestLeaves |
View |
| 392 | IntervalListIntersections |
View |
| 393 | MaximumDifferenceBetweenNodeAndAncestor |
View |
Misc
DP 0: reduce the problem to finite state machine 1: reduce the fsm to graph 2: reduce graph to logical graph 3: traverse this graph with keeping constraints