├── .vscode └── launch.json ├── 0004-median-of-two-sorted-arrays ├── 0004-median-of-two-sorted-arrays.java ├── NOTES.md └── README.md ├── 0005-longest-palindromic-substring ├── 0005-longest-palindromic-substring.java ├── NOTES.md └── README.md ├── 0013-roman-to-integer ├── 0013-roman-to-integer.java ├── NOTES.md └── README.md ├── 0014-longest-common-prefix ├── 0014-longest-common-prefix.java ├── NOTES.md └── README.md ├── 0020-valid-parentheses ├── 0020-valid-parentheses.java ├── NOTES.md └── README.md ├── 0021-merge-two-sorted-lists ├── 0021-merge-two-sorted-lists.java ├── NOTES.md └── README.md ├── 0023-merge-k-sorted-lists ├── 0023-merge-k-sorted-lists.java ├── NOTES.md └── README.md ├── 0028-find-the-index-of-the-first-occurrence-in-a-string ├── 0028-find-the-index-of-the-first-occurrence-in-a-string.java ├── NOTES.md └── README.md ├── 0033-search-in-rotated-sorted-array ├── 0033-search-in-rotated-sorted-array.java ├── NOTES.md └── README.md ├── 0037-sudoku-solver ├── 0037-sudoku-solver.java ├── NOTES.md └── README.md ├── 0039-combination-sum ├── 0039-combination-sum.java ├── NOTES.md └── README.md ├── 0040-combination-sum-ii ├── 0040-combination-sum-ii.java ├── NOTES.md └── README.md ├── 0046-permutations ├── 0046-permutations.java ├── NOTES.md └── README.md ├── 0051-n-queens ├── 0051-n-queens.java ├── NOTES.md └── README.md ├── 0060-permutation-sequence ├── 0060-permutation-sequence.java ├── NOTES.md └── README.md ├── 0064-minimum-path-sum ├── 0064-minimum-path-sum.java ├── NOTES.md └── README.md ├── 0071-simplify-path ├── 0071-simplify-path.java ├── NOTES.md └── README.md ├── 0087-scramble-string ├── 0087-scramble-string.java ├── NOTES.md └── README.md ├── 0090-subsets-ii ├── 0090-subsets-ii.java ├── NOTES.md └── README.md ├── 0094-binary-tree-inorder-traversal ├── 0094-binary-tree-inorder-traversal.java ├── NOTES.md └── README.md ├── 0101-symmetric-tree ├── 0101-symmetric-tree.java ├── NOTES.md └── README.md ├── 0106-construct-binary-tree-from-inorder-and-postorder-traversal ├── 0106-construct-binary-tree-from-inorder-and-postorder-traversal.java ├── NOTES.md └── README.md ├── 0109-convert-sorted-list-to-binary-search-tree ├── 0109-convert-sorted-list-to-binary-search-tree.java ├── NOTES.md └── README.md ├── 0129-sum-root-to-leaf-numbers ├── 0129-sum-root-to-leaf-numbers.java ├── NOTES.md └── README.md ├── 0131-palindrome-partitioning ├── 0131-palindrome-partitioning.java ├── NOTES.md └── README.md ├── 0133-clone-graph ├── 0133-clone-graph.java ├── NOTES.md └── README.md ├── 0142-linked-list-cycle-ii ├── 0142-linked-list-cycle-ii.java ├── NOTES.md └── README.md ├── 0144-binary-tree-preorder-traversal ├── 0144-binary-tree-preorder-traversal.java ├── NOTES.md └── README.md ├── 0145-binary-tree-postorder-traversal ├── NOTES.md └── README.md ├── 0151-reverse-words-in-a-string ├── 0151-reverse-words-in-a-string.java ├── NOTES.md └── README.md ├── 0152-maximum-product-subarray ├── 0152-maximum-product-subarray.java ├── NOTES.md └── README.md ├── 0165-compare-version-numbers ├── 0165-compare-version-numbers.java ├── NOTES.md └── README.md ├── 0169-majority-element ├── 0169-majority-element.java ├── NOTES.md └── README.md ├── 0191-number-of-1-bits ├── 0191-number-of-1-bits.java ├── NOTES.md └── README.md ├── 0211-design-add-and-search-words-data-structure ├── NOTES.md └── README.md ├── 0215-kth-largest-element-in-an-array ├── 0215-kth-largest-element-in-an-array.java ├── NOTES.md └── README.md ├── 0225-implement-stack-using-queues ├── 0225-implement-stack-using-queues.java ├── NOTES.md └── README.md ├── 0229-majority-element-ii ├── 0229-majority-element-ii.java ├── NOTES.md └── README.md ├── 0232-implement-queue-using-stacks ├── 0232-implement-queue-using-stacks.java ├── NOTES.md └── README.md ├── 0242-valid-anagram ├── 0242-valid-anagram.java ├── NOTES.md └── README.md ├── 0258-add-digits ├── 0258-add-digits.java ├── NOTES.md └── README.md ├── 0295-find-median-from-data-stream ├── 0295-find-median-from-data-stream.java ├── NOTES.md └── README.md ├── 0319-bulb-switcher ├── 0319-bulb-switcher.java ├── NOTES.md └── README.md ├── 0347-top-k-frequent-elements ├── 0347-top-k-frequent-elements.java ├── NOTES.md └── README.md ├── 0382-linked-list-random-node ├── 0382-linked-list-random-node.java └── NOTES.md ├── 0443-string-compression ├── 0443-string-compression.java ├── NOTES.md └── README.md ├── 0496-next-greater-element-i ├── 0496-next-greater-element-i.java ├── NOTES.md └── README.md ├── 0501-find-mode-in-binary-search-tree ├── 0501-find-mode-in-binary-search-tree.java ├── NOTES.md └── README.md ├── 0516-longest-palindromic-subsequence ├── 0516-longest-palindromic-subsequence.java ├── NOTES.md └── README.md ├── 0540-single-element-in-a-sorted-array ├── NOTES.md └── README.md ├── 0605-can-place-flowers ├── 0605-can-place-flowers.java ├── NOTES.md └── README.md ├── 0662-maximum-width-of-binary-tree ├── 0662-maximum-width-of-binary-tree.java ├── NOTES.md └── README.md ├── 0686-repeated-string-match ├── 0686-repeated-string-match.java ├── NOTES.md └── README.md ├── 0704-binary-search ├── 0704-binary-search.java ├── NOTES.md └── README.md ├── 0875-koko-eating-bananas ├── 0875-koko-eating-bananas.java ├── NOTES.md └── README.md ├── 0879-profitable-schemes ├── 0879-profitable-schemes.java ├── NOTES.md └── README.md ├── 0881-boats-to-save-people ├── 0881-boats-to-save-people.java ├── NOTES.md └── README.md ├── 0912-sort-an-array ├── 0912-sort-an-array.java ├── NOTES.md └── README.md ├── 0946-validate-stack-sequences ├── 0946-validate-stack-sequences.java ├── NOTES.md └── README.md ├── 0958-check-completeness-of-a-binary-tree ├── 0958-check-completeness-of-a-binary-tree.java ├── NOTES.md └── README.md ├── 0983-minimum-cost-for-tickets ├── NOTES.md └── README.md ├── 0987-vertical-order-traversal-of-a-binary-tree ├── 0987-vertical-order-traversal-of-a-binary-tree.java ├── NOTES.md └── README.md ├── 1160-find-words-that-can-be-formed-by-characters ├── 1160-find-words-that-can-be-formed-by-characters.java ├── NOTES.md └── README.md ├── 1254-number-of-closed-islands ├── 1254-number-of-closed-islands.java ├── NOTES.md └── README.md ├── 1266-minimum-time-visiting-all-points ├── NOTES.md └── README.md ├── 1312-minimum-insertion-steps-to-make-a-string-palindrome ├── 1312-minimum-insertion-steps-to-make-a-string-palindrome.java ├── NOTES.md └── README.md ├── 1319-number-of-operations-to-make-network-connected ├── 1319-number-of-operations-to-make-network-connected.java ├── NOTES.md └── README.md ├── 1345-jump-game-iv ├── NOTES.md └── README.md ├── 1424-diagonal-traverse-ii ├── 1424-diagonal-traverse-ii.java ├── NOTES.md └── README.md ├── 1431-kids-with-the-greatest-number-of-candies ├── NOTES.md └── README.md ├── 1444-number-of-ways-of-cutting-a-pizza ├── 1444-number-of-ways-of-cutting-a-pizza.java ├── NOTES.md └── README.md ├── 1472-design-browser-history ├── 1472-design-browser-history.java ├── NOTES.md └── README.md ├── 1491-average-salary-excluding-the-minimum-and-maximum-salary ├── 1491-average-salary-excluding-the-minimum-and-maximum-salary.java ├── NOTES.md └── README.md ├── 1539-kth-missing-positive-number ├── 1539-kth-missing-positive-number.java ├── NOTES.md └── README.md ├── 1561-maximum-number-of-coins-you-can-get ├── 1561-maximum-number-of-coins-you-can-get.java ├── NOTES.md └── README.md ├── 1579-remove-max-number-of-edges-to-keep-graph-fully-traversable ├── 1579-remove-max-number-of-edges-to-keep-graph-fully-traversable.java ├── NOTES.md └── README.md ├── 1630-arithmetic-subarrays ├── 1630-arithmetic-subarrays.java ├── NOTES.md └── README.md ├── 1685-sum-of-absolute-differences-in-a-sorted-array ├── NOTES.md └── README.md ├── 1697-checking-existence-of-edge-length-limited-paths ├── 1697-checking-existence-of-edge-length-limited-paths.java ├── NOTES.md └── README.md ├── 1721-swapping-nodes-in-a-linked-list ├── 1721-swapping-nodes-in-a-linked-list.java ├── NOTES.md └── README.md ├── 1845-seat-reservation-manager ├── 1845-seat-reservation-manager.java ├── NOTES.md └── README.md ├── 1930-unique-length-3-palindromic-subsequences ├── 1930-unique-length-3-palindromic-subsequences.java ├── NOTES.md └── README.md ├── 2187-minimum-time-to-complete-trips ├── 2187-minimum-time-to-complete-trips.java ├── NOTES.md └── README.md ├── 2300-successful-pairs-of-spells-and-potions ├── 2300-successful-pairs-of-spells-and-potions.java ├── NOTES.md └── README.md ├── 2316-count-unreachable-pairs-of-nodes-in-an-undirected-graph ├── 2316-count-unreachable-pairs-of-nodes-in-an-undirected-graph.java ├── NOTES.md └── README.md ├── 2336-smallest-number-in-infinite-set ├── 2336-smallest-number-in-infinite-set.java ├── NOTES.md └── README.md ├── 2348-number-of-zero-filled-subarrays ├── 2348-number-of-zero-filled-subarrays.java ├── NOTES.md └── README.md ├── 2360-longest-cycle-in-a-graph ├── 2360-longest-cycle-in-a-graph.java ├── NOTES.md └── README.md ├── 2390-removing-stars-from-a-string ├── 2390-removing-stars-from-a-string.java ├── NOTES.md └── README.md ├── 2405-optimal-partition-of-string ├── 2405-optimal-partition-of-string.java ├── NOTES.md └── README.md ├── 2439-minimize-maximum-of-array ├── 2439-minimize-maximum-of-array.java ├── NOTES.md └── README.md ├── 2444-count-subarrays-with-fixed-bounds ├── 2444-count-subarrays-with-fixed-bounds.java ├── NOTES.md └── README.md ├── 2785-sort-vowels-in-a-string ├── 2785-sort-vowels-in-a-string.java └── README.md ├── Flattening a Linked List - GFG ├── README.md └── flattening-a-linked-list.java ├── Fractional Knapsack - GFG ├── README.md └── fractional-knapsack.java ├── IntelliJava ├── .idea │ ├── .gitignore │ ├── description.html │ ├── encodings.xml │ ├── misc.xml │ ├── modules.xml │ ├── project-template.xml │ ├── uiDesigner.xml │ └── vcs.xml ├── IntelliJava.iml ├── out │ └── production │ │ └── IntelliJava │ │ ├── CustomLinkedList │ │ ├── CLL$Node.class │ │ ├── CLL.class │ │ ├── DLL$Node.class │ │ ├── DLL.class │ │ ├── LL$Node.class │ │ ├── LL.class │ │ └── Main.class │ │ └── com │ │ └── seal │ │ ├── Arrays.class │ │ ├── Basics.class │ │ ├── CelciusToFarenheit.class │ │ ├── Conditionals.class │ │ ├── HelloWorld.class │ │ ├── Loops.class │ │ ├── Methods.class │ │ ├── ReverseNum.class │ │ ├── Sum.class │ │ ├── TypeCasting.class │ │ ├── VarArgs.class │ │ ├── arrayformlc.class │ │ ├── contatenationarray.class │ │ ├── countdigits.class │ │ ├── fibonacci.class │ │ ├── goodpairs.class │ │ ├── mostcandies.class │ │ ├── richest.class │ │ ├── runningsum.class │ │ ├── shufflearray.class │ │ └── switchcase.class └── src │ ├── CustomLinkedList │ ├── CLL.java │ ├── DLL.java │ ├── LL.java │ └── Main.java │ ├── OOPSPrac │ └── Dog.java │ ├── OOPSPrac2 │ └── Chair.java │ └── com │ └── seal │ ├── Arrays.java │ ├── Basics.java │ ├── CelciusToFarenheit.java │ ├── Conditionals.java │ ├── HelloWorld.java │ ├── Loops.java │ ├── Methods.java │ ├── ReverseNum.java │ ├── Sum.java │ ├── TypeCasting.java │ ├── VarArgs.java │ ├── arrayformlc.java │ ├── contatenationarray.java │ ├── countdigits.java │ ├── fibonacci.java │ ├── goodpairs.java │ ├── mostcandies.java │ ├── richest.java │ ├── runningsum.java │ ├── shufflearray.java │ └── switchcase.java ├── Job Sequencing Problem - GFG ├── README.md └── job-sequencing-problem.java ├── K-th element of two Arrays - GFG ├── README.md └── kth-element-of-two-arrays.java ├── LICENSE ├── Largest subarray with 0 sum - GFG ├── README.md └── largest-subarray-with-0-sum.java ├── Learning&Leetcode ├── ArrayConcatenation.java ├── EquivalentStrings.java ├── GFG.class ├── HelloWorld.class ├── HelloWorld.java ├── NumMatrix.java ├── RunningSum.java ├── Solution.class ├── absdiff.class ├── absdiff.java ├── addbinary.java ├── alikestrings.java ├── alternatingbits.java ├── arrangingcoins.class ├── arrangingcoins.java ├── arrayform.class ├── arrayform.java ├── arrayfrompermutation.java ├── arrayintersection.java ├── arrayintersection2.java ├── arrayparitioning1.java ├── artihmeticprogression.java ├── assigncookies.class ├── assigncookies.java ├── avgexmaxmin.java ├── badversion.java ├── binarycodes.class ├── binarycodes.java ├── binarygap.class ├── binarygap.java ├── binarysearch.java ├── binarysearchrecusrsive.java ├── binarytreefactors.class ├── binarytreefactors.java ├── canjump.class ├── canjump.java ├── checkbst.java ├── checkdouble.java ├── checkifsorted.java ├── climbingstairs.java ├── coloumnumbers.class ├── coloumnumbers.java ├── combinationsum.class ├── combinationsum.java ├── combinationsumiv.class ├── combinationsumiv.java ├── combinationsumprac.java ├── combinationsumtwo.class ├── combinationsumtwo.java ├── complementnum.class ├── complementnum.java ├── consistentstrings.java ├── countiterms.java ├── countnegative.class ├── countnegative.java ├── cyclesort.class ├── cyclesort.java ├── cyclicsort.class ├── cyclicsort.java ├── decibinary.java ├── decodemessage.java ├── defangingip.java ├── deleteopforstrings.class ├── deleteopforstrings.java ├── dicetargetsum.class ├── dicetargetsum.java ├── disappeearednumsprac.java ├── divide2int.class ├── divide2int.java ├── drcryptstring.class ├── drcryptstring.java ├── duplicate.java ├── duplicatenumprac.java ├── evennumdigs.java ├── faircandyswap.java ├── findallduplicates.class ├── findallduplicates.java ├── findandreplacepattern.java ├── finddisaappearednums.class ├── finddisaappearednums.java ├── findduplicatecyclic.java ├── findnum.java ├── findpeakelement.class ├── findpeakelement.java ├── firstandlastposi.class ├── firstandlastposi.java ├── firstmissingpositive.class ├── firstmissingpositive.java ├── freqsort.class ├── freqsort.java ├── gasstation.java ├── globalparser.java ├── goodpairs.java ├── heightchecker.java ├── highestaltitude.class ├── highestaltitude.java ├── houserobber.java ├── insertpos.class ├── insertpos.java ├── interleavingstrings.class ├── interleavingstrings.java ├── intersectionprac.java ├── invert.java ├── jumpgamevii.java ├── kinversepairs.class ├── kinversepairs.java ├── knights.class ├── knights.java ├── kthlargestprac.class ├── kthlargestprac.java ├── kthmax.java ├── largestnumber.java ├── largesttriangle.java ├── leftdowntraversal.class ├── leftdowntraversal.java ├── lengthoflastword.java ├── lettercombinations.class ├── lettercombinations.java ├── levelordertraversal.java ├── linkedlistintersection$ListNode.class ├── linkedlistintersection.class ├── linkedlistintersection.java ├── longestcommonprefix.java ├── longestconsecutive.class ├── longestconsecutive.java ├── longestincreasingsubsequence.class ├── longestincreasingsubsequence.java ├── longestnicesubstring.java ├── longestplaindromicsubstring.class ├── longestplaindromicsubstring.java ├── longeststringchain.java ├── longestsubstringnorepeat.class ├── longestsubstringnorepeat.java ├── longestvalidbrackets.class ├── longestvalidbrackets.java ├── longpressedchar.class ├── longpressedchar.java ├── luckynum.class ├── luckynum.java ├── majorityelement.class ├── majorityelement.java ├── make2arrayequal.java ├── matchingsubsequence.class ├── matchingsubsequence.java ├── matdiagsum.java ├── max3prod.java ├── maxareacake.java ├── maxcandies.java ├── maxerasurevalue.class ├── maxerasurevalue.java ├── maximumgold.class ├── maximumgold.java ├── maximumvalue.java ├── maxislandarea.java ├── maxksum.class ├── maxksum.java ├── maxpointscards.class ├── maxpointscards.java ├── maxpopulation.class ├── maxpopulation.java ├── maxproduct2.java ├── maxprodwordlengths.class ├── maxprodwordlengths.java ├── maxrepeatingsubstring.class ├── maxrepeatingsubstring.java ├── maxsubarray.class ├── maxsubarray.java ├── maxunitsontruck.java ├── mazeproblem.class ├── mazeproblem.java ├── mcoloring.java ├── mergeintervals.java ├── mergesortedarray.class ├── mergesortedarray.java ├── mergesortedarrayprac.java ├── mergestringsalternatively.java ├── minmove.class ├── minmove.java ├── missingnum.class ├── missingnum.java ├── missingnumcyclic.class ├── missingnumcyclic.java ├── movechips.class ├── movechips.java ├── mssingnum.java ├── needlenhaystack.java ├── nondecreasingarray.class ├── nondecreasingarray.java ├── nqueens.class ├── nqueens.java ├── nqueens2.class ├── nqueens2.java ├── nqueensprac.class ├── nqueensprac.java ├── nqueensprac2.class ├── nqueensprac2.java ├── oddmatrix.java ├── onesnzeros.class ├── onesnzeros.java ├── pangram.class ├── pangram.java ├── peakmountainarray.class ├── peakmountainarray.java ├── perefctsquare.class ├── perefctsquare.java ├── permuatations.class ├── permuatations.java ├── permutattionsequence.class ├── permutattionsequence.java ├── plusone.class ├── plusone.java ├── poweroftwo.java ├── powxn.class ├── powxn.java ├── prime.class ├── prime.java ├── primesetbits.class ├── primesetbits.java ├── productsum.java ├── ranktransform.class ├── ranktransform.java ├── reachanumber.class ├── reachanumber.java ├── reconstructqueue.java ├── recursivebubble.class ├── recursivebubble.java ├── recursivestrongs.class ├── recursivestrongs.java ├── reducetozero.java ├── reducextozero.class ├── reducextozero.java ├── relativesort.class ├── relativesort.java ├── removeduplicates.class ├── removeduplicates.java ├── removepalindromesubs.class ├── removepalindromesubs.java ├── reshapearray.class ├── reshapearray.java ├── reversebits.class ├── reversebits.java ├── reverseprefix.java ├── reversestring.java ├── reversewords.java ├── richestcustomer.java ├── rightinterval.class ├── rightinterval.java ├── rightviewofbst.java ├── robotorigin.java ├── romantoint.java ├── rotatearray.java ├── rotatedarray.class ├── rotatedarray.java ├── rotatedsortedarray2.java ├── runningsumprac.java ├── secretmessage.java ├── segmentofones.class ├── segmentofones.java ├── setmatrixzeros.class ├── setmatrixzeros.java ├── setmismatch.class ├── setmismatch.java ├── setmismatchcyclic.class ├── setmismatchcyclic.java ├── setmismatchprac.class ├── setmismatchprac.java ├── shufflearray.java ├── shufflestring.class ├── shufflestring.java ├── similartriangles.class ├── similartriangles.java ├── singleelement.class ├── singleelement.java ├── singlenum.java ├── smallernos.java ├── smallerthancurrent2.java ├── smallestletter.class ├── smallestletter.java ├── sortbybits.class ├── sortbybits.java ├── sortbyparity.java ├── sortbyparity2.class ├── sortbyparity2.java ├── sortcolours.class ├── sortcolours.java ├── sortedfirstandlastposi.java ├── sortedsquares.java ├── sortsentence.class ├── sortsentence.java ├── specialarray.class ├── specialarray.java ├── specialarrayx.class ├── specialarrayx.java ├── specialcyclicsort.class ├── specialcyclicsort.java ├── spiralmatrix.class ├── spiralmatrix.java ├── spiralmatrix2.class ├── spiralmatrix2.java ├── splitstringspalindrome.java ├── sqrtx.class ├── sqrtx.java ├── subsequence.class ├── subsequence.java ├── subsetsofarray.class ├── subsetsofarray.java ├── substrings.class ├── substrings.java ├── substringsinstring.class ├── substringsinstring.java ├── substringstemp.class ├── substringstemp.java ├── substringwithsowords.java ├── subtractProductAndSum.java ├── sudokusolver.java ├── sumofpowerofthree.java ├── sumofxors.java ├── target.class ├── targetarray.class ├── targetarray.java ├── tempCodeRunnerFile.java ├── test.class ├── test.java ├── thirdmax.class ├── thirdmax.java ├── threesum.class ├── threesum.java ├── threesumclosest.java ├── threesumprac.java ├── tolowercase.class ├── tolowercase.java ├── transpose.java ├── transposematrix.java ├── treefrompreandinorder.java ├── twosum.class ├── twosum.java ├── twosum2.class ├── twosum2.java ├── twosumprac.java ├── uniquefrequency.class ├── uniquefrequency.java ├── uniqueint.class ├── uniqueint.java ├── uniquepaths.class ├── validpalindrome.class ├── validpalindrome.java ├── validpalindromeone.java ├── validparentheis.java ├── validsudoku.java ├── wigglesubsequence.java ├── wordsearch.class └── wordsearch.java ├── N meetings in one room - GFG ├── README.md └── n-meetings-in-one-room.java ├── README.md └── Subset Sums - GFG ├── README.md └── subset-sums.java /0004-median-of-two-sorted-arrays/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0005-longest-palindromic-substring/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0005-longest-palindromic-substring/README.md: -------------------------------------------------------------------------------- 1 |

5. Longest Palindromic Substring

Medium


Given a string s, return the longest palindromic substring in s.

2 | 3 |

 

4 |

Example 1:

5 | 6 |
Input: s = "babad"
 7 | Output: "bab"
 8 | Explanation: "aba" is also a valid answer.
 9 | 
10 | 11 |

Example 2:

12 | 13 |
Input: s = "cbbd"
14 | Output: "bb"
15 | 
16 | 17 |

 

18 |

Constraints:

19 | 20 | 24 |
-------------------------------------------------------------------------------- /0013-roman-to-integer/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0014-longest-common-prefix/0014-longest-common-prefix.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public String longestCommonPrefix(String[] strs) { 3 | 4 | int ans = 0; 5 | 6 | int minLen = Integer.MAX_VALUE; 7 | 8 | for(String s: strs){ 9 | minLen = Math.min(minLen, s.length()); 10 | } 11 | 12 | Outer: 13 | for(int strC=0; strC= strs[0].length()) break; 16 | 17 | char currPref = strs[0].charAt(strC); 18 | 19 | for(int i=1; i= strs[i].length() || currPref != strs[i].charAt(strC)){ 22 | break Outer; 23 | } 24 | } 25 | 26 | ans++; 27 | } 28 | 29 | return strs[0].substring(0, ans); 30 | 31 | } 32 | } -------------------------------------------------------------------------------- /0014-longest-common-prefix/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0020-valid-parentheses/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0021-merge-two-sorted-lists/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0023-merge-k-sorted-lists/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0028-find-the-index-of-the-first-occurrence-in-a-string/0028-find-the-index-of-the-first-occurrence-in-a-string.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int strStr(String h, String n) { 3 | // traverse like a sliding window 4 | 5 | for(int i = 0; i> combinationSum(int[] arr, int target) { 3 | Arrays.sort(arr); 4 | return getCombi (arr, target, 0, new ArrayList<>(), new ArrayList<>(), new HashSet<>()); 5 | } 6 | private List> getCombi(int[] arr, int t, int ind, ArrayList curr, List> ans, HashSet> hs){ 7 | 8 | if(t == 0){ 9 | 10 | if(!hs.contains(curr)){ 11 | ans.add(new ArrayList<>(curr)); 12 | hs.add(ans.get(ans.size()-1)); 13 | } 14 | 15 | return ans; 16 | } 17 | 18 | if (t < 0 || ind == arr.length) return ans; 19 | 20 | curr.add(arr[ind]); 21 | getCombi(arr, t-arr[ind], ind, curr, ans, hs); 22 | curr.remove(curr.size()-1); 23 | 24 | getCombi(arr, t, ind+1, curr, ans, hs); 25 | 26 | return ans; 27 | } 28 | } -------------------------------------------------------------------------------- /0039-combination-sum/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0040-combination-sum-ii/0040-combination-sum-ii.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | List> ans; 3 | public List> combinationSum2(int[] arr, int target) { 4 | Arrays.sort(arr); 5 | ans = new ArrayList<>(); 6 | getCombi2(arr, target, 0, new ArrayList<>()); 7 | return ans; 8 | } 9 | private void getCombi2(int[]arr, int t, int ind, ArrayList curr){ 10 | 11 | if(t==0){ 12 | ans.add(new ArrayList<>(curr)); 13 | return; 14 | } 15 | 16 | // if(t < 0 || ind == arr.length) return; 17 | 18 | for(int i=ind; iind && arr[i]==arr[i-1]) continue; 21 | 22 | if(arr[i] > t) break; 23 | 24 | curr.add(arr[i]); 25 | 26 | getCombi2(arr, t-arr[i], i+1, curr); 27 | 28 | curr.remove(curr.size()-1); 29 | 30 | } 31 | } 32 | } -------------------------------------------------------------------------------- /0040-combination-sum-ii/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0046-permutations/0046-permutations.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | private List> ans; 3 | 4 | public List> permute(int[] nums) { 5 | 6 | ans = new ArrayList<>(); 7 | 8 | getPermu(nums, 0, new ArrayList<>()); 9 | 10 | return ans; 11 | } 12 | 13 | private void getPermu(int[] arr, int ind, ArrayList curr){ 14 | 15 | if(curr.size() == arr.length){ 16 | ans.add(new ArrayList<>(curr)); 17 | return; 18 | } 19 | 20 | for(int i=0; i arr = new ArrayList<>(); 7 | 8 | for(int i=1; i stack = new Stack<>(); // create a stack to keep track of directories 4 | String[] directories = path.split("/"); // split the path by slash '/' 5 | for (String dir : directories) { // iterate over the directories 6 | if (dir.equals(".") || dir.isEmpty()) { // ignore the current directory '.' and empty directories 7 | continue; 8 | } else if (dir.equals("..")) { // go one level up for double period '..' 9 | if (!stack.isEmpty()) { // if stack is not empty, pop the top element 10 | stack.pop(); 11 | } 12 | } else { // for any other directory, push it to the stack 13 | stack.push(dir); 14 | } 15 | } 16 | return "/" + String.join("/", stack); // join the directories in the stack with slash '/' and add a slash at the beginning 17 | } 18 | } -------------------------------------------------------------------------------- /0071-simplify-path/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0087-scramble-string/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0090-subsets-ii/0090-subsets-ii.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public List> subsetsWithDup(int[] nums) { 3 | Arrays.sort(nums); 4 | return getSubsets(nums, 0, new ArrayList<>(), new ArrayList<>(), new HashSet<>()); 5 | } 6 | 7 | private List> getSubsets(int[] arr, int ind, ArrayList curr, List> ans, HashSet> hs){ 8 | if(ind == arr.length){ 9 | 10 | if(!hs.contains(curr)){ 11 | 12 | ArrayList newAns = new ArrayList<>(curr); 13 | 14 | ans.add(newAns); 15 | hs.add(newAns); 16 | } 17 | 18 | return ans; 19 | } 20 | 21 | getSubsets(arr, ind+1, curr, ans, hs); 22 | 23 | curr.add(arr[ind]); 24 | 25 | getSubsets(arr, ind+1, curr, ans, hs); 26 | 27 | curr.remove(curr.size()-1); 28 | 29 | return ans; 30 | 31 | } 32 | } -------------------------------------------------------------------------------- /0090-subsets-ii/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0090-subsets-ii/README.md: -------------------------------------------------------------------------------- 1 |

90. Subsets II

Medium


Given an integer array nums that may contain duplicates, return all possible subsets (the power set).

2 | 3 |

The solution set must not contain duplicate subsets. Return the solution in any order.

4 | 5 |

 

6 |

Example 1:

7 |
Input: nums = [1,2,2]
 8 | Output: [[],[1],[1,2],[1,2,2],[2],[2,2]]
 9 | 

Example 2:

10 |
Input: nums = [0]
11 | Output: [[],[0]]
12 | 
13 |

 

14 |

Constraints:

15 | 16 |
    17 |
  • 1 <= nums.length <= 10
  • 18 |
  • -10 <= nums[i] <= 10
  • 19 |
20 |
-------------------------------------------------------------------------------- /0094-binary-tree-inorder-traversal/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0101-symmetric-tree/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0106-construct-binary-tree-from-inorder-and-postorder-traversal/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0109-convert-sorted-list-to-binary-search-tree/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0129-sum-root-to-leaf-numbers/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0131-palindrome-partitioning/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0131-palindrome-partitioning/README.md: -------------------------------------------------------------------------------- 1 |

131. Palindrome Partitioning

Medium


Given a string s, partition s such that every substring of the partition is a palindrome. Return all possible palindrome partitioning of s.

2 | 3 |

 

4 |

Example 1:

5 |
Input: s = "aab"
 6 | Output: [["a","a","b"],["aa","b"]]
 7 | 

Example 2:

8 |
Input: s = "a"
 9 | Output: [["a"]]
10 | 
11 |

 

12 |

Constraints:

13 | 14 |
    15 |
  • 1 <= s.length <= 16
  • 16 |
  • s contains only lowercase English letters.
  • 17 |
18 |
-------------------------------------------------------------------------------- /0133-clone-graph/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0142-linked-list-cycle-ii/0142-linked-list-cycle-ii.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for singly-linked list. 3 | * class ListNode { 4 | * int val; 5 | * ListNode next; 6 | * ListNode(int x) { 7 | * val = x; 8 | * next = null; 9 | * } 10 | * } 11 | */ 12 | public class Solution { 13 | public ListNode detectCycle(ListNode head) { 14 | 15 | if(head==null || head.next == null) return null; 16 | 17 | ListNode f = head; 18 | ListNode s = head; 19 | 20 | while(f!=null && f.next!=null){ 21 | s = s.next; 22 | f = f.next.next; 23 | if(s==f) break; 24 | } 25 | if(f==null || f.next==null) return null; 26 | while(head!=f){ 27 | head = head.next; 28 | f = f.next; 29 | } 30 | return head; 31 | } 32 | } -------------------------------------------------------------------------------- /0142-linked-list-cycle-ii/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0144-binary-tree-preorder-traversal/0144-binary-tree-preorder-traversal.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * public class TreeNode { 4 | * int val; 5 | * TreeNode left; 6 | * TreeNode right; 7 | * TreeNode() {} 8 | * TreeNode(int val) { this.val = val; } 9 | * TreeNode(int val, TreeNode left, TreeNode right) { 10 | * this.val = val; 11 | * this.left = left; 12 | * this.right = right; 13 | * } 14 | * } 15 | */ 16 | class Solution { 17 | public List preorderTraversal(TreeNode root) { 18 | return preorder(root, new ArrayList<>()); 19 | } 20 | private List preorder(TreeNode root, List ans){ 21 | 22 | if(root == null){ 23 | return ans; 24 | } 25 | 26 | ans.add(root.val); 27 | 28 | preorder(root.left, ans); 29 | 30 | preorder(root.right, ans); 31 | 32 | return ans; 33 | 34 | } 35 | } -------------------------------------------------------------------------------- /0144-binary-tree-preorder-traversal/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0145-binary-tree-postorder-traversal/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0151-reverse-words-in-a-string/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0152-maximum-product-subarray/0152-maximum-product-subarray.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int maxProduct(int[] nums) { 3 | 4 | int n = nums.length; 5 | 6 | int max = Integer.MIN_VALUE; 7 | 8 | int prefix = 1; 9 | int suffix = 1; 10 | 11 | for(int i=0; i>>= 1; 14 | 15 | } 16 | 17 | return ans; 18 | } 19 | } -------------------------------------------------------------------------------- /0191-number-of-1-bits/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0211-design-add-and-search-words-data-structure/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0215-kth-largest-element-in-an-array/0215-kth-largest-element-in-an-array.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int findKthLargest(int[] nums, int k) { 3 | 4 | PriorityQueue pq = new PriorityQueue<>(Collections.reverseOrder()); 5 | 6 | for(int i: nums) pq.offer(i); 7 | 8 | while(k>1){ 9 | pq.poll(); 10 | k--; 11 | } 12 | 13 | return pq.peek(); 14 | 15 | } 16 | 17 | } -------------------------------------------------------------------------------- /0215-kth-largest-element-in-an-array/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0225-implement-stack-using-queues/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0229-majority-element-ii/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0232-implement-queue-using-stacks/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0242-valid-anagram/0242-valid-anagram.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean isAnagram(String s, String t) { 3 | 4 | HashMap hm = new HashMap<>(); 5 | 6 | if(s.length()!=t.length()) return false; 7 | 8 | for(char c: s.toCharArray()){ 9 | hm.put(c, hm.getOrDefault(c, 0)+1); 10 | } 11 | 12 | for(char c: t.toCharArray()){ 13 | 14 | if(!hm.containsKey(c)) return false; 15 | 16 | hm.put(c, hm.get(c)-1); 17 | 18 | } 19 | 20 | for(char c: hm.keySet()){ 21 | if(hm.get(c)!=0) return false; 22 | } 23 | 24 | return true; 25 | 26 | 27 | } 28 | } -------------------------------------------------------------------------------- /0242-valid-anagram/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0258-add-digits/0258-add-digits.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int addDigits(int num) { 3 | while(num>9){ 4 | 5 | // here add the digits 6 | int sum = 0; 7 | while(num>0){ 8 | sum += num %10; 9 | num /= 10; 10 | } 11 | // System.out.println(sum); 12 | num=sum; 13 | 14 | } 15 | return num; 16 | } 17 | } -------------------------------------------------------------------------------- /0258-add-digits/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0258-add-digits/README.md: -------------------------------------------------------------------------------- 1 |

258. Add Digits

Easy


Given an integer num, repeatedly add all its digits until the result has only one digit, and return it.

2 | 3 |

 

4 |

Example 1:

5 | 6 |
Input: num = 38
 7 | Output: 2
 8 | Explanation: The process is
 9 | 38 --> 3 + 8 --> 11
10 | 11 --> 1 + 1 --> 2 
11 | Since 2 has only one digit, return it.
12 | 
13 | 14 |

Example 2:

15 | 16 |
Input: num = 0
17 | Output: 0
18 | 
19 | 20 |

 

21 |

Constraints:

22 | 23 |
    24 |
  • 0 <= num <= 231 - 1
  • 25 |
26 | 27 |

 

28 |

Follow up: Could you do it without any loop/recursion in O(1) runtime?

29 |
-------------------------------------------------------------------------------- /0295-find-median-from-data-stream/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0319-bulb-switcher/0319-bulb-switcher.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int bulbSwitch(int n) { 3 | 4 | return (int)Math.sqrt(n); 5 | } 6 | } -------------------------------------------------------------------------------- /0319-bulb-switcher/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0347-top-k-frequent-elements/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0382-linked-list-random-node/0382-linked-list-random-node.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for singly-linked list. 3 | * public class ListNode { 4 | * int val; 5 | * ListNode next; 6 | * ListNode() {} 7 | * ListNode(int val) { this.val = val; } 8 | * ListNode(int val, ListNode next) { this.val = val; this.next = next; } 9 | * } 10 | */ 11 | class Solution { 12 | private ArrayList nodes = new ArrayList<>(); 13 | 14 | private int size = 0; 15 | 16 | public Solution(ListNode head) { 17 | ListNode temp = head; 18 | while(temp!=null){ 19 | nodes.add(temp); 20 | temp = temp.next; 21 | } 22 | size = nodes.size(); 23 | } 24 | 25 | public int getRandom() { 26 | return nodes.get((int)(Math.random() * size)).val; 27 | } 28 | } 29 | 30 | /** 31 | * Your Solution object will be instantiated and called as such: 32 | * Solution obj = new Solution(head); 33 | * int param_1 = obj.getRandom(); 34 | */ -------------------------------------------------------------------------------- /0382-linked-list-random-node/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0443-string-compression/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0496-next-greater-element-i/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0501-find-mode-in-binary-search-tree/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0516-longest-palindromic-subsequence/0516-longest-palindromic-subsequence.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int longestPalindromeSubseq(String s) { 3 | int n = s.length(); 4 | int[][] memo = new int[n][n]; 5 | return lps(s, 0, n - 1, memo); 6 | } 7 | 8 | private int lps(String s, int i, int j, int[][] memo) { 9 | if (memo[i][j] != 0) { 10 | return memo[i][j]; 11 | } 12 | if (i > j) { 13 | return 0; 14 | } 15 | if (i == j) { 16 | return 1; 17 | } 18 | 19 | if (s.charAt(i) == s.charAt(j)) { 20 | memo[i][j] = lps(s, i + 1, j - 1, memo) + 2; 21 | } else { 22 | memo[i][j] = Math.max(lps(s, i + 1, j, memo), lps(s, i, j - 1, memo)); 23 | } 24 | return memo[i][j]; 25 | } 26 | } -------------------------------------------------------------------------------- /0516-longest-palindromic-subsequence/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0540-single-element-in-a-sorted-array/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0540-single-element-in-a-sorted-array/README.md: -------------------------------------------------------------------------------- 1 |

540. Single Element in a Sorted Array

Medium


You are given a sorted array consisting of only integers where every element appears exactly twice, except for one element which appears exactly once.

2 | 3 |

Return the single element that appears only once.

4 | 5 |

Your solution must run in O(log n) time and O(1) space.

6 | 7 |

 

8 |

Example 1:

9 |
Input: nums = [1,1,2,3,3,4,4,8,8]
10 | Output: 2
11 | 

Example 2:

12 |
Input: nums = [3,3,7,7,10,11,11]
13 | Output: 10
14 | 
15 |

 

16 |

Constraints:

17 | 18 |
    19 |
  • 1 <= nums.length <= 105
  • 20 |
  • 0 <= nums[i] <= 105
  • 21 |
22 |
-------------------------------------------------------------------------------- /0605-can-place-flowers/0605-can-place-flowers.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public boolean canPlaceFlowers(int[] flowerbed, int n) { 3 | int count = 0; 4 | for (int i = 0; i < flowerbed.length; i++) { 5 | // Check if the current plot is empty. 6 | if (flowerbed[i] == 0) { 7 | // Check if the left and right plots are empty. 8 | boolean emptyLeftPlot = (i == 0) || (flowerbed[i - 1] == 0); 9 | boolean emptyRightPlot = (i == flowerbed.length - 1) || (flowerbed[i + 1] == 0); 10 | 11 | // If both plots are empty, we can plant a flower here. 12 | if (emptyLeftPlot && emptyRightPlot) { 13 | flowerbed[i] = 1; 14 | count++; 15 | } 16 | } 17 | } 18 | return count >= n; 19 | } 20 | } -------------------------------------------------------------------------------- /0605-can-place-flowers/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0662-maximum-width-of-binary-tree/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0686-repeated-string-match/0686-repeated-string-match.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int repeatedStringMatch(String a, String b) { 3 | 4 | int count = 0; 5 | 6 | StringBuilder sb = new StringBuilder(); 7 | 8 | while(sb.length() < b.length()){ 9 | sb.append(a); 10 | count++; 11 | } 12 | 13 | if(sb.toString().contains(b)) return count; 14 | 15 | if(sb.append(a).toString().contains(b)) return ++count; 16 | 17 | return -1; 18 | 19 | } 20 | } -------------------------------------------------------------------------------- /0686-repeated-string-match/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0704-binary-search/0704-binary-search.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int search(int[] arr, int target) { 3 | int start = 0; 4 | int end = arr.length-1; 5 | while(start<=end){ 6 | int mid = start + (end-start)/2; 7 | if(arr[mid] == target){ 8 | return mid; 9 | } 10 | else if(arr[mid] < target){ 11 | start = mid+1; 12 | } 13 | else{ 14 | end = mid-1; 15 | } 16 | } 17 | return -1; 18 | } 19 | } -------------------------------------------------------------------------------- /0704-binary-search/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0875-koko-eating-bananas/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0879-profitable-schemes/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0881-boats-to-save-people/0881-boats-to-save-people.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int numRescueBoats(int[] people, int limit) { 3 | // sort array 4 | // delete pairs 5 | 6 | Arrays.sort(people); 7 | 8 | int ans = 0; 9 | 10 | int left = 0; 11 | int right = people.length-1; 12 | 13 | // 1 2 2 3 14 | 15 | while(left <= right){ 16 | if(people[left] + people[right] <= limit){ 17 | left++; 18 | right--; 19 | ans++; 20 | } 21 | else{ 22 | right--; 23 | ans++; 24 | } 25 | 26 | } 27 | 28 | return ans; 29 | 30 | } 31 | 32 | } -------------------------------------------------------------------------------- /0881-boats-to-save-people/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0912-sort-an-array/0912-sort-an-array.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int[] sortArray(int[] nums) { 3 | Arrays.sort(nums); 4 | return nums; 5 | } 6 | } -------------------------------------------------------------------------------- /0912-sort-an-array/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0946-validate-stack-sequences/0946-validate-stack-sequences.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean validateStackSequences(int[] pushed, int[] popped) { 3 | 4 | Stack s = new Stack(); 5 | 6 | int j = 0; 7 | for(int ele: pushed){ 8 | s.push(ele); 9 | while(!s.isEmpty() && j pq; 4 | public SeatManager(int n) { 5 | pq = new PriorityQueue<>(); 6 | } 7 | 8 | public int reserve() { 9 | if(!pq.isEmpty()) return pq.poll(); 10 | return smallest++; 11 | } 12 | 13 | public void unreserve(int seatNumber) { 14 | pq.offer(seatNumber); 15 | } 16 | } 17 | 18 | /** 19 | * Your SeatManager object will be instantiated and called as such: 20 | * SeatManager obj = new SeatManager(n); 21 | * int param_1 = obj.reserve(); 22 | * obj.unreserve(seatNumber); 23 | */ -------------------------------------------------------------------------------- /1845-seat-reservation-manager/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /1930-unique-length-3-palindromic-subsequences/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /2187-minimum-time-to-complete-trips/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /2300-successful-pairs-of-spells-and-potions/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /2316-count-unreachable-pairs-of-nodes-in-an-undirected-graph/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /2336-smallest-number-in-infinite-set/2336-smallest-number-in-infinite-set.java: -------------------------------------------------------------------------------- 1 | class SmallestInfiniteSet { 2 | 3 | PriorityQueue q; 4 | int minnum; 5 | 6 | public SmallestInfiniteSet() { 7 | q = new PriorityQueue<>(); 8 | minnum = 1; 9 | } 10 | 11 | public int popSmallest() { 12 | 13 | if(!q.isEmpty()){ 14 | 15 | return q.poll(); 16 | 17 | } 18 | return minnum++; 19 | } 20 | 21 | public void addBack(int num) { 22 | if(num dist, boolean[] visit) { 5 | visit[node] = true; 6 | int neighbor = edges[node]; 7 | 8 | if (neighbor != -1 && !visit[neighbor]) { 9 | dist.put(neighbor, dist.get(node) + 1); 10 | dfs(neighbor, edges, dist, visit); 11 | } else if (neighbor != -1 && dist.containsKey(neighbor)) { 12 | answer = Math.max(answer, dist.get(node) - dist.get(neighbor) + 1); 13 | } 14 | } 15 | 16 | public int longestCycle(int[] edges) { 17 | int n = edges.length; 18 | boolean[] visit = new boolean[n]; 19 | 20 | for (int i = 0; i < n; i++) { 21 | if (!visit[i]) { 22 | Map dist = new HashMap<>(); 23 | dist.put(i, 1); 24 | dfs(i, edges, dist, visit); 25 | } 26 | } 27 | return answer; 28 | } 29 | } -------------------------------------------------------------------------------- /2360-longest-cycle-in-a-graph/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /2390-removing-stars-from-a-string/2390-removing-stars-from-a-string.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public String removeStars(String s) { 3 | 4 | // can use a stack 5 | 6 | StringBuilder ans = new StringBuilder(); 7 | 8 | for(char c: s.toCharArray()){ 9 | 10 | if(c == '*'){ 11 | ans.deleteCharAt(ans.length()-1); 12 | } 13 | else ans.append(c); 14 | 15 | } 16 | 17 | return ans.toString(); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /2390-removing-stars-from-a-string/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /2405-optimal-partition-of-string/2405-optimal-partition-of-string.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int partitionString(String s) { 3 | 4 | // slidng window 5 | // use hashsets for the window 6 | 7 | HashSet curr = new HashSet<>(); 8 | 9 | int ans = 1; 10 | 11 | int windowStart = 0; 12 | 13 | for(int i=0; i(); 20 | curr.add(ch); 21 | // System.out.println("-"); 22 | } 23 | else{ 24 | curr.add(ch); 25 | } 26 | // System.out.println(Arrays.asList(curr)); 27 | } 28 | 29 | return ans; 30 | 31 | } 32 | } -------------------------------------------------------------------------------- /2405-optimal-partition-of-string/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /2439-minimize-maximum-of-array/2439-minimize-maximum-of-array.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int minimizeArrayValue(int[] nums) { 3 | 4 | long sum = 0; 5 | 6 | int ans = nums[0]; 7 | sum = ans; 8 | 9 | for(int i=1; iSimple Java application that includes a class with main() method -------------------------------------------------------------------------------- /IntelliJava/.idea/encodings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /IntelliJava/.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /IntelliJava/.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /IntelliJava/.idea/project-template.xml: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /IntelliJava/.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /IntelliJava/IntelliJava.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /IntelliJava/out/production/IntelliJava/CustomLinkedList/CLL$Node.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sabyasachi-Seal/CodingInJava/e747fed9d30fdbfda059ac24c458b23707b40b31/IntelliJava/out/production/IntelliJava/CustomLinkedList/CLL$Node.class -------------------------------------------------------------------------------- /IntelliJava/out/production/IntelliJava/CustomLinkedList/CLL.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sabyasachi-Seal/CodingInJava/e747fed9d30fdbfda059ac24c458b23707b40b31/IntelliJava/out/production/IntelliJava/CustomLinkedList/CLL.class -------------------------------------------------------------------------------- /IntelliJava/out/production/IntelliJava/CustomLinkedList/DLL$Node.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sabyasachi-Seal/CodingInJava/e747fed9d30fdbfda059ac24c458b23707b40b31/IntelliJava/out/production/IntelliJava/CustomLinkedList/DLL$Node.class -------------------------------------------------------------------------------- /IntelliJava/out/production/IntelliJava/CustomLinkedList/DLL.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sabyasachi-Seal/CodingInJava/e747fed9d30fdbfda059ac24c458b23707b40b31/IntelliJava/out/production/IntelliJava/CustomLinkedList/DLL.class -------------------------------------------------------------------------------- /IntelliJava/out/production/IntelliJava/CustomLinkedList/LL$Node.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sabyasachi-Seal/CodingInJava/e747fed9d30fdbfda059ac24c458b23707b40b31/IntelliJava/out/production/IntelliJava/CustomLinkedList/LL$Node.class -------------------------------------------------------------------------------- /IntelliJava/out/production/IntelliJava/CustomLinkedList/LL.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sabyasachi-Seal/CodingInJava/e747fed9d30fdbfda059ac24c458b23707b40b31/IntelliJava/out/production/IntelliJava/CustomLinkedList/LL.class -------------------------------------------------------------------------------- /IntelliJava/out/production/IntelliJava/CustomLinkedList/Main.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sabyasachi-Seal/CodingInJava/e747fed9d30fdbfda059ac24c458b23707b40b31/IntelliJava/out/production/IntelliJava/CustomLinkedList/Main.class -------------------------------------------------------------------------------- /IntelliJava/out/production/IntelliJava/com/seal/Arrays.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sabyasachi-Seal/CodingInJava/e747fed9d30fdbfda059ac24c458b23707b40b31/IntelliJava/out/production/IntelliJava/com/seal/Arrays.class -------------------------------------------------------------------------------- /IntelliJava/out/production/IntelliJava/com/seal/Basics.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sabyasachi-Seal/CodingInJava/e747fed9d30fdbfda059ac24c458b23707b40b31/IntelliJava/out/production/IntelliJava/com/seal/Basics.class -------------------------------------------------------------------------------- /IntelliJava/out/production/IntelliJava/com/seal/CelciusToFarenheit.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sabyasachi-Seal/CodingInJava/e747fed9d30fdbfda059ac24c458b23707b40b31/IntelliJava/out/production/IntelliJava/com/seal/CelciusToFarenheit.class -------------------------------------------------------------------------------- /IntelliJava/out/production/IntelliJava/com/seal/Conditionals.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sabyasachi-Seal/CodingInJava/e747fed9d30fdbfda059ac24c458b23707b40b31/IntelliJava/out/production/IntelliJava/com/seal/Conditionals.class -------------------------------------------------------------------------------- /IntelliJava/out/production/IntelliJava/com/seal/HelloWorld.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sabyasachi-Seal/CodingInJava/e747fed9d30fdbfda059ac24c458b23707b40b31/IntelliJava/out/production/IntelliJava/com/seal/HelloWorld.class -------------------------------------------------------------------------------- /IntelliJava/out/production/IntelliJava/com/seal/Loops.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sabyasachi-Seal/CodingInJava/e747fed9d30fdbfda059ac24c458b23707b40b31/IntelliJava/out/production/IntelliJava/com/seal/Loops.class -------------------------------------------------------------------------------- /IntelliJava/out/production/IntelliJava/com/seal/Methods.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sabyasachi-Seal/CodingInJava/e747fed9d30fdbfda059ac24c458b23707b40b31/IntelliJava/out/production/IntelliJava/com/seal/Methods.class -------------------------------------------------------------------------------- /IntelliJava/out/production/IntelliJava/com/seal/ReverseNum.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sabyasachi-Seal/CodingInJava/e747fed9d30fdbfda059ac24c458b23707b40b31/IntelliJava/out/production/IntelliJava/com/seal/ReverseNum.class -------------------------------------------------------------------------------- /IntelliJava/out/production/IntelliJava/com/seal/Sum.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sabyasachi-Seal/CodingInJava/e747fed9d30fdbfda059ac24c458b23707b40b31/IntelliJava/out/production/IntelliJava/com/seal/Sum.class -------------------------------------------------------------------------------- /IntelliJava/out/production/IntelliJava/com/seal/TypeCasting.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sabyasachi-Seal/CodingInJava/e747fed9d30fdbfda059ac24c458b23707b40b31/IntelliJava/out/production/IntelliJava/com/seal/TypeCasting.class -------------------------------------------------------------------------------- /IntelliJava/out/production/IntelliJava/com/seal/VarArgs.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sabyasachi-Seal/CodingInJava/e747fed9d30fdbfda059ac24c458b23707b40b31/IntelliJava/out/production/IntelliJava/com/seal/VarArgs.class -------------------------------------------------------------------------------- /IntelliJava/out/production/IntelliJava/com/seal/arrayformlc.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sabyasachi-Seal/CodingInJava/e747fed9d30fdbfda059ac24c458b23707b40b31/IntelliJava/out/production/IntelliJava/com/seal/arrayformlc.class -------------------------------------------------------------------------------- /IntelliJava/out/production/IntelliJava/com/seal/contatenationarray.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sabyasachi-Seal/CodingInJava/e747fed9d30fdbfda059ac24c458b23707b40b31/IntelliJava/out/production/IntelliJava/com/seal/contatenationarray.class -------------------------------------------------------------------------------- /IntelliJava/out/production/IntelliJava/com/seal/countdigits.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sabyasachi-Seal/CodingInJava/e747fed9d30fdbfda059ac24c458b23707b40b31/IntelliJava/out/production/IntelliJava/com/seal/countdigits.class -------------------------------------------------------------------------------- /IntelliJava/out/production/IntelliJava/com/seal/fibonacci.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sabyasachi-Seal/CodingInJava/e747fed9d30fdbfda059ac24c458b23707b40b31/IntelliJava/out/production/IntelliJava/com/seal/fibonacci.class -------------------------------------------------------------------------------- /IntelliJava/out/production/IntelliJava/com/seal/goodpairs.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sabyasachi-Seal/CodingInJava/e747fed9d30fdbfda059ac24c458b23707b40b31/IntelliJava/out/production/IntelliJava/com/seal/goodpairs.class -------------------------------------------------------------------------------- /IntelliJava/out/production/IntelliJava/com/seal/mostcandies.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sabyasachi-Seal/CodingInJava/e747fed9d30fdbfda059ac24c458b23707b40b31/IntelliJava/out/production/IntelliJava/com/seal/mostcandies.class -------------------------------------------------------------------------------- /IntelliJava/out/production/IntelliJava/com/seal/richest.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sabyasachi-Seal/CodingInJava/e747fed9d30fdbfda059ac24c458b23707b40b31/IntelliJava/out/production/IntelliJava/com/seal/richest.class -------------------------------------------------------------------------------- /IntelliJava/out/production/IntelliJava/com/seal/runningsum.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sabyasachi-Seal/CodingInJava/e747fed9d30fdbfda059ac24c458b23707b40b31/IntelliJava/out/production/IntelliJava/com/seal/runningsum.class -------------------------------------------------------------------------------- /IntelliJava/out/production/IntelliJava/com/seal/shufflearray.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sabyasachi-Seal/CodingInJava/e747fed9d30fdbfda059ac24c458b23707b40b31/IntelliJava/out/production/IntelliJava/com/seal/shufflearray.class -------------------------------------------------------------------------------- /IntelliJava/out/production/IntelliJava/com/seal/switchcase.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sabyasachi-Seal/CodingInJava/e747fed9d30fdbfda059ac24c458b23707b40b31/IntelliJava/out/production/IntelliJava/com/seal/switchcase.class -------------------------------------------------------------------------------- /IntelliJava/src/OOPSPrac/Dog.java: -------------------------------------------------------------------------------- 1 | package OOPSPrac; 2 | 3 | abstract class Animal{ 4 | public void eat(){ 5 | System.out.println("Eating"); 6 | } 7 | public abstract void move(); 8 | void greet(){ 9 | System.out.println("hello"); 10 | } 11 | } 12 | 13 | public class Dog extends Animal{ 14 | public void move() { 15 | System.out.println("Moving"); 16 | } 17 | @Override 18 | protected void greet(){ 19 | super.greet(); 20 | System.out.println("Hello"); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /IntelliJava/src/OOPSPrac2/Chair.java: -------------------------------------------------------------------------------- 1 | package OOPSPrac2; 2 | import OOPSPrac.*; 3 | public class Chair { 4 | public static void main(String[] args) { 5 | Dog dog = new Dog(); 6 | dog.eat(); 7 | } 8 | } 9 | class Table extends Dog{ 10 | public void doggy() { 11 | Dog dog = new Dog(); 12 | dog.eat(); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /IntelliJava/src/com/seal/Arrays.java: -------------------------------------------------------------------------------- 1 | package com.seal; 2 | import java.util.Scanner; 3 | import java.util.ArrayList; 4 | public class Arrays { 5 | public static void main(String[] args){ 6 | Scanner in = new Scanner(System.in); 7 | System.out.println("Enter N"); 8 | int n = in.nextInt(); 9 | int[] arr = new int[n]; 10 | System.out.println("Enter the elements of the array"); 11 | for(int i=0; i newlist = new ArrayList(); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /IntelliJava/src/com/seal/Basics.java: -------------------------------------------------------------------------------- 1 | package com.seal; 2 | 3 | import java.util.Scanner; 4 | 5 | public class Basics { 6 | public static void main(String[] args){ 7 | if(true){ 8 | System.out.println("True Hello World"); 9 | } 10 | if(false){ 11 | System.out.println("False Hello World"); 12 | } 13 | int a = 10; 14 | if(a == 10){ 15 | System.out.println("a is 10"); 16 | } 17 | /*if(a){ 18 | System.out.println("a is true"); // Cannot convert from boolean to int(even with type casting) 19 | }*/ 20 | int count = 1; 21 | while(count != 6) { 22 | //System.out.println(count); 23 | count++; 24 | } 25 | for(int i=1; i!=6; i++){ 26 | System.out.println(i); 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /IntelliJava/src/com/seal/CelciusToFarenheit.java: -------------------------------------------------------------------------------- 1 | package com.seal; 2 | import java.util.Scanner; 3 | 4 | 5 | public class CelciusToFarenheit { 6 | public static void main(String[] args){ 7 | Scanner input = new Scanner (System.in); 8 | System.out.print("Enter the temperature in C: "); 9 | float c_temp = input.nextFloat(); 10 | float f_temp = (c_temp * 9/5) + 32; 11 | System.out.println(); 12 | System.out.print("The temperature in F is: "+f_temp); 13 | } 14 | } -------------------------------------------------------------------------------- /IntelliJava/src/com/seal/Conditionals.java: -------------------------------------------------------------------------------- 1 | package com.seal; 2 | import java.util.Scanner; 3 | public class Conditionals{ 4 | public static void main(String[] args){ 5 | Scanner input = new Scanner(System.in); 6 | System.out.println("Enter the Salary:"); 7 | int salary = input.nextInt(); 8 | if (salary>=10000){ 9 | salary = salary + 1000; 10 | } 11 | else if(salary>=5000){ 12 | salary += 5000; 13 | } 14 | else{ 15 | salary += 10000; 16 | } 17 | System.out.println("The final salary after bonus is: "+salary); 18 | } 19 | } -------------------------------------------------------------------------------- /IntelliJava/src/com/seal/HelloWorld.java: -------------------------------------------------------------------------------- 1 | package com.seal; 2 | 3 | import java.util.Scanner; 4 | 5 | public class HelloWorld { 6 | public static void main(String[] args) { 7 | Scanner input = new Scanner(System.in); 8 | System.out.print("Enter an Integer: "); 9 | int a = input.nextInt(); 10 | System.out.print("The number is "+a); 11 | int b = 234_000_000; 12 | System.out.println(b); 13 | float c = 67.67f; 14 | System.out.println(c); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /IntelliJava/src/com/seal/Loops.java: -------------------------------------------------------------------------------- 1 | package com.seal; 2 | 3 | import java.util.Scanner; 4 | 5 | public class Loops{ 6 | public static void main(String[] args){ 7 | Scanner input = new Scanner(System.in); 8 | System.out.println("Enter the limit: "); 9 | int limit = input.nextInt(); 10 | for(int i=0; i0){ 16 | //System.out.print(num+" "); 17 | num--; 18 | } 19 | do{ 20 | System.out.print(num+" "); 21 | num++; 22 | }while(num<=2*limit); 23 | } 24 | } -------------------------------------------------------------------------------- /IntelliJava/src/com/seal/ReverseNum.java: -------------------------------------------------------------------------------- 1 | package com.seal; 2 | import java.util.Scanner; 3 | public class ReverseNum{ 4 | public static void main(String[] args){ 5 | Scanner input = new Scanner(System.in); 6 | System.out.println("Enter a number to reverse: "); 7 | int number = input.nextInt(); 8 | int reverse = 0; 9 | while(number > 0){ 10 | int last = number % 10; 11 | reverse *= 10; 12 | reverse += last; 13 | number /= 10; 14 | } 15 | System.out.println("The reverse is "+ reverse); 16 | } 17 | } -------------------------------------------------------------------------------- /IntelliJava/src/com/seal/Sum.java: -------------------------------------------------------------------------------- 1 | package com.seal; 2 | import java.util.Scanner; 3 | public class Sum { 4 | public static void main(String[] args){ 5 | Scanner input = new Scanner(System.in); 6 | System.out.println("Enter two integers: "); 7 | int a = input.nextInt(); 8 | int b = input.nextInt(); 9 | int sum = a+b; 10 | System.out.print("The sum is "+ sum); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /IntelliJava/src/com/seal/VarArgs.java: -------------------------------------------------------------------------------- 1 | package com.seal; 2 | import java.util.Arrays; 3 | public class VarArgs { 4 | public static void main(String[] args){ 5 | show(); 6 | show(1,2,3,4,5); 7 | show(1,2,3,4,5,6,7,8,9,10); 8 | } 9 | static void show(int ...nums){ 10 | System.out.println(Arrays.toString(nums)); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /IntelliJava/src/com/seal/arrayformlc.java: -------------------------------------------------------------------------------- 1 | package com.seal; 2 | 3 | // https://leetcode.com/problems/add-to-array-form-of-integer/ 4 | import java.lang.Math; 5 | import java.util.*; 6 | class arrayformlc { 7 | public static List addToArrayForm(int[] num, int k) { 8 | int number = 0; 9 | for(int dig : num){ 10 | number = number*10 + dig; 11 | } 12 | number+=k; 13 | k = 0; 14 | while(number>0){ 15 | num[k] = number/(int)(Math.pow(10, num.length-k-1)); 16 | System.out.println(num[k]); 17 | number %= (int)(Math.pow(10, num.length-k-1)); 18 | k++; 19 | } 20 | List numbers = new ArrayList(); 21 | for(int i:num){ 22 | numbers.add(i); 23 | } 24 | return numbers; 25 | } 26 | public static void main(String[] args) { 27 | int[] nums = {2,1,5}; 28 | int k = 806; 29 | System.out.println(addToArrayForm(nums, k)); 30 | } 31 | } -------------------------------------------------------------------------------- /IntelliJava/src/com/seal/contatenationarray.java: -------------------------------------------------------------------------------- 1 | package com.seal; 2 | 3 | class contatenationarray { 4 | public int[] getConcatenation(int[] nums) { 5 | int n = nums.length * 2; 6 | int i; 7 | int arr[] = new int[n]; 8 | for (i=0; i0){ 13 | int last = num%10; 14 | if(last==digit){ 15 | count++; 16 | } 17 | num = num/10; 18 | } 19 | System.out.println("The number of times "+digit+" is present in "+temp+" is "+count); 20 | } 21 | } -------------------------------------------------------------------------------- /IntelliJava/src/com/seal/fibonacci.java: -------------------------------------------------------------------------------- 1 | package com.seal; 2 | import java.util.Scanner; 3 | public class fibonacci{ 4 | public static void main(String[] args){ 5 | Scanner in = new Scanner(System.in); 6 | System.out.print("Enter the Nth term you need: "); 7 | int limit = in.nextInt(); 8 | int prev = 0, temp; 9 | int curr = 1, count = 1; 10 | System.out.println(); 11 | while(count<=limit/*curr<=limit*/){ 12 | System.out.println("Term "+count+": "+prev); 13 | temp = curr; 14 | curr = prev+curr; 15 | prev = temp; 16 | count++; 17 | } 18 | System.out.println("The "+limit+"th term is: "+prev); 19 | } 20 | } -------------------------------------------------------------------------------- /IntelliJava/src/com/seal/goodpairs.java: -------------------------------------------------------------------------------- 1 | package com.seal; 2 | 3 | class goodpairs { 4 | public int numIdenticalPairs(int[] nums) { 5 | int goodpairs =0; 6 | for(int i=0;i kidsWithCandies(int[] candies, int extraCandies) { 5 | List result = new ArrayList<>(); 6 | int max = 0; 7 | for(int i = 0; i= max){ 12 | result.add(true); 13 | } 14 | } 15 | return result; 16 | } 17 | } -------------------------------------------------------------------------------- /IntelliJava/src/com/seal/richest.java: -------------------------------------------------------------------------------- 1 | package com.seal; 2 | import java.util.*; 3 | class richest { 4 | public int maximumWealth(int[][] accounts) { 5 | int richest = 0; 6 | for(int i=0; i richest){ 12 | richest = wealth; 13 | } 14 | } 15 | return richest; 16 | } 17 | } -------------------------------------------------------------------------------- /IntelliJava/src/com/seal/runningsum.java: -------------------------------------------------------------------------------- 1 | package com.seal; 2 | 3 | class runningsum { 4 | public int[] runningSum(int[] nums) { 5 | int[] arr = new int[nums.length]; 6 | arr[0] = nums[0]; 7 | for(int i=1; i=0 || pb>=0){ 9 | int sum = carry; 10 | if(pa >= 0) sum += a.charAt(pa) - '0'; 11 | if(pb >= 0) sum += b.charAt(pb) - '0'; 12 | sb.append(sum%2); 13 | carry = sum/2; 14 | pa--; 15 | pb--; 16 | } 17 | if(carry != 0) sb.append(carry); 18 | return sb.reverse().toString(); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /Learning&Leetcode/alikestrings.java: -------------------------------------------------------------------------------- 1 | // https://leetcode.com/problems/determine-if-string-halves-are-alike/ 2 | import java.util.*; 3 | public class alikestrings { 4 | public static boolean halvesAreAlike(String str) { 5 | Set st = new HashSet<>(); 6 | for (Character a: "aeiouAEIOU".toCharArray()) { 7 | st.add(a); 8 | } 9 | int h1=0, h2=0; 10 | for (int i = 0; i < str.length()/2; i++) { 11 | if(st.contains(str.charAt(i))){ 12 | h1++; 13 | } 14 | } 15 | for (int i = str.length()/2; i < str.length(); i++) { 16 | if(st.contains(str.charAt(i))){ 17 | h2++; 18 | } 19 | } 20 | return h1==h2; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /Learning&Leetcode/alternatingbits.java: -------------------------------------------------------------------------------- 1 | public class alternatingbits { 2 | public boolean hasAlternatingBits(int n) { 3 | int i = (n&1); 4 | while(n>=0){ 5 | if((n&1) == i){ 6 | i ^= 1; 7 | n >>= 1; 8 | } 9 | else return false; 10 | } 11 | return true; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /Learning&Leetcode/arrangingcoins.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sabyasachi-Seal/CodingInJava/e747fed9d30fdbfda059ac24c458b23707b40b31/Learning&Leetcode/arrangingcoins.class -------------------------------------------------------------------------------- /Learning&Leetcode/arrayform.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sabyasachi-Seal/CodingInJava/e747fed9d30fdbfda059ac24c458b23707b40b31/Learning&Leetcode/arrayform.class -------------------------------------------------------------------------------- /Learning&Leetcode/arrayform.java: -------------------------------------------------------------------------------- 1 | // https://leetcode.com/problems/add-to-array-form-of-integer/ 2 | import java.util.*; 3 | import java.lang.Math; 4 | class arrayform { 5 | public List addToArrayForm(int[] A, int K) { 6 | List res = new LinkedList<>(); 7 | for (int i = A.length - 1; i >= 0; i--) { 8 | res.add(0, (A[i] + K) % 10); 9 | K = (A[i] + K) / 10; 10 | } 11 | while (K > 0) { 12 | res.add(0, K % 10); 13 | K /= 10; 14 | } 15 | return res; 16 | } 17 | public static void main(String[] args) { 18 | int[] nums = {1,2,6,3,0,7,1,7,1,9,7,5,6,6,4,4,0,0,6,3}; 19 | int k = 516; 20 | System.out.println(addToArrayForm(nums, k)); 21 | } 22 | } -------------------------------------------------------------------------------- /Learning&Leetcode/arrayfrompermutation.java: -------------------------------------------------------------------------------- 1 | /*https://leetcode.com/problems/build-array-from-permutation/*/ 2 | 3 | class Solution { 4 | public int[] buildArray(int[] nums) { 5 | int[] ans = new int[nums.length]; 6 | for(int i=0; i=g[c]){ 11 | c++; 12 | } 13 | } 14 | return c; 15 | } 16 | public static void main(String[] args) { 17 | int[] g= {1, 2}; 18 | int[] s={1, 2, 3}; 19 | System.out.println(findContentChildren(g, s)); 20 | } 21 | } -------------------------------------------------------------------------------- /Learning&Leetcode/avgexmaxmin.java: -------------------------------------------------------------------------------- 1 | // https://leetcode.com/problems/average-salary-excluding-the-minimum-and-maximum-salary/ 2 | class Solution { 3 | public double average(int[] salary) { 4 | int max = salary[0]; 5 | int min = salary[0]; 6 | int sum = 0; 7 | for (int i = 0; i < salary.length; i++) { 8 | if(maxsalary[i]){ 12 | min = salary[i]; 13 | } 14 | sum += salary[i]; 15 | } 16 | return (double)(sum-max-min)/(salary.length-2); 17 | } 18 | } -------------------------------------------------------------------------------- /Learning&Leetcode/badversion.java: -------------------------------------------------------------------------------- 1 | //https://leetcode.com/problems/first-bad-version/ 2 | /* The isBadVersion API is defined in the parent class VersionControl. 3 | boolean isBadVersion(int version); */ 4 | 5 | public class Solution extends VersionControl { 6 | public int firstBadVersion(int n) { 7 | int start = 0, end = n, mid = 1; 8 | while(starttarget) return binary(nums, start, mid-1, target); 10 | else return binary(nums, mid+1, end, target); 11 | } 12 | return -1; 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /Learning&Leetcode/binarytreefactors.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sabyasachi-Seal/CodingInJava/e747fed9d30fdbfda059ac24c458b23707b40b31/Learning&Leetcode/binarytreefactors.class -------------------------------------------------------------------------------- /Learning&Leetcode/canjump.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sabyasachi-Seal/CodingInJava/e747fed9d30fdbfda059ac24c458b23707b40b31/Learning&Leetcode/canjump.class -------------------------------------------------------------------------------- /Learning&Leetcode/canjump.java: -------------------------------------------------------------------------------- 1 | // https://leetcode.com/problems/jump-game/ 2 | // https://www.youtube.com/watch?v=Yan0cv2cLy8 3 | public class canjump { 4 | public static boolean canJump(int[] nums) { 5 | int goalpost = nums.length-1; 6 | for (int j = goalpost; j >= 0; j--) { 7 | if(j + nums[j] >= goalpost){ 8 | goalpost = j; 9 | } 10 | if(goalpost == 0){ 11 | return true; 12 | } 13 | } 14 | return false; 15 | } 16 | public static void main(String[] args) { 17 | int[] arr = {3, 2, 1, 0, 4}; 18 | System.out.println(canJump(arr)); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /Learning&Leetcode/checkifsorted.java: -------------------------------------------------------------------------------- 1 | // https://practice.geeksforgeeks.org/problems/check-if-an-array-is-sorted0701/1/ 2 | public class checkifsorted { 3 | boolean arraySortedOrNot(int[] nums, int n) { 4 | if(n<2) return true; 5 | else if (nums[n-1]>=nums[n-2]) return arraySortedOrNot(nums, n-1); 6 | else return false; 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /Learning&Leetcode/climbingstairs.java: -------------------------------------------------------------------------------- 1 | public class climbingstairs { 2 | public int minCostClimbingStairs(int[] cost) { 3 | int[] dp = new int[cost.length]; 4 | return Math.min(mincost(cost, 0, 0, dp), mincost(cost, 1, 0, dp)); 5 | } 6 | public static int mincost(int cost[], int i, int total, int[] dp){ 7 | if(i >= cost.length){ 8 | return total; 9 | } 10 | if(dp[i] != 0) return dp[i]; 11 | dp[i] = cost[i] + Math.min(mincost(cost, i+1, total, dp), mincost(cost, i+2, total, dp)); 12 | return dp[i]; 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /Learning&Leetcode/coloumnumbers.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sabyasachi-Seal/CodingInJava/e747fed9d30fdbfda059ac24c458b23707b40b31/Learning&Leetcode/coloumnumbers.class -------------------------------------------------------------------------------- /Learning&Leetcode/coloumnumbers.java: -------------------------------------------------------------------------------- 1 | // https://leetcode.com/problems/excel-sheet-column-title/ 2 | public class coloumnumbers { 3 | public static String convertToTitle(int col) { 4 | StringBuilder ans = new StringBuilder(); 5 | while(col>0){ 6 | col--; 7 | ans.append((char)(col%26 + 65)); 8 | col /= 26; 9 | } 10 | ans.reverse(); 11 | return ans.toString(); 12 | } 13 | public static void main(String[] args) { 14 | System.out.println(convertToTitle(701)); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /Learning&Leetcode/combinationsum.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sabyasachi-Seal/CodingInJava/e747fed9d30fdbfda059ac24c458b23707b40b31/Learning&Leetcode/combinationsum.class -------------------------------------------------------------------------------- /Learning&Leetcode/combinationsumiv.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sabyasachi-Seal/CodingInJava/e747fed9d30fdbfda059ac24c458b23707b40b31/Learning&Leetcode/combinationsumiv.class -------------------------------------------------------------------------------- /Learning&Leetcode/combinationsumiv.java: -------------------------------------------------------------------------------- 1 | import java.util.Arrays; 2 | 3 | class combinationsumiv{ 4 | public int[] dp; 5 | public int combinationSum4(int[] arr, int target) { 6 | dp = new int[target+1]; 7 | Arrays.fill(dp, -1); 8 | return combi(arr, target); 9 | } 10 | public int combi(int arr[], int target){ 11 | if(dp[target] != -1) return dp[target]; 12 | if(target == 0){ 13 | return 1; 14 | } 15 | int res = 0; 16 | for(int i=0; i target) continue; 18 | res += combi(arr, target-arr[i]); 19 | } 20 | dp[target] = res; 21 | return res; 22 | } 23 | public static void main(String[] args){ 24 | combinationsumiv c = new combinationsumiv(); 25 | int[] arr = {1,2,3}; 26 | int target = 4; 27 | System.out.println(c.combinationSum4(arr, target)); 28 | } 29 | } -------------------------------------------------------------------------------- /Learning&Leetcode/combinationsumprac.java: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sabyasachi-Seal/CodingInJava/e747fed9d30fdbfda059ac24c458b23707b40b31/Learning&Leetcode/combinationsumprac.java -------------------------------------------------------------------------------- /Learning&Leetcode/combinationsumtwo.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sabyasachi-Seal/CodingInJava/e747fed9d30fdbfda059ac24c458b23707b40b31/Learning&Leetcode/combinationsumtwo.class -------------------------------------------------------------------------------- /Learning&Leetcode/complementnum.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sabyasachi-Seal/CodingInJava/e747fed9d30fdbfda059ac24c458b23707b40b31/Learning&Leetcode/complementnum.class -------------------------------------------------------------------------------- /Learning&Leetcode/complementnum.java: -------------------------------------------------------------------------------- 1 | public class complementnum { 2 | public static int findComplement(int num) { 3 | int ans = 0; 4 | int pos = 0; 5 | String number = Integer.toBinaryString(num); 6 | for (int i = number.length()-1; i >= 0; i--) { 7 | ans += ((number.charAt(i)^1)-'0') * Math.pow(2, pos); 8 | pos ++; 9 | } 10 | return ans; 11 | } 12 | public static void main(String[] args) { 13 | System.out.println(findComplement(5)); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /Learning&Leetcode/consistentstrings.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | public class consistentstrings { 3 | public int countConsistentStrings(String allowed, String[] words) { 4 | int ans = words.length; 5 | Set s = new HashSet<>(); 6 | for (int i = 0; i < allowed.length(); i++) { 7 | s.add(allowed.charAt(i)); 8 | } 9 | for (int i = 0; i < words.length; i++) { 10 | for (int j = 0; j < words[i].length(); j++) { 11 | if(!s.contains(words[i].charAt(j))){ 12 | ans--; 13 | break; 14 | } 15 | } 16 | } 17 | return ans; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /Learning&Leetcode/countiterms.java: -------------------------------------------------------------------------------- 1 | import java.util.ArrayList; 2 | 3 | // https://leetcode.com/problems/count-items-matching-a-rule/ 4 | 5 | class Solution { 6 | public int countMatches(List> items, String ruleKey, String ruleValue) { 7 | int i = 0; 8 | for (List item: items){ 9 | if(ruleKey.equals("type") && ruleValue.equals(item.get(0))){ 10 | i++; 11 | } 12 | else if(ruleKey.equals("color") && ruleValue.equals(item.get(1))){ 13 | i++; 14 | } 15 | else if(ruleKey.equals("name") && ruleValue.equals(item.get(2))){ 16 | i++; 17 | } 18 | } 19 | return i; 20 | } 21 | } -------------------------------------------------------------------------------- /Learning&Leetcode/countnegative.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sabyasachi-Seal/CodingInJava/e747fed9d30fdbfda059ac24c458b23707b40b31/Learning&Leetcode/countnegative.class -------------------------------------------------------------------------------- /Learning&Leetcode/cyclesort.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sabyasachi-Seal/CodingInJava/e747fed9d30fdbfda059ac24c458b23707b40b31/Learning&Leetcode/cyclesort.class -------------------------------------------------------------------------------- /Learning&Leetcode/cyclicsort.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sabyasachi-Seal/CodingInJava/e747fed9d30fdbfda059ac24c458b23707b40b31/Learning&Leetcode/cyclicsort.class -------------------------------------------------------------------------------- /Learning&Leetcode/cyclicsort.java: -------------------------------------------------------------------------------- 1 | public class cyclicsort { 2 | public static void main(String[] args) { 3 | int arr[] = {5, 2, 1, 4, 3}; 4 | sort(arr); 5 | for (int i = 0; i < arr.length; i++) { 6 | System.out.println(arr[i]+" "); 7 | } 8 | } 9 | public static void sort(int [] arr){ 10 | int i = 0; 11 | while(i max) max = c; 6 | } 7 | return max-48; 8 | } 9 | } -------------------------------------------------------------------------------- /Learning&Leetcode/decodemessage.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | public class decodemessage { 3 | public String decodeMessage(String key, String message) { 4 | HashMap hm = new HashMap<>(); 5 | char ch = 'a'; 6 | for (int i = 0; i < key.length(); i++) { 7 | if(key.charAt(i) != ' '){ 8 | if(!hm.containsKey(key.charAt(i))){ 9 | hm.put(key.charAt(i), (char)(ch)); 10 | ch++; 11 | } 12 | } 13 | } 14 | StringBuilder sb = new StringBuilder(); 15 | for (int i = 0; i < message.length(); i++) { 16 | if(message.charAt(i) == ' ') sb.append(message.charAt(i)); 17 | else sb.append(hm.get(message.charAt(i))); 18 | } 19 | return sb.toString(); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /Learning&Leetcode/defangingip.java: -------------------------------------------------------------------------------- 1 | // https://leetcode.com/problems/defanging-an-ip-address/ 2 | public class defangingip { 3 | // public String defangIPaddr(String address) { 4 | // address = address.replace(".", "[.]"); 5 | // return address; 6 | // } 7 | public String defangIPaddr(String address){ 8 | StringBuilder ans = new StringBuilder(); 9 | for (int i = 0; i < address.length(); i++) { 10 | if(address.charAt(i)=='.') ans.append("[.]"); 11 | else ans.append(address.charAt(i)); 12 | } 13 | return ans.toString(); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /Learning&Leetcode/deleteopforstrings.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sabyasachi-Seal/CodingInJava/e747fed9d30fdbfda059ac24c458b23707b40b31/Learning&Leetcode/deleteopforstrings.class -------------------------------------------------------------------------------- /Learning&Leetcode/dicetargetsum.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sabyasachi-Seal/CodingInJava/e747fed9d30fdbfda059ac24c458b23707b40b31/Learning&Leetcode/dicetargetsum.class -------------------------------------------------------------------------------- /Learning&Leetcode/dicetargetsum.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | public class dicetargetsum { 3 | public static void main(String[] args) { 4 | System.out.println(dice("", 5, new ArrayList<>())); 5 | } 6 | public static ArrayList dice(String p, int target, ArrayList ans){ 7 | if(target == 0){ 8 | ans.add(p); 9 | return ans; 10 | } 11 | else{ 12 | for (int i = 1; i <= 6 && target>=i; i++) { 13 | dice(p+i, target-i, ans); 14 | } 15 | return ans; 16 | } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /Learning&Leetcode/disappeearednumsprac.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | public class disappeearednumsprac { 3 | public static List findDisappearedNumbers(int[] nums){ 4 | List ans = new ArrayList<>(); 5 | sort(nums); 6 | for (int i = 0; i < nums.length; i++) { 7 | if(i+1 != nums[i]){ 8 | ans.add(i+1); 9 | } 10 | } 11 | return ans; 12 | } 13 | public static void sort(int arr[]){ 14 | int i = 0; 15 | while(i0){ 8 | num/=10; 9 | size++; 10 | } 11 | if((size&1)==0){ 12 | ans++; 13 | } 14 | } 15 | return ans; 16 | } 17 | } -------------------------------------------------------------------------------- /Learning&Leetcode/findallduplicates.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sabyasachi-Seal/CodingInJava/e747fed9d30fdbfda059ac24c458b23707b40b31/Learning&Leetcode/findallduplicates.class -------------------------------------------------------------------------------- /Learning&Leetcode/findandreplacepattern.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | public class findandreplacepattern { 3 | public List findAndReplacePattern(String[] words, String pattern) { 4 | List ans = new ArrayList<>(); 5 | for(String word: words){ 6 | if(match(word, pattern)){ 7 | ans.add(word); 8 | } 9 | } 10 | return ans; 11 | } 12 | private static boolean match(String word, String pattern){ 13 | char[] wrtopat = new char[26]; 14 | char[] patttowr = new char[26]; 15 | for(int i=0; i0 && correct alt){ 9 | alt = gain[i]; 10 | } 11 | } 12 | if (alt arr = new ArrayList(); 5 | Arrays.sort(nums1); 6 | Arrays.sort(nums2); 7 | int n1=0, n2=0; 8 | while(n1 queue = new LinkedList<>(); 9 | queue.add(0); 10 | int farthest = 0; 11 | while(!queue.isEmpty()){ 12 | int curr = queue.remove(); 13 | if(curr == s.length()-1) return true; 14 | for (int i = Math.max(curr+minJump, farthest); i <= Math.min(curr+maxJump, s.length()-1); i++) { 15 | if(s.charAt(i)=='0'){ 16 | queue.add(i); 17 | } 18 | } 19 | farthest = Math.min(curr+maxJump+1, s.length()-1); 20 | } 21 | return false; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /Learning&Leetcode/kinversepairs.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sabyasachi-Seal/CodingInJava/e747fed9d30fdbfda059ac24c458b23707b40b31/Learning&Leetcode/kinversepairs.class -------------------------------------------------------------------------------- /Learning&Leetcode/knights.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sabyasachi-Seal/CodingInJava/e747fed9d30fdbfda059ac24c458b23707b40b31/Learning&Leetcode/knights.class -------------------------------------------------------------------------------- /Learning&Leetcode/kthlargestprac.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sabyasachi-Seal/CodingInJava/e747fed9d30fdbfda059ac24c458b23707b40b31/Learning&Leetcode/kthlargestprac.class -------------------------------------------------------------------------------- /Learning&Leetcode/kthlargestprac.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | public class kthlargestprac { 3 | public static int findKthLargest(int[] nums, int k) { 4 | int max[] = new int[k]; 5 | int maxi = Integer.MIN_VALUE; 6 | for (int i = 0; i < nums.length; i++) { 7 | if(nums[i]>=maxi){ 8 | for (int j = max.length-1; j >= 1; j--) { 9 | max[j] = max[j-1]; 10 | } 11 | max[0] = nums[i]; 12 | maxi = max[0]; 13 | } 14 | } 15 | return nums[k-1]; 16 | } 17 | public static void main(String[] args) { 18 | int[] arr = {2, 4, 1 ,5 ,6 ,2 , 6, 8}; 19 | System.out.println(findKthLargest(arr, 3)); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /Learning&Leetcode/kthmax.java: -------------------------------------------------------------------------------- 1 | // https://leetcode.com/problems/kth-largest-element-in-an-array/ 2 | import java.util.*; 3 | public class kthmax { 4 | public int findKthLargest(int[] nums, int k) { 5 | // ArrayList ans = new ArrayList(); 6 | Arrays.sort(nums); 7 | // ans.add(nums[0]); 8 | // for (int i = 1; i < nums.length; i++) { 9 | // if(nums[i]!=nums[i-1]) ans.add(nums[i]); 10 | // } 11 | return nums[nums.length-k]; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /Learning&Leetcode/largestnumber.java: -------------------------------------------------------------------------------- 1 | import java.util.Arrays; 2 | 3 | // https://leetcode.com/problems/largest-number/ 4 | public class largestnumber { 5 | public String largestNumber(int[] nums) { 6 | // StringBuilder ans = new StringBuilder(); 7 | String[] digs = new String[nums.length]; 8 | for (int i = 0; i < digs.length; i++) { 9 | digs[i] = Integer.toString(nums[i]); 10 | } 11 | Arrays.sort(digs, (s1, s2) -> (s2 + s1).compareTo(s1 + s2)); 12 | return Arrays.stream(digs).reduce((x, y) -> x.equals("0")?y:x+y).get(); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /Learning&Leetcode/largesttriangle.java: -------------------------------------------------------------------------------- 1 | import java.util.Arrays; 2 | 3 | // https://leetcode.com/problems/largest-perimeter-triangle/ 4 | public class largesttriangle { 5 | // public int largestPerimeter(int[] nums) { 6 | // Arrays.sort(nums); 7 | // for(int i=nums.length-1; i>=2; i--){ 8 | // if(nums[i]=0; i--){ 17 | if(nums[i]+nums[i+1]>nums[i+2]){ 18 | return nums[i]+nums[i+1]+nums[i+2]; 19 | } 20 | } 21 | return 0; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /Learning&Leetcode/leftdowntraversal.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sabyasachi-Seal/CodingInJava/e747fed9d30fdbfda059ac24c458b23707b40b31/Learning&Leetcode/leftdowntraversal.class -------------------------------------------------------------------------------- /Learning&Leetcode/leftdowntraversal.java: -------------------------------------------------------------------------------- 1 | public class leftdowntraversal { 2 | public static void main(String[] args) { 3 | int m = 2; 4 | int n = 3; 5 | int arr[][] = new int[m+1][n+1]; 6 | int x = 1; 7 | for (int i = 1; i < arr.length; i++) { 8 | for (int j = 1; j < arr[0].length; j++) { 9 | arr[i][j] = x; 10 | x++; 11 | } 12 | } 13 | int ans = 0; 14 | for (int i = 1; i < arr[0].length; i++) { 15 | System.out.print(arr[1][i]+" "); 16 | ans += arr[1][i]; 17 | } 18 | for (int i = 2; i < arr.length; i++) { 19 | ans += arr[i][arr[0].length-1]; 20 | } 21 | System.out.println(ans); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /Learning&Leetcode/lengthoflastword.java: -------------------------------------------------------------------------------- 1 | public class lengthoflastword { 2 | public int lengthOfLastWord(String s) { 3 | // return s.split(" ")[s.split(" ").length -1].length(); 4 | int i; 5 | int len = 0; 6 | for (i = s.length()-1; i >=0 ; i--) { 7 | if(s.charAt(i) != ' ') len++; 8 | else{ 9 | if(len>0) return len; 10 | } 11 | } 12 | return len; 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /Learning&Leetcode/lettercombinations.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sabyasachi-Seal/CodingInJava/e747fed9d30fdbfda059ac24c458b23707b40b31/Learning&Leetcode/lettercombinations.class -------------------------------------------------------------------------------- /Learning&Leetcode/linkedlistintersection$ListNode.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sabyasachi-Seal/CodingInJava/e747fed9d30fdbfda059ac24c458b23707b40b31/Learning&Leetcode/linkedlistintersection$ListNode.class -------------------------------------------------------------------------------- /Learning&Leetcode/linkedlistintersection.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sabyasachi-Seal/CodingInJava/e747fed9d30fdbfda059ac24c458b23707b40b31/Learning&Leetcode/linkedlistintersection.class -------------------------------------------------------------------------------- /Learning&Leetcode/longestcommonprefix.java: -------------------------------------------------------------------------------- 1 | 2 | import java.util.Arrays; 3 | 4 | public class longestcommonprefix { 5 | public String longestCommonPrefix(String[] strs){ 6 | Arrays.sort(strs); 7 | String str1 = strs[0]; 8 | String str2 = strs[strs.length-1]; 9 | StringBuilder sb = new StringBuilder(); 10 | for (int i = 0; i < ((str1.length() dp = new HashMap<>(); 6 | Arrays.sort(words, (a, b) ->a.length()-b.length()) ; 7 | int res = 0; 8 | for(String word:words){ 9 | int best = 0; 10 | for (int i = 0; i < word.length(); i++) { 11 | String prev = word.substring(0, i) + word.substring(i+1); 12 | best= Math.max(best, dp.getOrDefault(prev, 0)+1); 13 | } 14 | dp.put(word, best); 15 | res = Math.max(res, best); 16 | } 17 | return res; 18 | } 19 | } -------------------------------------------------------------------------------- /Learning&Leetcode/longestsubstringnorepeat.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sabyasachi-Seal/CodingInJava/e747fed9d30fdbfda059ac24c458b23707b40b31/Learning&Leetcode/longestsubstringnorepeat.class -------------------------------------------------------------------------------- /Learning&Leetcode/longestvalidbrackets.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sabyasachi-Seal/CodingInJava/e747fed9d30fdbfda059ac24c458b23707b40b31/Learning&Leetcode/longestvalidbrackets.class -------------------------------------------------------------------------------- /Learning&Leetcode/longpressedchar.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sabyasachi-Seal/CodingInJava/e747fed9d30fdbfda059ac24c458b23707b40b31/Learning&Leetcode/longpressedchar.class -------------------------------------------------------------------------------- /Learning&Leetcode/longpressedchar.java: -------------------------------------------------------------------------------- 1 | // https://leetcode.com/problems/long-pressed-name/ 2 | public class longpressedchar { 3 | public static boolean isLongPressedName(String name, String typed) { 4 | int ori = 0, ty = 0; 5 | while(ty0 && ty maxver){ 12 | maxver = verticalCuts[i+1]-verticalCuts[i]; 13 | } 14 | } 15 | for (int i = 0; i < horizontalCuts.length-1; i++) { 16 | if(horizontalCuts[i+1]-horizontalCuts[i] > maxhei){ 17 | maxhei = horizontalCuts[i+1]-horizontalCuts[i]; 18 | } 19 | } 20 | return (int)((long)maxhei * maxver % 1000000007); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /Learning&Leetcode/maxcandies.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public List kidsWithCandies(int[] candies, int extraCandies) { 3 | List ans = new ArrayList(); 4 | int maxcandies = candies[0]; 5 | int i; 6 | for(i=0; imaxcandies){ 8 | maxcandies = candies[i]; 9 | } 10 | } 11 | for(i = 0; i= maxcandies){ 13 | ans.add(true); 14 | } 15 | else{ 16 | ans.add(false); 17 | } 18 | } 19 | return ans; 20 | } 21 | } -------------------------------------------------------------------------------- /Learning&Leetcode/maxerasurevalue.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sabyasachi-Seal/CodingInJava/e747fed9d30fdbfda059ac24c458b23707b40b31/Learning&Leetcode/maxerasurevalue.class -------------------------------------------------------------------------------- /Learning&Leetcode/maximumgold.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sabyasachi-Seal/CodingInJava/e747fed9d30fdbfda059ac24c458b23707b40b31/Learning&Leetcode/maximumgold.class -------------------------------------------------------------------------------- /Learning&Leetcode/maxislandarea.java: -------------------------------------------------------------------------------- 1 | //https://leetcode.com/problems/max-area-of-island/ 2 | public class maxislandarea { 3 | public int maxAreaOfIsland(int[][] grid) { 4 | int max = 0; 5 | for(int i=0; i < grid.length; i++){ 6 | for(int j=0; j=grid.length||col>=grid[0].length) return area; 16 | 17 | if(grid[row][col] == 0) return area; 18 | 19 | grid[row][col] = 0; 20 | 21 | return 1 + maxarea(grid, row-1, col, area) + 22 | maxarea(grid, row, col-1, area) + 23 | maxarea(grid, row+1, col, area) + 24 | maxarea(grid, row, col+1, area); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /Learning&Leetcode/maxksum.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sabyasachi-Seal/CodingInJava/e747fed9d30fdbfda059ac24c458b23707b40b31/Learning&Leetcode/maxksum.class -------------------------------------------------------------------------------- /Learning&Leetcode/maxpointscards.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sabyasachi-Seal/CodingInJava/e747fed9d30fdbfda059ac24c458b23707b40b31/Learning&Leetcode/maxpointscards.class -------------------------------------------------------------------------------- /Learning&Leetcode/maxpopulation.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sabyasachi-Seal/CodingInJava/e747fed9d30fdbfda059ac24c458b23707b40b31/Learning&Leetcode/maxpopulation.class -------------------------------------------------------------------------------- /Learning&Leetcode/maxproduct2.java: -------------------------------------------------------------------------------- 1 | import java.util.Arrays; 2 | 3 | public class maxproduct2 { 4 | // public int maxProduct(int[] nums) { 5 | // Arrays.sort(nums); 6 | // return nums[nums.length-1]*nums[nums.length-2]; 7 | // } 8 | public int maxProduct(int[] nums){ 9 | int max1 = nums[0]; 10 | int max2 = nums[1]; 11 | for (int i = 1; i < nums.length; i++) { 12 | if(nums[i]>max1){ 13 | max2 = max1; 14 | max1 = nums[i]; 15 | } 16 | else if(nums[i]>max2){ 17 | max2 = nums[i]; 18 | } 19 | } 20 | return (max1-1)*(max2-1); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /Learning&Leetcode/maxprodwordlengths.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sabyasachi-Seal/CodingInJava/e747fed9d30fdbfda059ac24c458b23707b40b31/Learning&Leetcode/maxprodwordlengths.class -------------------------------------------------------------------------------- /Learning&Leetcode/maxrepeatingsubstring.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sabyasachi-Seal/CodingInJava/e747fed9d30fdbfda059ac24c458b23707b40b31/Learning&Leetcode/maxrepeatingsubstring.class -------------------------------------------------------------------------------- /Learning&Leetcode/maxsubarray.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sabyasachi-Seal/CodingInJava/e747fed9d30fdbfda059ac24c458b23707b40b31/Learning&Leetcode/maxsubarray.class -------------------------------------------------------------------------------- /Learning&Leetcode/maxsubarray.java: -------------------------------------------------------------------------------- 1 | public class maxsubarray { 2 | public static int maxSubArray(int[] nums){ 3 | int max = nums[0], sum = 0; 4 | for(int num: nums){ 5 | sum += num; 6 | if(sum>max){ 7 | max = sum; 8 | } 9 | if(sum<0){ 10 | sum = 0; 11 | } 12 | } 13 | return max; 14 | } 15 | public static int max(int a, int b){ 16 | return (a>b)?a:b; 17 | } 18 | public static void main(String[] args) { 19 | int[] arr = {-2}; 20 | System.out.println(maxSubArray(arr)); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /Learning&Leetcode/maxunitsontruck.java: -------------------------------------------------------------------------------- 1 | // https://leetcode.com/problems/maximum-units-on-a-truck/ 2 | import java.util.*; 3 | public class maxunitsontruck { 4 | public int maximumUnits(int[][] box, int ts) { 5 | // sort according to units 6 | Arrays.sort(box, (a, b) -> a[1]-b[1]); 7 | for(int x[]: box) System.out.println(x[0] + " " + x[1]); 8 | int ans = 0; 9 | int i = box.length-1; 10 | while(ts>0 && i>=0){ 11 | if(box[i][0] <= ts){ 12 | ans += box[i][0]*box[i][1]; 13 | ts -= box[i][0]; 14 | } 15 | else if(box[i][0] > ts){ 16 | ans += ts*box[i][1]; 17 | ts -= ts; 18 | } 19 | i--; 20 | } 21 | return ans; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /Learning&Leetcode/mazeproblem.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sabyasachi-Seal/CodingInJava/e747fed9d30fdbfda059ac24c458b23707b40b31/Learning&Leetcode/mazeproblem.class -------------------------------------------------------------------------------- /Learning&Leetcode/mergeintervals.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | // https://leetcode.com/problems/merge-intervals/ 3 | public class mergeintervals { 4 | public int[][] merge(int[][] intervals) { 5 | Arrays.sort(intervals, (i1, i2) -> Integer.compare(i1[0], i2[0])); 6 | List ans = new ArrayList<>(); 7 | int[] currint = intervals[0]; 8 | ans.add(currint); 9 | for (int[] inter : intervals) { 10 | if(currint[1] >= inter[0]){ 11 | currint[1] = Math.max(currint[1], inter[1]); 12 | } 13 | else{ 14 | currint = inter; 15 | ans.add(currint); 16 | } 17 | } 18 | return ans.toArray(new int[ans.size()][]); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /Learning&Leetcode/mergesortedarray.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sabyasachi-Seal/CodingInJava/e747fed9d30fdbfda059ac24c458b23707b40b31/Learning&Leetcode/mergesortedarray.class -------------------------------------------------------------------------------- /Learning&Leetcode/mergesortedarrayprac.java: -------------------------------------------------------------------------------- 1 | public class mergesortedarrayprac { 2 | public static void merge(int[] nums1, int m, int[] nums2, int n){ 3 | int end = m+n-1; 4 | while(m>0 && n>0){ 5 | if(nums1[m-1] > nums2[n-1]){ 6 | nums1[end] = nums1[m-1]; 7 | m--; 8 | } 9 | else{ 10 | nums1[end] = nums2[n-1]; 11 | n--; 12 | } 13 | end--; 14 | } 15 | while(n>0){ 16 | nums1[end] = nums1[n-1]; 17 | n--; 18 | end--; 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /Learning&Leetcode/mergestringsalternatively.java: -------------------------------------------------------------------------------- 1 | // https://leetcode.com/problems/merge-strings-alternately/ 2 | public class mergestringsalternatively { 3 | public String mergeAlternately(String word1, String word2) { 4 | int lindex = 0; 5 | int rindex = 0; 6 | StringBuilder ans = new StringBuilder(); 7 | while(lindex=k){ 9 | end = mid; 10 | } 11 | else{ 12 | start = mid +1; 13 | } 14 | } 15 | return start+k; 16 | } 17 | public static void main(String[] args) { 18 | int[] arr = {1, 2, 3, 4}; 19 | int k = 5; 20 | System.out.println(findKthPositive(arr, k)); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /Learning&Leetcode/missingnumcyclic.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sabyasachi-Seal/CodingInJava/e747fed9d30fdbfda059ac24c458b23707b40b31/Learning&Leetcode/missingnumcyclic.class -------------------------------------------------------------------------------- /Learning&Leetcode/movechips.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sabyasachi-Seal/CodingInJava/e747fed9d30fdbfda059ac24c458b23707b40b31/Learning&Leetcode/movechips.class -------------------------------------------------------------------------------- /Learning&Leetcode/movechips.java: -------------------------------------------------------------------------------- 1 | import javax.xml.transform.Source; 2 | 3 | // https://leetcode.com/problems/minimum-cost-to-move-chips-to-the-same-position/ 4 | public class movechips { 5 | public static int minCostToMoveChips(int[] position) { 6 | int odd = 0; 7 | for(int num: position){ 8 | if((num&1)==1){ 9 | odd++; 10 | } 11 | } 12 | if(position.length-odd==0){ 13 | return 0; 14 | } 15 | return (odd>position.length-odd)?odd:position.length-odd; 16 | } 17 | public static void main(String[] args) { 18 | int[] arr = {1, 1, 1}; 19 | System.out.println(minCostToMoveChips(arr)); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /Learning&Leetcode/mssingnum.java: -------------------------------------------------------------------------------- 1 | public class mssingnum { 2 | // public int missingNumber(int[] nums) { 3 | // int sum = 0; 4 | // for (int i : nums) { 5 | // sum += i; 6 | // } 7 | // return ((nums.length)*(nums.length+1)/2) - sum; 8 | // } 9 | public int missingNumber(int[] nums) { 10 | int res = nums.length; 11 | for (int i = 0; i < nums.length; i++) { 12 | res = res ^ i; 13 | res = res ^ nums[i]; 14 | } 15 | return res; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /Learning&Leetcode/needlenhaystack.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public static int strStr(String haystack, String needle) { 3 | return issubstring(haystack, needle); 4 | } 5 | public static int issubstring(String word, String s){ 6 | // int l = -1; 7 | // int r = 0; 8 | // while(r= nums[i-1]) nums[i] = nums[i+1]; 11 | else nums[i+1] = nums[i]; 12 | tolerate = true; 13 | } 14 | return true; 15 | } 16 | public static void main(String[] args) { 17 | System.out.println(checkPossibility(new int[]{3, 2, 3})); 18 | } 19 | } -------------------------------------------------------------------------------- /Learning&Leetcode/nqueens.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sabyasachi-Seal/CodingInJava/e747fed9d30fdbfda059ac24c458b23707b40b31/Learning&Leetcode/nqueens.class -------------------------------------------------------------------------------- /Learning&Leetcode/nqueens2.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sabyasachi-Seal/CodingInJava/e747fed9d30fdbfda059ac24c458b23707b40b31/Learning&Leetcode/nqueens2.class -------------------------------------------------------------------------------- /Learning&Leetcode/nqueensprac.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sabyasachi-Seal/CodingInJava/e747fed9d30fdbfda059ac24c458b23707b40b31/Learning&Leetcode/nqueensprac.class -------------------------------------------------------------------------------- /Learning&Leetcode/nqueensprac2.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sabyasachi-Seal/CodingInJava/e747fed9d30fdbfda059ac24c458b23707b40b31/Learning&Leetcode/nqueensprac2.class -------------------------------------------------------------------------------- /Learning&Leetcode/oddmatrix.java: -------------------------------------------------------------------------------- 1 | // https://leetcode.com/problems/cells-with-odd-values-in-a-matrix/ 2 | 3 | class Solution { 4 | public int oddCells(int m, int n, int[][] indices) { 5 | int[][] ans = new int[m][n]; 6 | int i, j; 7 | for(i=0; iarr[mid+1]){ 8 | end = mid; 9 | } 10 | else if(arr[mid]0){ 8 | for (int i = 0; i < n-1; i++) { 9 | x = x * temp; 10 | } 11 | } 12 | else{ 13 | for (int i = 0; i < Math.abs(n)+1; i++) { 14 | x = x / temp; 15 | } 16 | } 17 | return x; 18 | } 19 | public static void main(String[] args) { 20 | System.out.println(myPow(2.0, -2)); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /Learning&Leetcode/prime.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sabyasachi-Seal/CodingInJava/e747fed9d30fdbfda059ac24c458b23707b40b31/Learning&Leetcode/prime.class -------------------------------------------------------------------------------- /Learning&Leetcode/prime.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | public class prime { 3 | public static void main(String[] args){ 4 | Scanner in = new Scanner(System.in); 5 | int n = in.nextInt(); 6 | System.out.println(isprime(n)); 7 | } 8 | static boolean isprime(int n){ 9 | if(n>2){ 10 | int count = 2; 11 | while(count*count<=n){ 12 | if(n%count==0){ 13 | return false; 14 | } 15 | count++; 16 | } 17 | return count*count>n; 18 | } 19 | return false; 20 | } 21 | } 22 | 23 | -------------------------------------------------------------------------------- /Learning&Leetcode/primesetbits.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sabyasachi-Seal/CodingInJava/e747fed9d30fdbfda059ac24c458b23707b40b31/Learning&Leetcode/primesetbits.class -------------------------------------------------------------------------------- /Learning&Leetcode/primesetbits.java: -------------------------------------------------------------------------------- 1 | 2 | public class primesetbits { 3 | public int countPrimeSetBits(int left, int right) { 4 | int ans = 0; 5 | for (int i = left; i <= right; i++) { 6 | int t = getset(i); 7 | if(t==2||t==3||t==5||t==7||t==11||t==13||t==17||t==19) ans ++; 8 | } 9 | return ans; 10 | } 11 | public int getset(int n){ 12 | int ans = 0; 13 | while(n>0){ 14 | ans += (n&1); 15 | n >>>= 1; 16 | } 17 | return ans; 18 | } 19 | public static void main(String[] args) { 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /Learning&Leetcode/ranktransform.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sabyasachi-Seal/CodingInJava/e747fed9d30fdbfda059ac24c458b23707b40b31/Learning&Leetcode/ranktransform.class -------------------------------------------------------------------------------- /Learning&Leetcode/reachanumber.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sabyasachi-Seal/CodingInJava/e747fed9d30fdbfda059ac24c458b23707b40b31/Learning&Leetcode/reachanumber.class -------------------------------------------------------------------------------- /Learning&Leetcode/reachanumber.java: -------------------------------------------------------------------------------- 1 | // https://leetcode.com/problems/reach-a-number/submissions/ 2 | public class reachanumber { 3 | public static int reachNumber(int target) { 4 | // target = Math.abs(target); 5 | // int sum =0, count=0; 6 | // while(sum (a[0]==b[0])?a[1]-b[1]:b[0]-a[0]); 5 | List ans = new ArrayList<>(); 6 | for(int[] i:people){ 7 | ans.add(i[1], i); 8 | } 9 | return ans.toArray(new int[people.length][]); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /Learning&Leetcode/recursivebubble.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sabyasachi-Seal/CodingInJava/e747fed9d30fdbfda059ac24c458b23707b40b31/Learning&Leetcode/recursivebubble.class -------------------------------------------------------------------------------- /Learning&Leetcode/recursivebubble.java: -------------------------------------------------------------------------------- 1 | import java.util.Arrays; 2 | public class recursivebubble { 3 | public static void main(String[] args) { 4 | int []arr = new int[]{2, 5, 1, 2, 4, 1, 6, 9}; 5 | recursivebubble o = new recursivebubble(); 6 | o.bubble(0, 0, arr); 7 | System.out.println(Arrays.toString(arr)); 8 | } 9 | private void bubble(int i, int j, int []arr){ 10 | if(i == arr.length){ 11 | return; 12 | } 13 | if(j < arr.length-1){ 14 | if(arr[j] > arr[j+1]){ 15 | arr[j] = arr[j] ^ arr[j+1]; 16 | arr[j+1] = arr[j] ^ arr[j+1]; 17 | arr[j] = arr[j] ^ arr[j+1]; 18 | } 19 | bubble(i, j+1, arr); 20 | } 21 | else{ 22 | bubble(i+1, 0, arr); 23 | } 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /Learning&Leetcode/recursivestrongs.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sabyasachi-Seal/CodingInJava/e747fed9d30fdbfda059ac24c458b23707b40b31/Learning&Leetcode/recursivestrongs.class -------------------------------------------------------------------------------- /Learning&Leetcode/recursivestrongs.java: -------------------------------------------------------------------------------- 1 | 2 | import java.util.*; 3 | public class recursivestrongs { 4 | public static void main(String[] args) { 5 | ArrayList ans = noa("", "abba", new ArrayList<>()); 6 | for (int i = 0; i < ans.size(); i++) { 7 | System.out.println(ans.get(i)); 8 | } 9 | } 10 | public static ArrayList noa(String n, String ori, ArrayList ans){ 11 | if(ori.isEmpty()){ 12 | if(!n.isEmpty() && !ans.contains(n))ans.add(n); 13 | return ans; 14 | } 15 | else{ 16 | noa(n, ori.substring(1), ans); 17 | noa(n+ori.charAt(0), ori.substring(1), ans); 18 | return ans; 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /Learning&Leetcode/reducetozero.java: -------------------------------------------------------------------------------- 1 | // https://leetcode.com/problems/number-of-steps-to-reduce-a-number-to-zero/ 2 | public class reducetozero { 3 | public int numberOfSteps(int num) { 4 | int steps = 0; 5 | while(num>0){ 6 | if((num&1)==1){ 7 | num--; 8 | } 9 | else{ 10 | num /= 2; 11 | } 12 | steps++; 13 | } 14 | return steps; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /Learning&Leetcode/reducextozero.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sabyasachi-Seal/CodingInJava/e747fed9d30fdbfda059ac24c458b23707b40b31/Learning&Leetcode/reducextozero.class -------------------------------------------------------------------------------- /Learning&Leetcode/reducextozero.java: -------------------------------------------------------------------------------- 1 | class reducextozero { 2 | public static int minOperations(int[] nums, int x) { 3 | int sum = 0; 4 | for(int n:nums) sum+=n; 5 | int max = -1, currsum =0; 6 | int l = 0, r = 0; 7 | while(rsum-x){ 10 | currsum -= nums[l]; 11 | l++; 12 | } 13 | if(currsum==sum-x) max = Math.max(max, r-l+1); 14 | r++; 15 | } 16 | return max==-1?-1:nums.length-max; 17 | } 18 | public static void main(String[] args) { 19 | // int a[] = {3, 2, 20, 1 ,1, 3}; 20 | int a[] = {6016,5483,541,4325,8149,3515,7865,2209,9623,9763,4052,6540,2123,2074,765,7520,4941,5290,5868,6150,6006,6077,2856,7826,9119}; 21 | int target = 31841; 22 | System.out.println(minOperations(a, target)); 23 | } 24 | } -------------------------------------------------------------------------------- /Learning&Leetcode/relativesort.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sabyasachi-Seal/CodingInJava/e747fed9d30fdbfda059ac24c458b23707b40b31/Learning&Leetcode/relativesort.class -------------------------------------------------------------------------------- /Learning&Leetcode/removeduplicates.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sabyasachi-Seal/CodingInJava/e747fed9d30fdbfda059ac24c458b23707b40b31/Learning&Leetcode/removeduplicates.class -------------------------------------------------------------------------------- /Learning&Leetcode/removepalindromesubs.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sabyasachi-Seal/CodingInJava/e747fed9d30fdbfda059ac24c458b23707b40b31/Learning&Leetcode/removepalindromesubs.class -------------------------------------------------------------------------------- /Learning&Leetcode/removepalindromesubs.java: -------------------------------------------------------------------------------- 1 | // https://leetcode.com/problems/remove-palindromic-subsequences/ 2 | public class removepalindromesubs { 3 | public static int removePalindromeSub(String s) { 4 | if(s.length()==0) return 0; 5 | else if(ispalindrome(s)) return 1; 6 | else return 2; 7 | } 8 | public static boolean ispalindrome(String str){ 9 | int front = 0; 10 | int back = str.length()-1; 11 | while(front<=back){ 12 | if(str.charAt(front)==str.charAt(back)){ 13 | front++; 14 | back--; 15 | } 16 | else{ 17 | return false; 18 | } 19 | } 20 | return true; 21 | } 22 | public static void main(String[] args) { 23 | String s = "bbaabb"; 24 | System.out.println(removePalindromeSub(s)); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /Learning&Leetcode/reshapearray.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sabyasachi-Seal/CodingInJava/e747fed9d30fdbfda059ac24c458b23707b40b31/Learning&Leetcode/reshapearray.class -------------------------------------------------------------------------------- /Learning&Leetcode/reshapearray.java: -------------------------------------------------------------------------------- 1 | public class reshapearray { 2 | public static int[][] matrixReshape(int[][] mat, int r, int c) { 3 | if((mat.length*mat[0].length) == r*c){ 4 | int[][] ans = new int[r][c]; 5 | r = mat[0].length; 6 | for(int i=0;i<(mat.length*mat[0].length);i++){ 7 | ans[i/c][i%c] = mat[i/r][i%r]; 8 | } 9 | return ans; 10 | } 11 | return mat; 12 | } 13 | public static void main(String[] args) { 14 | int[][]arr = {{1,2},{3,4}}; 15 | int r = 1; 16 | int c = 4; 17 | int[][] ans = matrixReshape(arr, r, c); 18 | for (int i = 0; i < ans.length; i++) { 19 | for (int j = 0; j < ans[0].length; j++) { 20 | System.out.println(ans[i][j]); 21 | } 22 | } 23 | } 24 | } 25 | 26 | -------------------------------------------------------------------------------- /Learning&Leetcode/reversebits.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sabyasachi-Seal/CodingInJava/e747fed9d30fdbfda059ac24c458b23707b40b31/Learning&Leetcode/reversebits.class -------------------------------------------------------------------------------- /Learning&Leetcode/reversebits.java: -------------------------------------------------------------------------------- 1 | class reversebits{ 2 | // you need treat n as an unsigned value 3 | public static int reverseBits(int n) { 4 | int result = 0; 5 | for (int i = 0; i < 32; i++) { 6 | int end = n&1; 7 | n = n>>1; 8 | result = result<<1; 9 | result = result | end; 10 | } 11 | return result; 12 | } 13 | } -------------------------------------------------------------------------------- /Learning&Leetcode/reverseprefix.java: -------------------------------------------------------------------------------- 1 | // https://leetcode.com/problems/reverse-prefix-of-word/ 2 | public class reverseprefix { 3 | public static String reversePrefix(String word, char ch) { 4 | int ind = searchit(word, ch); 5 | if(ind > -1){ 6 | StringBuilder ans = new StringBuilder(); 7 | ans.append(word.substring(0, ind)); 8 | ans.reverse(); 9 | ans.append(word.substring(ind, word.length())); 10 | return ans.toString(); 11 | } 12 | return word; 13 | } 14 | public static int searchit(String s, char c){ 15 | for (int i = 0; i < s.length(); i++) { 16 | if(s.charAt(i) == c){ 17 | return i+1; 18 | } 19 | } 20 | return -1; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /Learning&Leetcode/reversestring.java: -------------------------------------------------------------------------------- 1 | public class reversestring { 2 | public static void reverseString(char[] s) { 3 | reverse(s, 0, s.length-1); 4 | } 5 | public static void swap(char[] arr, int a, int b){ 6 | char temp = arr[a]; 7 | arr[a] = arr[b]; 8 | arr[b] = temp; 9 | } 10 | public static void reverse(char[] arr, int i, int j){ 11 | if(i<=j){ 12 | swap(arr, i, j); 13 | reverse(arr, ++i, --j); 14 | } 15 | } 16 | 17 | } 18 | -------------------------------------------------------------------------------- /Learning&Leetcode/reversewords.java: -------------------------------------------------------------------------------- 1 | // https://leetcode.com/problems/reverse-words-in-a-string-iii/ 2 | public class reversewords { 3 | public String reverseWords(String s) { 4 | String[] words = s.split(" "); 5 | for (int i = 0; i < words.length; i++) { 6 | words[i] = reverse(words[i]); 7 | } 8 | return String.join(" ", words); 9 | } 10 | public static String reverse(String s){ 11 | StringBuilder temp = new StringBuilder(); 12 | for (int i = s.length()-1; i >=0; i--) { 13 | temp.append(s.charAt(i)); 14 | } 15 | return temp.toString(); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /Learning&Leetcode/richestcustomer.java: -------------------------------------------------------------------------------- 1 | /* https://leetcode.com/problems/richest-customer-wealth/ */ 2 | class Solution { 3 | public int maximumWealth(int[][] accounts) { 4 | int richest=0, current; 5 | for(int i = 0; i richest){ 11 | richest = current; 12 | } 13 | } 14 | return richest; 15 | } 16 | } -------------------------------------------------------------------------------- /Learning&Leetcode/rightinterval.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sabyasachi-Seal/CodingInJava/e747fed9d30fdbfda059ac24c458b23707b40b31/Learning&Leetcode/rightinterval.class -------------------------------------------------------------------------------- /Learning&Leetcode/robotorigin.java: -------------------------------------------------------------------------------- 1 | // https://leetcode.com/problems/robot-return-to-origin/ 2 | public class robotorigin { 3 | public boolean judgeCircle(String moves) { 4 | int x = 0; 5 | int y = 0; 6 | for(char c : moves.toCharArray()){ 7 | if(c=='U') x++; 8 | else if(c=='D') x--; 9 | else if(c=='L') y++; 10 | else if(c=='R') y--; 11 | } 12 | return (x==0 && y==0); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /Learning&Leetcode/romantoint.java: -------------------------------------------------------------------------------- 1 | import java.util.HashMap; 2 | 3 | // https://leetcode.com/problems/roman-to-integer/ 4 | public class romantoint { 5 | public int romanToInt(String s) { 6 | HashMap nums = new HashMap<>(); 7 | nums.put('I', 1); 8 | nums.put('V', 5); 9 | nums.put('X', 10); 10 | nums.put('L', 50); 11 | nums.put('C', 100); 12 | nums.put('D', 500); 13 | nums.put('M', 1000); 14 | int sum = 0; 15 | for (int i = 0; i < s.length()-1; i++) { 16 | if(nums.get(s.charAt(i)) < nums.get(s.charAt(i+1))){ 17 | sum -= nums.get(s.charAt(i)); 18 | } 19 | else{ 20 | sum += nums.get(s.charAt(i)); 21 | } 22 | } 23 | sum += nums.get(s.charAt(s.length()-1)); 24 | return sum; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /Learning&Leetcode/rotatearray.java: -------------------------------------------------------------------------------- 1 | // https://leetcode.com/problems/rotate-array/ 2 | public class rotatearray { 3 | public void rotate(int[] nums, int k) { 4 | int[] ans = new int[nums.length]; 5 | for (int i = 0; i < ans.length; i++) { 6 | ans[i] = nums[i]; 7 | } 8 | for (int i = 0; i < ans.length; i++) { 9 | nums[(i+k)%nums.length] = ans[i]; 10 | } 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /Learning&Leetcode/rotatedarray.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sabyasachi-Seal/CodingInJava/e747fed9d30fdbfda059ac24c458b23707b40b31/Learning&Leetcode/rotatedarray.class -------------------------------------------------------------------------------- /Learning&Leetcode/runningsumprac.java: -------------------------------------------------------------------------------- 1 | class runningsumprac{ 2 | public int[] runningSum(int[] nums){ 3 | for (int i = 1; i < nums.length; i++) { 4 | nums[i] = nums[i] + nums[i-1]; 5 | } 6 | return nums; 7 | } 8 | } -------------------------------------------------------------------------------- /Learning&Leetcode/secretmessage.java: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sabyasachi-Seal/CodingInJava/e747fed9d30fdbfda059ac24c458b23707b40b31/Learning&Leetcode/secretmessage.java -------------------------------------------------------------------------------- /Learning&Leetcode/segmentofones.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sabyasachi-Seal/CodingInJava/e747fed9d30fdbfda059ac24c458b23707b40b31/Learning&Leetcode/segmentofones.class -------------------------------------------------------------------------------- /Learning&Leetcode/segmentofones.java: -------------------------------------------------------------------------------- 1 | // https://leetcode.com/problems/check-if-binary-string-has-at-most-one-segment-of-ones/ 2 | public class segmentofones { 3 | public static boolean checkOnesSegment(String s) { 4 | int i = 0; 5 | boolean foundzero = false; 6 | while(i Character.compare(i1[0], i2[0])); 12 | HashMap hm = new HashMap<>(); 13 | for (int i = 0; i < tri.length; i++) { 14 | if(!hm.containsKey(tri[i][0])) hm.put(tri[i][0], tri[i][1]); 15 | else{ 16 | count ++; 17 | } 18 | } 19 | return 5; 20 | } 21 | public static void main(String[] args){ 22 | char[][] t = new char[][]{{'A','B'}, {'B','D'}, {'D','E'}, {'A', 'F'}}; 23 | System.out.println(triangle(t, 'F')); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /Learning&Leetcode/singleelement.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sabyasachi-Seal/CodingInJava/e747fed9d30fdbfda059ac24c458b23707b40b31/Learning&Leetcode/singleelement.class -------------------------------------------------------------------------------- /Learning&Leetcode/singleelement.java: -------------------------------------------------------------------------------- 1 | // https://leetcode.com/problems/single-element-in-a-sorted-array/ 2 | public class singleelement { 3 | public static int singleNonDuplicate(int[] arr) { 4 | int mid; 5 | int start = 0, end = arr.length-1; 6 | while(startnums[j] && i!=j){ 10 | count++; 11 | } 12 | } 13 | arr[i] = count; 14 | } 15 | return arr; 16 | } 17 | } -------------------------------------------------------------------------------- /Learning&Leetcode/smallestletter.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sabyasachi-Seal/CodingInJava/e747fed9d30fdbfda059ac24c458b23707b40b31/Learning&Leetcode/smallestletter.class -------------------------------------------------------------------------------- /Learning&Leetcode/smallestletter.java: -------------------------------------------------------------------------------- 1 | // https://leetcode.com/problems/find-smallest-letter-greater-than-target/ 2 | public class smallestletter { 3 | public static char nextGreatestLetter(char[] letters, char target) { 4 | int start = 0, end=letters.length-1, mid=1; 5 | while(start<=end){ 6 | mid = start + (end-start)/2; 7 | if(letters[mid]==target){ 8 | start = mid + 1; 9 | } 10 | else if(letters[mid] target){ 24 | end = mid-1; 25 | } 26 | } 27 | return index; 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /Learning&Leetcode/sortedsquares.java: -------------------------------------------------------------------------------- 1 | import java.util.Arrays; 2 | 3 | // https://leetcode.com/problems/squares-of-a-sorted-array/ 4 | public class sortedsquares { 5 | // public int[] sortedSquares(int[] nums) { 6 | // for (int i = 0; i < nums.length; i++) { 7 | // nums[i] = nums[i]*nums[i]; 8 | // } 9 | // Arrays.sort(nums); 10 | // return nums; 11 | // } 12 | public int[] sortedSquares(int[] nums){ 13 | int[] ans = new int[nums.length]; 14 | int left = 0; 15 | int right = nums.length-1; 16 | for (int i = ans.length-1; i >= 0; i--) { 17 | if(Math.abs(nums[left])>Math.abs(nums[right])){ 18 | ans[i] = nums[left] * nums[left]; 19 | left++; 20 | } 21 | else{ 22 | ans[i] = nums[right] * nums[right]; 23 | right--; 24 | } 25 | } 26 | return ans; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /Learning&Leetcode/sortsentence.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sabyasachi-Seal/CodingInJava/e747fed9d30fdbfda059ac24c458b23707b40b31/Learning&Leetcode/sortsentence.class -------------------------------------------------------------------------------- /Learning&Leetcode/sortsentence.java: -------------------------------------------------------------------------------- 1 | import java.util.ArrayList; 2 | 3 | // https://leetcode.com/problems/sorting-the-sentence/ 4 | import java.util.*; 5 | public class sortsentence { 6 | public static String sortSentence(String s) { 7 | String[] ans = s.split(" "); 8 | String[] copy = s.split(" "); 9 | for(String str: copy){ 10 | ans[str.charAt(str.length()-1)-1-48] = str.substring(0, str.length()-1); 11 | } 12 | return Arrays.toString(ans).join(" ", ans); 13 | } 14 | public static void main(String[] args) { 15 | System.out.println(sortSentence("is2 sentence4 This1 a3")); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /Learning&Leetcode/specialarray.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sabyasachi-Seal/CodingInJava/e747fed9d30fdbfda059ac24c458b23707b40b31/Learning&Leetcode/specialarray.class -------------------------------------------------------------------------------- /Learning&Leetcode/specialarrayx.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sabyasachi-Seal/CodingInJava/e747fed9d30fdbfda059ac24c458b23707b40b31/Learning&Leetcode/specialarrayx.class -------------------------------------------------------------------------------- /Learning&Leetcode/specialarrayx.java: -------------------------------------------------------------------------------- 1 | // https://leetcode.com/problems/special-array-with-x-elements-greater-than-or-equal-x/ 2 | import java.util.*; 3 | public class specialarrayx { 4 | public static int specialArray(int[] nums){ 5 | Arrays.sort(nums); 6 | for (int i=1,p=0; i<=nums[nums.length-1];) { 7 | if(p=i){ 8 | if(i==nums.length-p){ 9 | return i; 10 | } 11 | else{ 12 | i++; 13 | } 14 | } 15 | else{ 16 | p++; 17 | } 18 | } 19 | return -1; 20 | } 21 | public static void main(String[] args) { 22 | int[] arr= {0,4,3,0,4}; 23 | System.out.println(specialArray(arr)); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /Learning&Leetcode/specialcyclicsort.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sabyasachi-Seal/CodingInJava/e747fed9d30fdbfda059ac24c458b23707b40b31/Learning&Leetcode/specialcyclicsort.class -------------------------------------------------------------------------------- /Learning&Leetcode/specialcyclicsort.java: -------------------------------------------------------------------------------- 1 | public class specialcyclicsort { 2 | public static int sort(int nums[]){ 3 | int i = 0; 4 | while(i0 && nums[i] != nums[correct]){ 7 | swap(nums, i, correct); 8 | } 9 | else{ 10 | i++; 11 | } 12 | } 13 | for (i = 1; i <= nums.length; i++) { 14 | if(nums[i-1] != i){ 15 | return i; 16 | } 17 | } 18 | return nums.length+1; 19 | } 20 | public static void swap(int arr[], int a, int b){ 21 | arr[a] = arr[a] ^ arr[b]; 22 | arr[b] = arr[a] ^ arr[b]; 23 | arr[a] = arr[a] ^ arr[b]; 24 | } 25 | public static void main(String[] args) { 26 | int[] arr = {3,4,-1,1}; 27 | sort(arr); 28 | for (int i = 0; i < arr.length; i++) { 29 | System.out.println(arr[i] + " "); 30 | } 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /Learning&Leetcode/spiralmatrix.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sabyasachi-Seal/CodingInJava/e747fed9d30fdbfda059ac24c458b23707b40b31/Learning&Leetcode/spiralmatrix.class -------------------------------------------------------------------------------- /Learning&Leetcode/spiralmatrix2.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sabyasachi-Seal/CodingInJava/e747fed9d30fdbfda059ac24c458b23707b40b31/Learning&Leetcode/spiralmatrix2.class -------------------------------------------------------------------------------- /Learning&Leetcode/splitstringspalindrome.java: -------------------------------------------------------------------------------- 1 | public class splitstringspalindrome { 2 | public static boolean checkPalindromeFormation(String a, String b) { 3 | return check(a, b) || check (b, a); 4 | } 5 | public static boolean check(String a, String b){ 6 | int left = 0; 7 | int right = a.length()-1; 8 | while(left < right){ 9 | if(a.charAt(left) != b.charAt(right)){ 10 | return ispalindrome(a, left, right) || ispalindrome(b, left, right); 11 | } 12 | left++; 13 | right--; 14 | } 15 | return true; 16 | } 17 | public static boolean ispalindrome(String s, int start, int end){ 18 | while(start0){ 9 | System.out.print(arr[k2] + " "); 10 | } 11 | } 12 | System.out.print("\n"); 13 | } 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /Learning&Leetcode/substrings.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sabyasachi-Seal/CodingInJava/e747fed9d30fdbfda059ac24c458b23707b40b31/Learning&Leetcode/substrings.class -------------------------------------------------------------------------------- /Learning&Leetcode/substrings.java: -------------------------------------------------------------------------------- 1 | class substrings{ 2 | public static void main(String[] args) { 3 | substring("abcd"); 4 | } 5 | public static String substring(String s){ 6 | if(s.length()<2){ 7 | return ""; 8 | } 9 | System.out.println(s); 10 | for (int i = 0; i < s.length(); i++) { 11 | substring(s.substring(0, i)); 12 | 13 | substring(s.substring(i+1, s.length())); 14 | } 15 | return s; 16 | } 17 | } -------------------------------------------------------------------------------- /Learning&Leetcode/substringsinstring.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sabyasachi-Seal/CodingInJava/e747fed9d30fdbfda059ac24c458b23707b40b31/Learning&Leetcode/substringsinstring.class -------------------------------------------------------------------------------- /Learning&Leetcode/substringstemp.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sabyasachi-Seal/CodingInJava/e747fed9d30fdbfda059ac24c458b23707b40b31/Learning&Leetcode/substringstemp.class -------------------------------------------------------------------------------- /Learning&Leetcode/substringstemp.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | public class substringstemp { 3 | public static void main(String[] args) { 4 | List> ans = subs("", "aab", new ArrayList<>(), new ArrayList<>()); 5 | for (int i = 0; i < ans.size(); i++) { 6 | System.out.println(ans.get(i)); 7 | } 8 | } 9 | public static List> subs(String p, String up, List curr, List> ans){ 10 | if(up.isEmpty()){ 11 | // curr.add(p); 12 | ans.add(new ArrayList<>(curr)); 13 | return ans; 14 | } 15 | curr.add(p+up.charAt(0)); 16 | subs(p+up.charAt(0), up.substring(1), curr, ans); 17 | curr.remove(curr.size()-1); 18 | subs(p, up.substring(1), curr, ans); 19 | return ans; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /Learning&Leetcode/subtractProductAndSum.java: -------------------------------------------------------------------------------- 1 | /*Given an integer number n, return the difference between the product of its digits and the sum of its digits. */ 2 | class Solution { 3 | public int subtractProductAndSum(int n) { 4 | int sum = 0; 5 | int product = 1; 6 | while(n>0){ 7 | int last = n%10; 8 | sum += last; 9 | product *= last; 10 | n /= 10; 11 | } 12 | return (product-sum); 13 | } 14 | } -------------------------------------------------------------------------------- /Learning&Leetcode/sumofpowerofthree.java: -------------------------------------------------------------------------------- 1 | public class sumofpowerofthree { 2 | public boolean checkPowersOfThree(int n) { 3 | return three(n); 4 | } 5 | public static boolean three(int n){ 6 | if(n==0 || n==1) return true; 7 | else if(n%3 == 2) return false; 8 | else return three(n/3); 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /Learning&Leetcode/target.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sabyasachi-Seal/CodingInJava/e747fed9d30fdbfda059ac24c458b23707b40b31/Learning&Leetcode/target.class -------------------------------------------------------------------------------- /Learning&Leetcode/targetarray.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sabyasachi-Seal/CodingInJava/e747fed9d30fdbfda059ac24c458b23707b40b31/Learning&Leetcode/targetarray.class -------------------------------------------------------------------------------- /Learning&Leetcode/tempCodeRunnerFile.java: -------------------------------------------------------------------------------- 1 | ans += arr[1][i]; -------------------------------------------------------------------------------- /Learning&Leetcode/test.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sabyasachi-Seal/CodingInJava/e747fed9d30fdbfda059ac24c458b23707b40b31/Learning&Leetcode/test.class -------------------------------------------------------------------------------- /Learning&Leetcode/thirdmax.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sabyasachi-Seal/CodingInJava/e747fed9d30fdbfda059ac24c458b23707b40b31/Learning&Leetcode/thirdmax.class -------------------------------------------------------------------------------- /Learning&Leetcode/threesum.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sabyasachi-Seal/CodingInJava/e747fed9d30fdbfda059ac24c458b23707b40b31/Learning&Leetcode/threesum.class -------------------------------------------------------------------------------- /Learning&Leetcode/tolowercase.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sabyasachi-Seal/CodingInJava/e747fed9d30fdbfda059ac24c458b23707b40b31/Learning&Leetcode/tolowercase.class -------------------------------------------------------------------------------- /Learning&Leetcode/tolowercase.java: -------------------------------------------------------------------------------- 1 | // https://leetcode.com/problems/to-lower-case/ 2 | public class tolowercase { 3 | // public static String toLowerCase(String s) { 4 | // StringBuilder str = new StringBuilder(); 5 | // for(char a: s.toCharArray()){ 6 | // if(a>=65 && a<=90){ 7 | // str.append((char)(a+32)); 8 | // } 9 | // else{ 10 | // str.append((char)(a)); 11 | // } 12 | // } 13 | // return str.toString(); 14 | // } 15 | public static String toLowerCase(String s){ 16 | return s.toLowerCase(); 17 | } 18 | public static void main(String[] args) { 19 | String s = "THIS"; 20 | System.out.println(toLowerCase(s)); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /Learning&Leetcode/transpose.java: -------------------------------------------------------------------------------- 1 | // https://leetcode.com/problems/transpose-matrix/ 2 | class Solution { 3 | public int[][] transpose(int[][] matrix) { 4 | int[][] ans = new int[matrix.length][matrix[0].length]; 5 | for(int i=0; i=97 && c<= 122) || (c>=48 && c<=57)){ 7 | str.append(c); 8 | } 9 | else if((c>=65 && c<=90)){ 10 | str.append(Character.toLowerCase(c)); 11 | } 12 | } 13 | return isitpali(str.toString()); 14 | } 15 | public static boolean isitpali(String s){ 16 | int left = 0; 17 | int right = s.length(); 18 | while(left apnastack = new Stack<>(); 7 | for(char ch: s.toCharArray()){ 8 | if(ch == '('){ 9 | apnastack.push(')'); 10 | } 11 | else if(ch == '{'){ 12 | apnastack.push('}'); 13 | } 14 | else if(ch == '['){ 15 | apnastack.push(']'); 16 | } 17 | else if(apnastack.isEmpty() || apnastack.pop() != ch){ 18 | return false; 19 | } 20 | } 21 | return apnastack.isEmpty(); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /Learning&Leetcode/wigglesubsequence.java: -------------------------------------------------------------------------------- 1 | // https://leetcode.com/problems/wiggle-subsequence/ 2 | public class wigglesubsequence { 3 | public int wiggleMaxLength(int[] nums) { 4 | if(nums.length == 1 ) return 1; 5 | int i = 0; 6 | int posinegi = 0; 7 | int count = 0; 8 | while(i