├── .editorconfig ├── .gitattributes ├── .github ├── FUNDING.yml ├── dependabot.yml └── workflows │ ├── build.yml │ ├── gradle-dependency-submission.yml │ └── gradle-validate-wrapper.yml ├── .gitignore ├── LICENSE ├── README.md ├── build.gradle ├── c ├── build.gradle └── src │ ├── main │ ├── c │ │ ├── crackingthecode │ │ │ └── part1datastructures │ │ │ │ └── chapter1arraysandstrings.c │ │ └── projecteuler │ │ │ ├── problem003.c │ │ │ ├── problem004.c │ │ │ ├── problem005.c │ │ │ ├── problem006.c │ │ │ ├── problem007.c │ │ │ ├── problem008.c │ │ │ ├── problem009.c │ │ │ ├── problem010.c │ │ │ ├── problem011.c │ │ │ ├── problem011.txt │ │ │ ├── problem012.c │ │ │ ├── problem014.c │ │ │ └── problem015.c │ └── include │ │ ├── api │ │ └── color.h │ │ └── crackingthecode │ │ └── part1datastructures │ │ └── chapter1arraysandstrings.h │ └── test │ ├── c │ └── crackingthecode │ │ ├── part1datastructures │ │ └── test_chapter1arraysandstrings.c │ │ └── suite_crackingthecode.c │ └── include │ └── crackingthecode │ └── part1datastructures │ └── test_chapter1arraysandstrings.h ├── cpp ├── build.gradle └── src │ ├── main │ ├── cpp │ │ ├── api │ │ │ └── graph_node.cpp │ │ └── crackingthecode │ │ │ └── part1datastructures │ │ │ └── chapter1arraysandstrings.cpp │ └── include │ │ ├── api │ │ ├── color.h │ │ ├── graph_node.h │ │ └── util │ │ │ └── swap.h │ │ └── crackingthecode │ │ └── part1datastructures │ │ └── chapter1arraysandstrings.h │ └── test │ ├── cpp │ ├── api │ │ └── test_graph_node.cpp │ └── crackingthecode │ │ ├── part1datastructures │ │ └── test_chapter1arraysandstrings.cpp │ │ └── test_main.cpp │ └── include │ └── test │ └── configurable_event_listener.h ├── docs ├── build.gradle └── src │ └── main │ └── asciidoc │ ├── all.adoc │ ├── api.adoc │ ├── codeeveal.adoc │ ├── codingbat.adoc │ ├── crackingthecode.adoc │ ├── github.adoc │ ├── index.adoc │ ├── leetcode.adoc │ ├── projecteuler.adoc │ ├── questions.adoc │ ├── stackoverflow.adoc │ └── tests.adoc ├── gradle ├── libs.versions.toml └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── java ├── build.gradle └── src │ ├── main │ └── java │ │ ├── android │ │ └── AsyncTask.java │ │ ├── api │ │ ├── Color.java │ │ ├── DoublyLinkedList.java │ │ ├── GraphNode.java │ │ ├── HashMap.java │ │ ├── LinkedList.java │ │ ├── Node.java │ │ ├── Pair.java │ │ ├── Point.java │ │ ├── Queue.java │ │ ├── Stack.java │ │ ├── TreeNode.java │ │ ├── Trie.java │ │ ├── TrieNode.java │ │ ├── Trio.java │ │ └── util │ │ │ └── Swap.java │ │ ├── architecture │ │ └── mvp │ │ │ ├── Contract.java │ │ │ ├── PresenterImpl.java │ │ │ └── ViewImpl.java │ │ ├── codeeval │ │ └── easy │ │ │ ├── agedistribution │ │ │ ├── Main.java │ │ │ └── input.txt │ │ │ ├── armstrongnumbers │ │ │ ├── Main.java │ │ │ └── input.txt │ │ │ ├── bitpositions │ │ │ ├── Main.java │ │ │ └── input.txt │ │ │ ├── calculatedistance │ │ │ ├── Main.java │ │ │ └── input.txt │ │ │ ├── capitalizewords │ │ │ ├── Main.java │ │ │ └── input.txt │ │ │ ├── compressedsequence │ │ │ ├── Main.java │ │ │ └── input.txt │ │ │ ├── datarecovery │ │ │ ├── Main.java │ │ │ └── input.txt │ │ │ ├── deltatime │ │ │ ├── Main.java │ │ │ └── input.txt │ │ │ ├── evennumbers │ │ │ ├── Main.java │ │ │ └── input.txt │ │ │ ├── hiddendigits │ │ │ ├── Main.java │ │ │ └── input.txt │ │ │ ├── lettercasepercentageratio │ │ │ ├── Main.java │ │ │ └── input.txt │ │ │ ├── longestword │ │ │ ├── Main.java │ │ │ └── input.txt │ │ │ ├── lowestuniquenumber │ │ │ ├── Main.java │ │ │ └── input.txt │ │ │ ├── matrixrotation │ │ │ ├── Main.java │ │ │ └── input.txt │ │ │ ├── mixedcontent │ │ │ ├── Main.java │ │ │ └── input.txt │ │ │ ├── morsecode │ │ │ ├── Main.java │ │ │ └── input.txt │ │ │ ├── multiplylists │ │ │ ├── Main.java │ │ │ └── input.txt │ │ │ ├── readmore │ │ │ ├── Main.java │ │ │ └── input.txt │ │ │ ├── roadtrip │ │ │ ├── Main.java │ │ │ └── input.txt │ │ │ ├── rollercoaster │ │ │ ├── Main.java │ │ │ └── input.txt │ │ │ ├── romannumerals │ │ │ ├── Main.java │ │ │ └── input.txt │ │ │ ├── sumofdigits │ │ │ ├── Main.java │ │ │ └── input.txt │ │ │ ├── swapcase │ │ │ ├── Main.java │ │ │ └── input.txt │ │ │ ├── themajorelement │ │ │ ├── Main.java │ │ │ └── input.txt │ │ │ ├── uniqueelements │ │ │ ├── Main.java │ │ │ └── input.txt │ │ │ ├── withoutrepetitions │ │ │ ├── Main.java │ │ │ └── input.txt │ │ │ └── wordtodigit │ │ │ ├── Main.java │ │ │ └── input.txt │ │ ├── coderbyte │ │ └── FirstFactorial.java │ │ ├── codingbat │ │ ├── ap1 │ │ │ ├── BigHeights.java │ │ │ ├── CommonTwo.java │ │ │ ├── CopyEndy.java │ │ │ ├── CopyEvens.java │ │ │ ├── DividesSelf.java │ │ │ ├── HasOne.java │ │ │ ├── MatchUp.java │ │ │ ├── MergeTwo.java │ │ │ ├── ScoreUp.java │ │ │ ├── Scores100.java │ │ │ ├── ScoresAverage.java │ │ │ ├── ScoresClump.java │ │ │ ├── ScoresIncreasing.java │ │ │ ├── ScoresSpecial.java │ │ │ ├── SumHeights.java │ │ │ ├── SumHeights2.java │ │ │ ├── UserCompare.java │ │ │ ├── WordsCount.java │ │ │ ├── WordsFront.java │ │ │ ├── WordsWithout.java │ │ │ └── WordsWithoutList.java │ │ ├── functional1 │ │ │ ├── AddStar.java │ │ │ ├── Copies3.java │ │ │ ├── Doubling.java │ │ │ ├── Lower.java │ │ │ ├── Math1.java │ │ │ ├── MoreY.java │ │ │ ├── NoX.java │ │ │ ├── RightDigit.java │ │ │ └── Square.java │ │ ├── functional2 │ │ │ ├── NoNeg.java │ │ │ └── NoZ.java │ │ ├── map1 │ │ │ ├── MapAB.java │ │ │ ├── MapAB2.java │ │ │ ├── MapAB3.java │ │ │ ├── MapAB4.java │ │ │ ├── MapBully.java │ │ │ ├── MapShare.java │ │ │ ├── Topping1.java │ │ │ ├── Topping2.java │ │ │ └── Topping3.java │ │ ├── map2 │ │ │ ├── AllSwap.java │ │ │ ├── FirstChar.java │ │ │ ├── FirstSwap.java │ │ │ ├── Pairs.java │ │ │ ├── Word0.java │ │ │ ├── WordAppend.java │ │ │ ├── WordCount.java │ │ │ ├── WordLen.java │ │ │ └── WordMultiple.java │ │ ├── recursion1 │ │ │ ├── AllStar.java │ │ │ ├── Array11.java │ │ │ ├── Array6.java │ │ │ ├── BunnyEars.java │ │ │ ├── BunnyEars2.java │ │ │ ├── ChangePi.java │ │ │ ├── ChangeXY.java │ │ │ ├── Count11.java │ │ │ ├── Count7.java │ │ │ ├── CountAbc.java │ │ │ ├── CountX.java │ │ │ ├── Factorial.java │ │ │ ├── Fibonacci.java │ │ │ ├── NestParen.java │ │ │ ├── NoX.java │ │ │ ├── PairStar.java │ │ │ ├── PowerN.java │ │ │ ├── StrCount.java │ │ │ ├── StringClean.java │ │ │ └── SumDigits.java │ │ └── warmup1 │ │ │ ├── BackAround.java │ │ │ ├── Close10.java │ │ │ ├── DelDel.java │ │ │ ├── Diff21.java │ │ │ ├── EndUp.java │ │ │ ├── EveryNth.java │ │ │ ├── Front22.java │ │ │ ├── Front3.java │ │ │ ├── FrontBack.java │ │ │ ├── HasTeen.java │ │ │ ├── IcyHot.java │ │ │ ├── In1020.java │ │ │ ├── In3050.java │ │ │ ├── IntMax.java │ │ │ ├── LastDigit.java │ │ │ ├── LoneTeen.java │ │ │ ├── Makes10.java │ │ │ ├── Max1020.java │ │ │ ├── MissingChar.java │ │ │ ├── MixStart.java │ │ │ ├── MonkeyTrouble.java │ │ │ ├── NearHundred.java │ │ │ ├── NotString.java │ │ │ ├── Or35.java │ │ │ ├── ParrotTrouble.java │ │ │ ├── PosNeg.java │ │ │ ├── SleepIn.java │ │ │ ├── StartHi.java │ │ │ ├── StartOz.java │ │ │ ├── StringE.java │ │ │ └── SumDouble.java │ │ ├── crackingthecode │ │ ├── Chapter11Testing.java │ │ ├── Chapter12SystemDesignAndMemoryLimits.java │ │ ├── Chapter13CPlusPlus.java │ │ ├── Chapter14Java.java │ │ ├── Chapter15Databases.java │ │ ├── Chapter17Networking.java │ │ ├── Chapter18ThreadsAndLocks.java │ │ ├── Chapter19Moderate.java │ │ ├── Chapter1ArraysAndStrings.java │ │ ├── Chapter20Hard.java │ │ ├── Chapter2LinkedLists.java │ │ ├── Chapter3StacksAndQueues.java │ │ ├── Chapter4TreesAndGraphs.java │ │ ├── Chapter5BitManipulation.java │ │ ├── Chapter6BrainTeasers.java │ │ ├── Chapter7ObjectOrientedDesign.java │ │ ├── Chapter8Recursion.java │ │ └── Chapter9SortingAndSearching.java │ │ ├── leetcode │ │ ├── ExploreInsertIntoABinarySearchTree.java │ │ ├── ExploreSearchInABinarySearchTree.java │ │ ├── Problem100SameTree.java │ │ ├── Problem101SymmetricTree.java │ │ ├── Problem1022SumOfRootToLeafBinaryNumbers.java │ │ ├── Problem102BinaryTreeLevelOrderTraversal.java │ │ ├── Problem1038BinarySearchTreeToGreaterSumTree.java │ │ ├── Problem1047RemoveAllAdjacentDuplicatesInString.java │ │ ├── Problem104MaximumDepthOfBinaryTree.java │ │ ├── Problem1078OccurrencesAfterBigram.java │ │ ├── Problem107BinaryTreeLevelOrderTraversalII.java │ │ ├── Problem1085SumOfDigitsInTheMinimumNumber.java │ │ ├── Problem1089DuplicateZeros.java │ │ ├── Problem108ConvertSortedArrayToBinarySearchTree.java │ │ ├── Problem1099TwoSumLessThanK.java │ │ ├── Problem109ConvertSortedListToBinarySearchTree.java │ │ ├── Problem10RegularExpressionMatching.java │ │ ├── Problem1108DefangingAnIPAddress.java │ │ ├── Problem110BalancedBinaryTree.java │ │ ├── Problem1114PrintInOrder.java │ │ ├── Problem1115PrintFooBarAlternately.java │ │ ├── Problem1119RemoveVowelsFromAString.java │ │ ├── Problem111MinimumDepthOfBinaryTree.java │ │ ├── Problem112PathSum.java │ │ ├── Problem1133LargestUniqueNumber.java │ │ ├── Problem1134ArmstrongNumber.java │ │ ├── Problem114FlattenBinaryTreeToLinkedList.java │ │ ├── Problem116PopulatingNextRightPointersInEachNode.java │ │ ├── Problem118PascalsTriangle.java │ │ ├── Problem119PascalsTriangleII.java │ │ ├── Problem1207UniqueNumberOfOccurrences.java │ │ ├── Problem1219PathWithMaximumGold.java │ │ ├── Problem121BestTimeToBuyAndSellStock.java │ │ ├── Problem1221SplitAStringInBalancedStrings.java │ │ ├── Problem1232CheckIfItIsAStraightLine.java │ │ ├── Problem125ValidPalindrome.java │ │ ├── Problem1265PrintImmutableLinkedListInReverse.java │ │ ├── Problem1281SubtractTheProductAndSumOfDigitsOfAnInteger.java │ │ ├── Problem1287ElementAppearingMoreThan25InSortedArray.java │ │ ├── Problem128LongestConsecutiveSequence.java │ │ ├── Problem1290ConvertBinaryNumberInALinkedListToInteger.java │ │ ├── Problem1295FindNumbersWithEvenNumberOfDigits.java │ │ ├── Problem129SumRootToLeafNumbers.java │ │ ├── Problem12IntegerToRoman.java │ │ ├── Problem1304FindNUniqueIntegersSumUpToZero.java │ │ ├── Problem1317ConvertIntegerToTheSumOfTwoNoZeroIntegers.java │ │ ├── Problem1331RankTransformOfAnArray.java │ │ ├── Problem1337TheKWeakestRowsInAMatrix.java │ │ ├── Problem133CloneGraph.java │ │ ├── Problem1342NumberOfStepsToReduceANumberToZero.java │ │ ├── Problem136SingleNumber.java │ │ ├── Problem137SingleNumberII.java │ │ ├── Problem1394FindLuckyIntegerInAnArray.java │ │ ├── Problem13RomanToInteger.java │ │ ├── Problem141LinkedListCycle.java │ │ ├── Problem142LinkedListCycleII.java │ │ ├── Problem1431KidsWithTheGreatestNumberOfCandies.java │ │ ├── Problem1446ConsecutiveCharacters.java │ │ ├── Problem144BinaryTreePreorderTraversal.java │ │ ├── Problem145BinaryTreePostorderTraversal.java │ │ ├── Problem1486XOROperationInAnArray.java │ │ ├── Problem14LongestCommonPrefix.java │ │ ├── Problem150EvaluateReversePolishNotation.java │ │ ├── Problem151ReverseWordsInAString.java │ │ ├── Problem1523CountOddNumbersInAnIntervalRange.java │ │ ├── Problem1528ShuffleString.java │ │ ├── Problem153FindMinimumInRotatedSortedArray.java │ │ ├── Problem153Sum.java │ │ ├── Problem155MinStack.java │ │ ├── Problem157ReadNCharactersGivenRead4.java │ │ ├── Problem160IntersectionOfTwoLinkedLists.java │ │ ├── Problem162FindPeakElement.java │ │ ├── Problem1636SortArrayByIncreasingFrequency.java │ │ ├── Problem165CompareVersionNumbers.java │ │ ├── Problem1662CheckIfTwoStringArraysAreEquivalent.java │ │ ├── Problem1672RichestCustomerWealth.java │ │ ├── Problem167TwoSumII.java │ │ ├── Problem168ExcelSheetColumnTitle.java │ │ ├── Problem169MajorityElement.java │ │ ├── Problem171ExcelSheetColumnNumber.java │ │ ├── Problem172FactorialTrailingZeroes.java │ │ ├── Problem1748SumOfUniqueElements.java │ │ ├── Problem1768MergeStringsAlternately.java │ │ ├── Problem179LargestNumber.java │ │ ├── Problem17LetterCombinationsOfAPhoneNumber.java │ │ ├── Problem1800MaximumAscendingSubarraySum.java │ │ ├── Problem1816TruncateSentence.java │ │ ├── Problem184Sum.java │ │ ├── Problem189RotateArray.java │ │ ├── Problem190ReverseBits.java │ │ ├── Problem191NumberOf1Bits.java │ │ ├── Problem198HouseRobber.java │ │ ├── Problem19RemoveNthNodeFromEndOfList.java │ │ ├── Problem1TwoSum.java │ │ ├── Problem200NumberOfIslands.java │ │ ├── Problem202HappyNumber.java │ │ ├── Problem203RemoveLinkedListElements.java │ │ ├── Problem204CountPrimes.java │ │ ├── Problem206ReverseLinkedList.java │ │ ├── Problem208ImplementTrie.java │ │ ├── Problem20ValidParentheses.java │ │ ├── Problem212WordSearchII.java │ │ ├── Problem215KthLargestElementInAnArray.java │ │ ├── Problem217ContainsDuplicate.java │ │ ├── Problem21MergeTwoSortedLists.java │ │ ├── Problem226InvertBinaryTree.java │ │ ├── Problem22GenerateParentheses.java │ │ ├── Problem231PowerOfTwo.java │ │ ├── Problem232ImplementQueueUsingStacks.java │ │ ├── Problem234PalindromeLinkedList.java │ │ ├── Problem235LowestCommonAncestorOfABinarySearchTree.java │ │ ├── Problem236LowestCommonAncestorOfABinaryTree.java │ │ ├── Problem237DeleteNodeInALinkedList.java │ │ ├── Problem238ProductOfArrayExceptSelf.java │ │ ├── Problem23MergeKLists.java │ │ ├── Problem240SearchA2DMatrixII.java │ │ ├── Problem242ValidAnagram.java │ │ ├── Problem252MeetingRooms.java │ │ ├── Problem253MeetingRoomsII.java │ │ ├── Problem258AddDigits.java │ │ ├── Problem263UglyNumber.java │ │ ├── Problem268MissingNumber.java │ │ ├── Problem26RemoveDuplicatesFromSortedArray.java │ │ ├── Problem273IntegerToEnglishWords.java │ │ ├── Problem278FirstBadVersion.java │ │ ├── Problem27RemoveElement.java │ │ ├── Problem283MoveZeroes.java │ │ ├── Problem287FindTheDuplicateNumber.java │ │ ├── Problem28ImplementStrStr.java │ │ ├── Problem290WordPattern.java │ │ ├── Problem292NimGame.java │ │ ├── Problem2AddTwoNumbers.java │ │ ├── Problem303RangeSumQueryImmutable.java │ │ ├── Problem304RangeSumQuery2DImmutable.java │ │ ├── Problem326PowerOfThree.java │ │ ├── Problem329LongestIncreasingPathInAMatrix.java │ │ ├── Problem338CountingBits.java │ │ ├── Problem339NestedListWeightSum.java │ │ ├── Problem33SearchInRotatedSortedArray.java │ │ ├── Problem342PowerOfFour.java │ │ ├── Problem344ReverseString.java │ │ ├── Problem345ReverseVowelsOfAString.java │ │ ├── Problem346MovingAverageFromDataStream.java │ │ ├── Problem347TopKFrequentElements.java │ │ ├── Problem349IntersectionOfTwoArrays.java │ │ ├── Problem350IntersectionOfTwoArraysII.java │ │ ├── Problem35SearchInsertPosition.java │ │ ├── Problem367ValidPerfectSquare.java │ │ ├── Problem371SumOfTwoIntegers.java │ │ ├── Problem374GuessNumberHigherOrLower.java │ │ ├── Problem382LinkedListRandomNode.java │ │ ├── Problem383RansomNote.java │ │ ├── Problem384ShuffleAnArray.java │ │ ├── Problem387FirstUniqueCharacterInAString.java │ │ ├── Problem389FindTheDifference.java │ │ ├── Problem398RandomPickIndex.java │ │ ├── Problem39CombinationSum.java │ │ ├── Problem404SumOfLeftLeaves.java │ │ ├── Problem405ConvertANumberToHexadecimal.java │ │ ├── Problem412FizzBuzz.java │ │ ├── Problem414ThirdMaximumNumber.java │ │ ├── Problem415AddStrings.java │ │ ├── Problem42TrappingRainWater.java │ │ ├── Problem434NumberOfSegmentsInAString.java │ │ ├── Problem442FindAllDuplicatesInAnArray.java │ │ ├── Problem448FindAllNumbersDisappearedInAnArray.java │ │ ├── Problem450DeleteNodeInABST.java │ │ ├── Problem451SortCharactersByFrequency.java │ │ ├── Problem4544SumII.java │ │ ├── Problem461HammingDistance.java │ │ ├── Problem46Permutations.java │ │ ├── Problem476NumberComplement.java │ │ ├── Problem485MaxConsecutiveOnes.java │ │ ├── Problem48RotateImage.java │ │ ├── Problem496NextGreaterElementI.java │ │ ├── Problem49GroupAnagrams.java │ │ ├── Problem500KeyboardRow.java │ │ ├── Problem504Base7.java │ │ ├── Problem507PerfectNumber.java │ │ ├── Problem509FibonacciNumber.java │ │ ├── Problem50Pow.java │ │ ├── Problem515FindLargestValueInEachTreeRow.java │ │ ├── Problem520DetectCapital.java │ │ ├── Problem532KDiffPairsInAnArray.java │ │ ├── Problem535EncodeAndDecodeTinyURL.java │ │ ├── Problem538ConvertBSTToGreaterTree.java │ │ ├── Problem53MaximumSubarray.java │ │ ├── Problem540SingleElementInASortedArray.java │ │ ├── Problem541ReverseStringII.java │ │ ├── Problem541ReverseWordsInAStringII.java │ │ ├── Problem551StudentAttendanceRecordI.java │ │ ├── Problem557ReverseWordsInAStringIII.java │ │ ├── Problem55JumpGame.java │ │ ├── Problem560SubarraySumEqualsK.java │ │ ├── Problem56MergeIntervals.java │ │ ├── Problem572SubtreeOfAnotherTree.java │ │ ├── Problem575DistributeCandies.java │ │ ├── Problem58LengthOfLastWord.java │ │ ├── Problem617MergeTwoBinaryTrees.java │ │ ├── Problem61RotateList.java │ │ ├── Problem62UniquePaths.java │ │ ├── Problem633SumOfSquareNumbers.java │ │ ├── Problem637AverageOfLevelsInBinaryTree.java │ │ ├── Problem648ReplaceWords.java │ │ ├── Problem657JudgeRouteCircle.java │ │ ├── Problem66PlusOne.java │ │ ├── Problem677MapSumPairs.java │ │ ├── Problem67AddBinary.java │ │ ├── Problem686RepeatedStringMatch.java │ │ ├── Problem690EmployeeImportance.java │ │ ├── Problem692TopKFrequentWords.java │ │ ├── Problem69Sqrt.java │ │ ├── Problem704BinarySearch.java │ │ ├── Problem705DesignHashSet.java │ │ ├── Problem706DesignHashMap.java │ │ ├── Problem707DesignLinkedList.java │ │ ├── Problem709ToLowerCase.java │ │ ├── Problem724FindPivotIndex.java │ │ ├── Problem733FloodFill.java │ │ ├── Problem73SetMatrixZeroes.java │ │ ├── Problem744FindSmallestLetterGreaterThanTarget.java │ │ ├── Problem747LargestNumberAtLeastTwiceOfOthers.java │ │ ├── Problem74SearchA2DMatrix.java │ │ ├── Problem75SortColors.java │ │ ├── Problem760FindAnagramMappings.java │ │ ├── Problem769MaxChunksToMakeSorted.java │ │ ├── Problem771JewelsAndStones.java │ │ ├── Problem77Combinations.java │ │ ├── Problem78Subsets.java │ │ ├── Problem796RotateString.java │ │ ├── Problem79WordSearch.java │ │ ├── Problem7ReverseInteger.java │ │ ├── Problem804UniqueMorseCodeWords.java │ │ ├── Problem80RemoveDuplicatesFromSortedArrayII.java │ │ ├── Problem819MostCommonWord.java │ │ ├── Problem832FlippingAnImage.java │ │ ├── Problem83RemoveDuplicatesFromSortedList.java │ │ ├── Problem867TransposeMatrix.java │ │ ├── Problem872LeafSimilarTrees.java │ │ ├── Problem876MiddleOfTheLinkedList.java │ │ ├── Problem884UncommonWordsFromTwoSentences.java │ │ ├── Problem88MergeSortedArray.java │ │ ├── Problem905SortArrayByParity.java │ │ ├── Problem90SubsetsII.java │ │ ├── Problem912SortAnArray.java │ │ ├── Problem917ReverseOnlyLetters.java │ │ ├── Problem938RangeSumOfBST.java │ │ ├── Problem94BinaryTreeInorderTraversal.java │ │ ├── Problem961NRepeatedElementInSize2NArray.java │ │ ├── Problem965UnivaluedBinaryTree.java │ │ ├── Problem977SquaresOfASortedArray.java │ │ ├── Problem98ValidateBinarySearchTree.java │ │ ├── Problem9PalindromeNumber.java │ │ └── api │ │ │ ├── Employee.java │ │ │ ├── ImmutableListNode.java │ │ │ ├── Interval.java │ │ │ ├── ListNode.java │ │ │ ├── NestedInteger.java │ │ │ ├── Node.java │ │ │ └── TreeNode.java │ │ ├── projecteuler │ │ ├── Number001.java │ │ ├── Number002.java │ │ ├── Number003.java │ │ └── Number004.java │ │ └── questions │ │ ├── AG10CharWindow.java │ │ ├── AGMonochrome.java │ │ ├── BstContains.java │ │ ├── BstFindMax.java │ │ ├── BstFindMin.java │ │ ├── BstHeight.java │ │ ├── BstInOrder.java │ │ ├── BubbleSort.java │ │ ├── FindKSmallestNumbers.java │ │ ├── LongestSequenceMatrix.java │ │ ├── MaxNonContSubArray.java │ │ ├── MergeSort.java │ │ ├── NaturalNumbersSum.java │ │ ├── NumberLength.java │ │ ├── Partition.java │ │ ├── Permutations.java │ │ ├── QuickSort.java │ │ ├── RemoveEveryOther.java │ │ ├── ReorderBooleanArray.java │ │ ├── ReverseArray.java │ │ ├── SearchAdjacentElements.java │ │ ├── Substring.java │ │ ├── TrailingZeros.java │ │ └── TreeHeight.java │ └── test │ └── groovy │ ├── api │ ├── ColorSpec.groovy │ ├── GraphNodeSpec.groovy │ ├── LinkedListSpec.groovy │ ├── NodeSpec.groovy │ ├── PairSpec.groovy │ ├── PointSpec.groovy │ ├── QueueSpec.groovy │ ├── StackSpec.groovy │ ├── TreeNodeSpec.groovy │ ├── TrieNodeSpec.groovy │ ├── TrieSpec.groovy │ ├── TrioSpec.groovy │ └── util │ │ └── SwapSpec.groovy │ ├── codingbat │ └── recursion1 │ │ └── AllStarTest.groovy │ ├── crackingthecode │ ├── part1datastructures │ │ ├── Chapter1ArraysAndStringsSpec.groovy │ │ ├── Chapter2LinkedListsSpec.groovy │ │ ├── Chapter3StacksAndQueuesSpec.groovy │ │ └── Chapter4TreesAndGraphsSpec.groovy │ ├── part2conceptsandalgorithms │ │ ├── Chapter5BitManipulationSpec.groovy │ │ ├── Chapter6BrainTeasersSpec.groovy │ │ ├── Chapter8RecursionSpec.groovy │ │ └── Chapter9SortingAndSearchingSpec.groovy │ └── part4additionalreviewproblems │ │ ├── Chapter19ModerateSpec.groovy │ │ └── Chapter20HardSpec.groovy │ ├── leetcode │ ├── ExploreBinarySearchSpec.groovy │ ├── Problem1047RemoveAllAdjacentDuplicatesInStringSpec.groovy │ ├── Problem108ConvertSortedArrayToBinarySearchTreeSpec.groovy │ ├── Problem10RegularExpressionMatchingSpec.groovy │ ├── Problem1219PathWithMaximumGoldSpec.groovy │ ├── Problem121BestTimeToBuyAndSellStockSpec.groovy │ ├── Problem125ValidPalindromeSpec.groovy │ ├── Problem1295FindNumbersWithEvenNumberOfDigitsSpec.groovy │ ├── Problem12IntegerToRomanSpec.groovy │ ├── Problem136SingleNumberSpec.groovy │ ├── Problem13RomanToIntegerSpec.groovy │ ├── Problem1431KidsWithTheGreatestNumberOfCandiesSpec.groovy │ ├── Problem1446ConsecutiveCharactersSpec.groovy │ ├── Problem144BinaryTreePreorderTraversalSpec.groovy │ ├── Problem1486XOROperationInAnArraySpec.groovy │ ├── Problem14LongestCommonPrefixSpec.groovy │ ├── Problem150EvaluateReversePolishNotationSpec.groovy │ ├── Problem151ReverseWordsInAStringSpec.groovy │ ├── Problem1523CountOddNumbersInAnIntervalRangeSpec.groovy │ ├── Problem1528ShuffleStringSpec.groovy │ ├── Problem153FindMinimumInRotatedSortedArraySpec.groovy │ ├── Problem153SumSpec.groovy │ ├── Problem162FindPeakElementSpec.groovy │ ├── Problem1636SortArrayByIncreasingFrequencySpec.groovy │ ├── Problem165CompareVersionNumbersSpec.groovy │ ├── Problem1662CheckIfTwoStringArraysAreEquivalentSpec.groovy │ ├── Problem172FactorialTrailingZeroesSpec.groovy │ ├── Problem1748SumOfUniqueElementsSpec.groovy │ ├── Problem1768MergeStringsAlternatelySpec.groovy │ ├── Problem179LargestNumberSpec.groovy │ ├── Problem17LetterCombinationsOfAPhoneNumberSpec.groovy │ ├── Problem1800MaximumAscendingSubarraySumSpec.groovy │ ├── Problem1816TruncateSentenceSpec.groovy │ ├── Problem184SumSpec.groovy │ ├── Problem189RotateArraySpec.groovy │ ├── Problem1TwoSumSpec.groovy │ ├── Problem200NumberOfIslandsSpec.groovy │ ├── Problem20ValidParenthesesSpec.groovy │ ├── Problem215KthLargestElementInAnArraySpec.groovy │ ├── Problem21MergeTwoSortedListsSpec.groovy │ ├── Problem22GenerateParenthesesSpec.groovy │ ├── Problem237DeleteNodeInALinkedListSpec.groovy │ ├── Problem238ProductOfArrayExceptSelfSpec.groovy │ ├── Problem240SearchA2DMatrixIISpec.groovy │ ├── Problem242ValidAnagramSpec.groovy │ ├── Problem252MeetingRoomsSpec.groovy │ ├── Problem253MeetingRoomsIISpec.groovy │ ├── Problem268MissingNumberSpec.groovy │ ├── Problem26RemoveDuplicatesFromSortedArraySpec.groovy │ ├── Problem273IntegerToEnglishWordsSpec.groovy │ ├── Problem27RemoveElementSpec.groovy │ ├── Problem28ImplementStrStrSpec.groovy │ ├── Problem290WordPatternSpec.groovy │ ├── Problem292NimGameSpec.groovy │ ├── Problem2AddTwoNumbersSpec.groovy │ ├── Problem329LongestIncreasingPathInAMatrixSpec.groovy │ ├── Problem33SearchInRotatedSortedArraySpec.groovy │ ├── Problem344ReverseStringSpec.groovy │ ├── Problem345ReverseVowelsOfAStringSpec.groovy │ ├── Problem347TopKFrequentElementsSpec.groovy │ ├── Problem349IntersectionOfTwoArraysSpec.groovy │ ├── Problem350IntersectionOfTwoArraysIISpec.groovy │ ├── Problem35SearchInsertPositionSpec.groovy │ ├── Problem367ValidPerfectSquareSpec.groovy │ ├── Problem371SumOfTwoIntegersSpec.groovy │ ├── Problem387FirstUniqueCharacterInAStringSpec.groovy │ ├── Problem39CombinationSumSpec.groovy │ ├── Problem405ConvertANumberToHexadecimalSpec.groovy │ ├── Problem412FizzBuzzSpec.groovy │ ├── Problem414ThirdMaximumNumberSpec.groovy │ ├── Problem42TrappingRainWaterSpec.groovy │ ├── Problem442FindAllDuplicatesInAnArraySpec.groovy │ ├── Problem450DeleteNodeInABSTSpec.groovy │ ├── Problem451SortCharactersByFrequencySpec.groovy │ ├── Problem4544SumIISpec.groovy │ ├── Problem46PermutationsSpec.groovy │ ├── Problem485MaxConsecutiveOnesSpec.groovy │ ├── Problem49GroupAnagramsSpec.groovy │ ├── Problem500KeyboardRowSpec.groovy │ ├── Problem504Base7Spec.groovy │ ├── Problem509FibonacciNumberSpec.groovy │ ├── Problem50PowSpec.groovy │ ├── Problem520DetectCapitalSpec.groovy │ ├── Problem532KDiffPairsInAnArraySpec.groovy │ ├── Problem53MaximumSubarraySpec.groovy │ ├── Problem541ReverseStringIISpec.groovy │ ├── Problem557ReverseWordsInAStringIIISpec.groovy │ ├── Problem55JumpGameSpec.groovy │ ├── Problem560SubarraySumEqualsKSpec.groovy │ ├── Problem56MergeIntervalsSpec.groovy │ ├── Problem657JudgeRouteCircleSpec.groovy │ ├── Problem66PlusOneSpec.groovy │ ├── Problem67AddBinarySpec.groovy │ ├── Problem686RepeatedStringMatchSpec.groovy │ ├── Problem692TopKFrequentWordsSpec.groovy │ ├── Problem69SqrtSpec.groovy │ ├── Problem709ToLowerCaseSpec.groovy │ ├── Problem724FindPivotIndexSpec.groovy │ ├── Problem744FindSmallestLetterGreaterThanTargetSpec.groovy │ ├── Problem74SearchA2DMatrixSpec.groovy │ ├── Problem760FindAnagramMappingsSpec.groovy │ ├── Problem769MaxChunksToMakeSortedSpec.groovy │ ├── Problem771JewelsAndStonesSpec.groovy │ ├── Problem77CombinationsSpec.groovy │ ├── Problem78SubsetsSpec.groovy │ ├── Problem796RotateStringSpec.groovy │ ├── Problem7ReverseIntegerSpec.groovy │ ├── Problem80RemoveDuplicatesFromSortedArrayIISpec.groovy │ ├── Problem88MergeSortedArraySpec.groovy │ ├── Problem905SortArrayByParitySpec.groovy │ ├── Problem90SubsetsIISpec.groovy │ ├── Problem961NRepeatedElementInSize2NArraySpec.groovy │ ├── Problem977SquaresOfASortedArraySpec.groovy │ └── Problem9PalindromeNumberSpec.groovy │ ├── projecteuler │ ├── Number001Spec.groovy │ ├── Number002Spec.groovy │ ├── Number003Spec.groovy │ └── Number004Spec.groovy │ └── questions │ ├── BstContainsSpec.groovy │ ├── BstFindMaxSpec.groovy │ ├── BstFindMinSpec.groovy │ ├── BubbleSortSpec.groovy │ ├── FindKSmallestNumbersSpec.groovy │ ├── LongestSequenceMatrixSpec.groovy │ ├── MergeSortSpec.groovy │ ├── NaturalNumbersSumSpec.groovy │ ├── NumberLengthSpec.groovy │ ├── PartitionSpec.groovy │ ├── PermutationsSpec.groovy │ ├── QuickSortSpec.groovy │ ├── RemoveEveryOtherSpec.groovy │ ├── ReorderBooleanArraySpec.groovy │ ├── ReverseArraySpec.groovy │ ├── SearchAdjacentElementsSpec.groovy │ ├── SubstringSpec.groovy │ ├── TrailingZerosSpec.groovy │ └── TreeHeightSpec.groovy ├── kotlin ├── build.gradle └── src │ ├── main │ └── kotlin │ │ ├── api │ │ ├── Color.kt │ │ ├── GraphNode.kt │ │ ├── LinkedList.kt │ │ ├── Node.kt │ │ ├── Pair.kt │ │ ├── Point.kt │ │ ├── Queue.kt │ │ ├── Stack.kt │ │ ├── TreeNode.kt │ │ ├── Trie.kt │ │ ├── TrieNode.kt │ │ ├── Trio.kt │ │ └── util │ │ │ └── Swap.kt │ │ ├── exercism │ │ ├── HelloWorld.kt │ │ ├── ReverseString.kt │ │ └── Twofer.kt │ │ ├── koans │ │ ├── builders.kt │ │ ├── collections.kt │ │ ├── conventions.kt │ │ ├── generics.kt │ │ ├── introduction.kt │ │ └── properties.kt │ │ └── leetcode │ │ ├── ExploreInsertIntoABinarySearchTree.kt │ │ ├── ExploreSearchInABinarySearchTree.kt │ │ ├── Problem412FizzBuzz.kt │ │ └── api │ │ ├── Interval.kt │ │ ├── ListNode.kt │ │ ├── TreeLinkNode.kt │ │ ├── TreeNode.kt │ │ └── UndirectedGraphNode.kt │ └── test │ └── kotlin │ ├── api │ ├── ColorSpec.kt │ └── NodeSpec.kt │ └── exercism │ ├── HelloWorldTest.kt │ ├── ReverseStringTest.kt │ └── TwoferTest.kt ├── python ├── build.gradle ├── setup.cfg ├── setup.py └── src │ └── main │ └── python │ └── projecteuler │ ├── problem11.py │ ├── problem12.py │ ├── problem14.py │ ├── problem15.py │ ├── problem17.py │ ├── problem21.py │ ├── problem25.py │ ├── problem28.py │ ├── problem3.py │ ├── problem30.py │ ├── problem32.py │ ├── problem36.py │ ├── problem37.py │ ├── problem38.py │ ├── problem41.py │ ├── problem43.py │ ├── problem45.py │ ├── problem48.py │ ├── problem52.py │ ├── problem53.py │ ├── problem55.py │ ├── problem56.py │ ├── problem59.py │ ├── problem59.txt │ ├── problem63.py │ └── problem73.py ├── renovate.json └── settings.gradle /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | # Change these settings to your own preference 4 | [*] 5 | indent_style = space 6 | indent_size = 4 7 | 8 | # We recommend you to keep these unchanged 9 | end_of_line = lf 10 | charset = utf-8 11 | trim_trailing_whitespace = true 12 | insert_final_newline = true 13 | 14 | [*.md] 15 | trim_trailing_whitespace = false 16 | 17 | [*.yml] 18 | indent_size = 2 19 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | # Set the default behavior, in case people don't have core.autocrlf set. 2 | * text eol=lf 3 | *.bat eol=crlf 4 | *.jar binary 5 | -------------------------------------------------------------------------------- /.github/dependabot.yml: -------------------------------------------------------------------------------- 1 | version: 2 2 | 3 | updates: 4 | - package-ecosystem: "github-actions" 5 | directory: "/" 6 | schedule: 7 | interval: "daily" 8 | - package-ecosystem: "gradle" 9 | directory: "/" 10 | schedule: 11 | interval: "daily" 12 | -------------------------------------------------------------------------------- /.github/workflows/gradle-validate-wrapper.yml: -------------------------------------------------------------------------------- 1 | name: Validate Gradle Wrapper 2 | 3 | on: 4 | pull_request: 5 | paths: 6 | - gradlew 7 | - gradlew.bat 8 | - gradle/wrapper/** 9 | 10 | jobs: 11 | validate: 12 | runs-on: ubuntu-latest 13 | 14 | steps: 15 | - name: Checkout Project 16 | uses: actions/checkout@v4.2.2 17 | 18 | - name: Validate Gradle Wrapper 19 | uses: gradle/actions/wrapper-validation@v4 20 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ### Gradle ### 2 | .gradle/ 3 | build/ 4 | /*/build/ 5 | out/ 6 | CUnitAutomated-*.xml 7 | 8 | # Ignore Gradle GUI config 9 | gradle-app.setting 10 | 11 | # Avoid ignoring Gradle wrapper jar file (.jar files are usually ignored) 12 | !gradle-wrapper.jar 13 | 14 | # Cache of project 15 | .gradletasknamecache 16 | 17 | ### Intellij ### 18 | *.iml 19 | .idea/ 20 | 21 | /src/main/cpp/extra 22 | 23 | ### VSCode ### 24 | /.vscode 25 | 26 | ### Mac OSX ### 27 | .DS_Store 28 | 29 | ### Android Studio ### 30 | local.properties 31 | 32 | python/activate 33 | python/pinned.txt 34 | python.egg-info/ 35 | -------------------------------------------------------------------------------- /c/src/main/c/crackingthecode/part1datastructures/chapter1arraysandstrings.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include "crackingthecode/part1datastructures/chapter1arraysandstrings.h" 5 | 6 | bool isUniqueCharacters(char* input) { 7 | if (!input || *input == '\0') { 8 | return false; 9 | } 10 | 11 | int check = 0; 12 | for (; *input != '\0'; input++) { 13 | 14 | int value = 1 << (*input - 'a'); 15 | bool hasValue = (check & value) > 0; 16 | if (hasValue) { 17 | return false; 18 | } 19 | 20 | check = check | value; 21 | } 22 | 23 | return true; 24 | } 25 | -------------------------------------------------------------------------------- /c/src/main/c/projecteuler/problem005.c: -------------------------------------------------------------------------------- 1 | /* Smallest multiple 2 | * Problem 5 3 | * 2520 is the smallest number that can be divided by each of the numbers 4 | * from 1 to 10 without any remainder. What is the smallest positive 5 | * number that is evenly divisible by all of the numbers from 1 to 20? */ 6 | #include 7 | 8 | int smallest(int n) { 9 | int i = 0; 10 | 11 | for (i = 1; i < 21; i++) { 12 | if (n % i) { 13 | return 0; 14 | } 15 | } 16 | 17 | return 1; 18 | } 19 | 20 | 21 | int problem005() { 22 | int i = 1; 23 | 24 | while (! smallest(i)) { 25 | i++; 26 | } 27 | 28 | printf("%d\n", i); 29 | 30 | return 0; 31 | } 32 | -------------------------------------------------------------------------------- /c/src/main/c/projecteuler/problem015.c: -------------------------------------------------------------------------------- 1 | /* Lattice paths 2 | * Problem 15 3 | * Starting in the top left corner of a 2×2 grid, and only being able to 4 | * move to the right and down, there are exactly 6 routes to the bottom 5 | * right corner.How many such routes are there through a 20×20 grid? */ 6 | #include 7 | #include 8 | 9 | int problem015() { 10 | /* (20 * 2!/20!)/20! - > (21 -> 40)/20! */ 11 | 12 | int i = 0; 13 | int k = 0; 14 | double product = 1; 15 | 16 | for (i = 21, k = 20; i < 41; i++, k--) { 17 | product *= ((double)i / k); 18 | } 19 | 20 | printf("%ld\n", (long)ceil(product)); 21 | 22 | return 0; 23 | } 24 | -------------------------------------------------------------------------------- /c/src/main/include/api/color.h: -------------------------------------------------------------------------------- 1 | #ifndef COLOR_H 2 | #define COLOR_H 3 | 4 | enum color { 5 | GREEN, 6 | RED, 7 | BLUE 8 | }; 9 | 10 | #endif 11 | -------------------------------------------------------------------------------- /c/src/main/include/crackingthecode/part1datastructures/chapter1arraysandstrings.h: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | bool isUniqueCharacters(char* input); 4 | -------------------------------------------------------------------------------- /c/src/test/c/crackingthecode/part1datastructures/test_chapter1arraysandstrings.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "crackingthecode/part1datastructures/chapter1arraysandstrings.h" 4 | #include "crackingthecode/part1datastructures/test_chapter1arraysandstrings.h" 5 | 6 | void test_isUniqueCharacters() { 7 | CU_ASSERT(!isUniqueCharacters(NULL)); 8 | CU_ASSERT(!isUniqueCharacters("")); 9 | CU_ASSERT(!isUniqueCharacters("aab")); 10 | CU_ASSERT(!isUniqueCharacters("qwertyuiopplkjhgfdsazxcvbnm")); 11 | CU_ASSERT(isUniqueCharacters("abc")); 12 | CU_ASSERT(isUniqueCharacters("qazwsxedcrfvtgbyhnujmikolp")); 13 | } 14 | -------------------------------------------------------------------------------- /c/src/test/c/crackingthecode/suite_crackingthecode.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include "gradle_cunit_register.h" 3 | 4 | #include "crackingthecode/part1datastructures/test_chapter1arraysandstrings.h" 5 | 6 | int suite_init() { 7 | return 0; 8 | } 9 | 10 | int suite_clean() { 11 | return 0; 12 | } 13 | 14 | void gradle_cunit_register() { 15 | CU_pSuite suite = CU_add_suite("crackingthecode tests", suite_init, suite_clean); 16 | CU_add_test(suite, "test_isUniqueCharacters", test_isUniqueCharacters); 17 | } 18 | -------------------------------------------------------------------------------- /c/src/test/include/crackingthecode/part1datastructures/test_chapter1arraysandstrings.h: -------------------------------------------------------------------------------- 1 | void test_isUniqueCharacters(); 2 | -------------------------------------------------------------------------------- /cpp/src/main/cpp/crackingthecode/part1datastructures/chapter1arraysandstrings.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include "crackingthecode/part1datastructures/chapter1arraysandstrings.h" 5 | 6 | bool isUniqueCharacters(char* input) { 7 | if (!input || *input == '\0') { 8 | return false; 9 | } 10 | 11 | int check = 0; 12 | for (; *input != '\0'; input++) { 13 | 14 | int value = 1 << (*input - 'a'); 15 | bool hasValue = (check & value) > 0; 16 | if (hasValue) { 17 | return false; 18 | } 19 | 20 | check = check | value; 21 | } 22 | 23 | return true; 24 | } 25 | -------------------------------------------------------------------------------- /cpp/src/main/include/api/color.h: -------------------------------------------------------------------------------- 1 | #ifndef COLOR_H 2 | #define COLOR_H 3 | 4 | enum color { 5 | GREEN, 6 | RED, 7 | BLUE 8 | }; 9 | 10 | #endif 11 | -------------------------------------------------------------------------------- /cpp/src/main/include/api/graph_node.h: -------------------------------------------------------------------------------- 1 | #ifndef GRAPHNODE_H 2 | #define GRAPHNODE_H 3 | 4 | #include 5 | 6 | template 7 | class GraphNode { 8 | public: 9 | T value; 10 | GraphNode* next; 11 | GraphNode* previous; 12 | GraphNode() = default; 13 | GraphNode(T& value) : value(value) { } 14 | // GraphNode(GraphNode* next, T& copy); // copy 15 | // GraphNode(GraphNode* next, T&& move); // move 16 | // template 17 | // GraphNode(GraphNode* next, Args... args); // emplace data 18 | bool operator==(GraphNode&); 19 | friend std::ostream& operator<<(std::ostream&, GraphNode&); 20 | }; 21 | 22 | #endif 23 | -------------------------------------------------------------------------------- /cpp/src/main/include/api/util/swap.h: -------------------------------------------------------------------------------- 1 | #ifndef SWAP_H 2 | #define SWAP_H 3 | 4 | template 5 | class swap { 6 | public: 7 | static void swap(T& a, T& b) { 8 | T temp = a; 9 | a = b; 10 | b = temp; 11 | } 12 | }; 13 | 14 | #endif 15 | -------------------------------------------------------------------------------- /cpp/src/main/include/crackingthecode/part1datastructures/chapter1arraysandstrings.h: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | bool isUniqueCharacters(char* input); 4 | -------------------------------------------------------------------------------- /cpp/src/test/cpp/crackingthecode/part1datastructures/test_chapter1arraysandstrings.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "crackingthecode/part1datastructures/chapter1arraysandstrings.h" 4 | 5 | TEST(chapter1arraysandstrings, test_isUniqueCharacters) { 6 | ASSERT_TRUE(!isUniqueCharacters(nullptr)); 7 | ASSERT_TRUE(!isUniqueCharacters("")); 8 | ASSERT_TRUE(!isUniqueCharacters("aab")); 9 | ASSERT_TRUE(!isUniqueCharacters("qwertyuiopplkjhgfdsazxcvbnm")); 10 | ASSERT_TRUE(isUniqueCharacters("abc")); 11 | ASSERT_TRUE(isUniqueCharacters("qazwsxedcrfvtgbyhnujmikolp")); 12 | } 13 | -------------------------------------------------------------------------------- /cpp/src/test/cpp/crackingthecode/test_main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "test/configurable_event_listener.h" 3 | 4 | int main(int argc, char **argv) { 5 | // Init Gtest 6 | testing::InitGoogleTest(&argc, argv); 7 | 8 | // Remove the test default listener 9 | testing::TestEventListeners &listeners = testing::UnitTest::GetInstance()->listeners(); 10 | auto printer = listeners.Release(listeners.default_result_printer()); 11 | 12 | // Configure new listener 13 | auto *listener = ConfigurableEventListener::Builder(printer) 14 | .showFailures() 15 | .build(); 16 | listeners.Append(listener); 17 | 18 | // Run 19 | return RUN_ALL_TESTS(); 20 | } 21 | -------------------------------------------------------------------------------- /docs/src/main/asciidoc/all.adoc: -------------------------------------------------------------------------------- 1 | = {title} 2 | 3 | include::api.adoc[leveloffset=+1] 4 | include::codeeveal.adoc[leveloffset=+1] 5 | include::codingbat.adoc[leveloffset=+1] 6 | include::crackingthecode.adoc[leveloffset=+1] 7 | include::github.adoc[leveloffset=+1] 8 | include::leetcode.adoc[leveloffset=+1] 9 | include::projecteuler.adoc[leveloffset=+1] 10 | include::questions.adoc[leveloffset=+1] 11 | include::stackoverflow.adoc[leveloffset=+1] 12 | include::tests.adoc[leveloffset=+1] 13 | -------------------------------------------------------------------------------- /docs/src/main/asciidoc/api.adoc: -------------------------------------------------------------------------------- 1 | = Api 2 | 3 | Common classes used across solutions. 4 | -------------------------------------------------------------------------------- /docs/src/main/asciidoc/codeeveal.adoc: -------------------------------------------------------------------------------- 1 | = CodeEval 2 | 3 | *Site:* https://codeeval.com/ 4 | 5 | *My Solutions:* link:java/src/main/java/codeeval[https://github.com/jaredsburrows/cs-interview-questions/tree/master/java/src/main/java/codeeval] 6 | 7 | *My Profile:* https://codeeval.com/profile/jaredsburrows/ 8 | -------------------------------------------------------------------------------- /docs/src/main/asciidoc/codingbat.adoc: -------------------------------------------------------------------------------- 1 | = Codingbat 2 | 3 | *Site:* https://codingbat.com 4 | 5 | *My Solutions:* link:java/src/main/java/codingbat[https://github.com/jaredsburrows/cs-interview-questions/tree/master/java/src/main/java/codingbat] 6 | -------------------------------------------------------------------------------- /docs/src/main/asciidoc/crackingthecode.adoc: -------------------------------------------------------------------------------- 1 | = Cracking The Code Interview 2 | 3 | *Site:* https://www.careercup.com/book 4 | 5 | *Github:* https://github.com/gaylemcd/ctci 6 | 7 | *My Solutions:* link:java/src/main/java/crackingthecode[https://github.com/jaredsburrows/cs-interview-questions/tree/master/java/src/main/java/crackingthecode] 8 | -------------------------------------------------------------------------------- /docs/src/main/asciidoc/github.adoc: -------------------------------------------------------------------------------- 1 | = Github 2 | 3 | *My Profile:* https://github.com/jaredsburrows 4 | 5 | +++ 6 | 7 | 11 | 15 | 16 | 17 | +++ 18 | -------------------------------------------------------------------------------- /docs/src/main/asciidoc/index.adoc: -------------------------------------------------------------------------------- 1 | = {title} 2 | 3 | . <> 4 | . <> 5 | . <> 6 | . <> 7 | . <> 8 | . <> 9 | . <> 10 | . <> 11 | . <> 12 | . <> 13 | 14 | <> 15 | -------------------------------------------------------------------------------- /docs/src/main/asciidoc/leetcode.adoc: -------------------------------------------------------------------------------- 1 | = LeetCode 2 | 3 | *Site:* https://leetcode.com/ 4 | 5 | *My Solutions:* link:java/src/main/java/leetcode[https://github.com/jaredsburrows/cs-interview-questions/tree/master/java/src/main/java/leetcode] 6 | 7 | *My Profile:* https://leetcode.com/jaredsburrows/ 8 | -------------------------------------------------------------------------------- /docs/src/main/asciidoc/projecteuler.adoc: -------------------------------------------------------------------------------- 1 | = ProjectEuler 2 | 3 | *Site:* https://projecteuler.net/ 4 | 5 | *My Solutions:* link:java/rc/main/java/projecteuler[https://github.com/jaredsburrows/cs-interview-questions/tree/master/java/src/main/java/projecteuler] 6 | 7 | *My Profile:* 8 | 9 | image::https://projecteuler.net/profile/jaredsburrows.png[Profile] 10 | -------------------------------------------------------------------------------- /docs/src/main/asciidoc/questions.adoc: -------------------------------------------------------------------------------- 1 | = Questions 2 | 3 | *My Solutions:* link:java/src/main/java/questions[https://github.com/jaredsburrows/cs-interview-questions/tree/master/java/src/main/java/questions] 4 | -------------------------------------------------------------------------------- /docs/src/main/asciidoc/stackoverflow.adoc: -------------------------------------------------------------------------------- 1 | = StackOverflow 2 | 3 | *My Profile:* https://stackoverflow.com/users/950427/jared-burrows 4 | 5 | image::https://stackexchange.com/users/flair/918082.png[Profile] 6 | -------------------------------------------------------------------------------- /docs/src/main/asciidoc/tests.adoc: -------------------------------------------------------------------------------- 1 | = Tests 2 | 3 | Here is the output of the tests: link:[https://jaredsburrows.github.io/cs-interview-questions/tests/] 4 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jaredsburrows/cs-interview-questions/822346d7e77c1d14d3f1a9bcee0a1450a94c0647/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionBase=GRADLE_USER_HOME 2 | distributionPath=wrapper/dists 3 | distributionUrl=https\://services.gradle.org/distributions/gradle-8.14-all.zip 4 | networkTimeout=10000 5 | validateDistributionUrl=true 6 | zipStoreBase=GRADLE_USER_HOME 7 | zipStorePath=wrapper/dists 8 | -------------------------------------------------------------------------------- /java/src/main/java/api/Color.java: -------------------------------------------------------------------------------- 1 | package api; 2 | 3 | public enum Color { 4 | GREEN, 5 | RED, 6 | BLUE 7 | } 8 | -------------------------------------------------------------------------------- /java/src/main/java/api/DoublyLinkedList.java: -------------------------------------------------------------------------------- 1 | package api; 2 | 3 | public final class DoublyLinkedList { 4 | private Node head; 5 | private Node tail; 6 | 7 | public void setHead(Node node) { 8 | if (node == null) { 9 | } 10 | 11 | 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /java/src/main/java/api/util/Swap.java: -------------------------------------------------------------------------------- 1 | package api.util; 2 | 3 | public class Swap { 4 | // Time - O(1), Space - O(1) 5 | public static void swap(T[] array, int left, int right) { 6 | T temp = array[left]; 7 | array[left] = array[right]; 8 | array[right] = temp; 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /java/src/main/java/architecture/mvp/Contract.java: -------------------------------------------------------------------------------- 1 | package architecture.mvp; 2 | 3 | public interface Contract { 4 | // Activity/Fragment/View 5 | interface View { 6 | void setPresenter(Presenter presenter); 7 | 8 | void showEmptyTaskError(); 9 | 10 | void showEmptyTaskSuccess(); 11 | 12 | void setTitle(String title); 13 | } 14 | 15 | // 1 per Activity/Fragment/View 16 | interface Presenter { 17 | boolean isDataMissing(); 18 | 19 | void saveTask(String title); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /java/src/main/java/architecture/mvp/PresenterImpl.java: -------------------------------------------------------------------------------- 1 | package architecture.mvp; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | public class PresenterImpl implements Contract.Presenter { 7 | private final List tasks = new ArrayList<>(); 8 | private final Contract.View view; 9 | 10 | public PresenterImpl(Contract.View view) { 11 | this.view = view; 12 | 13 | view.setPresenter(this); 14 | } 15 | 16 | @Override 17 | public boolean isDataMissing() { 18 | return tasks.isEmpty(); 19 | } 20 | 21 | 22 | @Override 23 | public void saveTask(String title) { 24 | tasks.add(title); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /java/src/main/java/architecture/mvp/ViewImpl.java: -------------------------------------------------------------------------------- 1 | package architecture.mvp; 2 | 3 | public class ViewImpl implements Contract.View { 4 | private Contract.Presenter presenter; 5 | private String title; 6 | 7 | @Override 8 | public void setPresenter(Contract.Presenter presenter) { 9 | this.presenter = presenter; 10 | } 11 | 12 | @Override 13 | public void showEmptyTaskError() { 14 | System.out.println("Error!"); 15 | } 16 | 17 | @Override 18 | public void showEmptyTaskSuccess() { 19 | System.out.println("Success!"); 20 | } 21 | 22 | @Override 23 | public void setTitle(String title) { 24 | this.title = title; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /java/src/main/java/codeeval/easy/agedistribution/input.txt: -------------------------------------------------------------------------------- 1 | 0 2 | 19 3 | -------------------------------------------------------------------------------- /java/src/main/java/codeeval/easy/armstrongnumbers/input.txt: -------------------------------------------------------------------------------- 1 | 6 2 | 153 3 | 351 4 | -------------------------------------------------------------------------------- /java/src/main/java/codeeval/easy/bitpositions/input.txt: -------------------------------------------------------------------------------- 1 | 86,2,3 2 | 125,1,2 3 | 86,2,3 4 | 125,1,2 5 | -------------------------------------------------------------------------------- /java/src/main/java/codeeval/easy/calculatedistance/input.txt: -------------------------------------------------------------------------------- 1 | (25, 4) (1, -6) 2 | (47, 43) (-25, -11) 3 | -------------------------------------------------------------------------------- /java/src/main/java/codeeval/easy/capitalizewords/input.txt: -------------------------------------------------------------------------------- 1 | Hello world 2 | javaScript language 3 | a letter 4 | 1st thing 5 | -------------------------------------------------------------------------------- /java/src/main/java/codeeval/easy/compressedsequence/input.txt: -------------------------------------------------------------------------------- 1 | 40 40 40 40 29 29 29 29 29 29 29 29 57 57 92 92 92 92 92 86 86 86 86 86 86 86 86 86 86 2 | 73 73 73 73 41 41 41 41 41 41 41 41 41 41 3 | 1 1 3 3 3 2 2 2 2 14 14 14 11 11 11 2 4 | 7 5 | -------------------------------------------------------------------------------- /java/src/main/java/codeeval/easy/datarecovery/input.txt: -------------------------------------------------------------------------------- 1 | 2000 and was not However, implemented 1998 it until;9 8 3 4 1 5 7 2 2 | programming first The language;3 2 1 3 | programs Manchester The written ran Mark 1952 1 in Autocode from;6 2 1 7 5 3 11 4 8 9 4 | -------------------------------------------------------------------------------- /java/src/main/java/codeeval/easy/deltatime/input.txt: -------------------------------------------------------------------------------- 1 | 14:01:57 12:47:11 2 | 13:09:42 22:16:15 3 | 08:08:06 08:38:28 4 | 23:35:07 02:49:59 5 | 14:31:45 14:46:56 6 | -------------------------------------------------------------------------------- /java/src/main/java/codeeval/easy/evennumbers/input.txt: -------------------------------------------------------------------------------- 1 | 701 2 | 4123 3 | 2936 4 | -------------------------------------------------------------------------------- /java/src/main/java/codeeval/easy/hiddendigits/input.txt: -------------------------------------------------------------------------------- 1 | abcdefghik 2 | Xa,}A#5N}{xOBwYBHIlH,#W 3 | (ABW>'yy^'M{X-K}q, 4 | 6240488 5 | -------------------------------------------------------------------------------- /java/src/main/java/codeeval/easy/lettercasepercentageratio/input.txt: -------------------------------------------------------------------------------- 1 | thisTHIS 2 | AAbbCCDDEE 3 | N 4 | UkJ 5 | -------------------------------------------------------------------------------- /java/src/main/java/codeeval/easy/longestword/input.txt: -------------------------------------------------------------------------------- 1 | some line with text 2 | another line 3 | -------------------------------------------------------------------------------- /java/src/main/java/codeeval/easy/lowestuniquenumber/input.txt: -------------------------------------------------------------------------------- 1 | 3 3 9 1 6 5 8 1 5 3 2 | 9 2 9 9 1 8 8 8 2 1 1 3 | -------------------------------------------------------------------------------- /java/src/main/java/codeeval/easy/matrixrotation/input.txt: -------------------------------------------------------------------------------- 1 | a b c d 2 | a b c d e f g h i j k l m n o p 3 | a b c d e f g h i 4 | -------------------------------------------------------------------------------- /java/src/main/java/codeeval/easy/mixedcontent/input.txt: -------------------------------------------------------------------------------- 1 | 8,33,21,0,16,50,37,0,melon,7,apricot,peach,pineapple,17,21 2 | 24,13,14,43,41 3 | 8,33,21,0,16,50,37,0,melon,7,apricot,peach,pineapple,17,21 4 | 24,13,14,43,41 5 | 8,33,21,0,16,50,37,0 6 | melon,apricot,peach,pineapple 7 | -------------------------------------------------------------------------------- /java/src/main/java/codeeval/easy/morsecode/input.txt: -------------------------------------------------------------------------------- 1 | .- ...- ..--- .-- .... .. . -.-. -..- ....- ..... 2 | -... .... ...-- 3 | -------------------------------------------------------------------------------- /java/src/main/java/codeeval/easy/multiplylists/input.txt: -------------------------------------------------------------------------------- 1 | 9 0 6 | 15 14 9 2 | 5 | 8 3 | 13 4 15 1 15 5 | 1 4 15 14 8 2 4 | -------------------------------------------------------------------------------- /java/src/main/java/codeeval/easy/readmore/input.txt: -------------------------------------------------------------------------------- 1 | Tom exhibited. 2 | Amy Lawrence was proud and glad, and she tried to make Tom see it in her face - but he wouldn't look. 3 | Tom was tugging at a button-hole and looking sheepish. 4 | Two thousand verses is a great many - very, very great many. 5 | Tom's mouth watered for the apple, but he stuck to his work. 6 | 123456789A123456789B123456789C123456789D123456789E12345 7 | 123456789A123456789B123456789C123456789D123456789E123 5 8 | 123456789A123456789B123456789C123456789D123456789E123456 9 | -------------------------------------------------------------------------------- /java/src/main/java/codeeval/easy/roadtrip/input.txt: -------------------------------------------------------------------------------- 1 | Rkbs,5453; Wdqiz,1245; Rwds,3890; Ujma,5589; Tbzmo,1303; 2 | Vgdfz,70; Mgknxpi,3958; Nsptghk,2626; Wuzp,2559; Jcdwi,3761; 3 | Yvnzjwk,5363; Pkabj,5999; Xznvb,3584; Jfksvx,1240; Inwm,5720; 4 | Ramytdb,2683; Voclqmb,5236; 5 | -------------------------------------------------------------------------------- /java/src/main/java/codeeval/easy/rollercoaster/input.txt: -------------------------------------------------------------------------------- 1 | To be, or not to be: that is the question. 2 | Whether 'tis nobler in the mind to suffer. 3 | The slings and arrows of outrageous fortune. 4 | Or to take arms against a sea of troubles. 5 | And by opposing end them, to die: to sleep. 6 | -------------------------------------------------------------------------------- /java/src/main/java/codeeval/easy/romannumerals/input.txt: -------------------------------------------------------------------------------- 1 | 159 2 | 296 3 | 3992 4 | -------------------------------------------------------------------------------- /java/src/main/java/codeeval/easy/sumofdigits/Main.java: -------------------------------------------------------------------------------- 1 | package codeeval.easy.sumofdigits; 2 | 3 | import java.io.File; 4 | import java.nio.charset.Charset; 5 | import java.util.Scanner; 6 | 7 | public final class Main { 8 | public static void main(String[] args) throws Exception { 9 | Scanner scanner = new Scanner(new File(args[0]), Charset.defaultCharset()); 10 | 11 | while (scanner.hasNext()) { 12 | int sum = 0; 13 | int number = scanner.nextInt(); 14 | while (number != 0) { 15 | sum = sum + (number % 10); 16 | number = number / 10; 17 | } 18 | System.out.println(sum); 19 | } 20 | } 21 | } 22 | 23 | -------------------------------------------------------------------------------- /java/src/main/java/codeeval/easy/sumofdigits/input.txt: -------------------------------------------------------------------------------- 1 | 23 2 | 496 3 | -------------------------------------------------------------------------------- /java/src/main/java/codeeval/easy/swapcase/input.txt: -------------------------------------------------------------------------------- 1 | Hello world! 2 | JavaScript language 1.8 3 | A letter 4 | -------------------------------------------------------------------------------- /java/src/main/java/codeeval/easy/themajorelement/input.txt: -------------------------------------------------------------------------------- 1 | 92,19,19,76,19,21,19,85,19,19,19,94,19,19,22,67,83,19,19,54,59,1,19,19 2 | 92,11,30,92,1,11,92,38,92,92,43,92,92,51,92,36,97,92,92,92,43,22,84,92,92 3 | 4,79,89,98,48,42,39,79,55,70,21,39,98,16,96,2,10,24,14,47,0,50,95,20,95,48,50,12,42 4 | -------------------------------------------------------------------------------- /java/src/main/java/codeeval/easy/uniqueelements/input.txt: -------------------------------------------------------------------------------- 1 | 1,1,1,2,2,3,3,4,4 2 | 2,3,4,5,5 3 | 21,31,41,51,51 4 | -------------------------------------------------------------------------------- /java/src/main/java/codeeval/easy/withoutrepetitions/input.txt: -------------------------------------------------------------------------------- 1 | But as he spake he drew the good sword from its scabbard, and smote a heathen knight, Jusssstin of thee Iron Valley. 2 | No matttter whom you choose, she deccccclared, I will abide by your decision. 3 | Wwwhat is your will? 4 | At his magic speech the ground oppened and he began the path of descent. 5 | I should fly away and you would never see me again. 6 | -------------------------------------------------------------------------------- /java/src/main/java/codeeval/easy/wordtodigit/input.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jaredsburrows/cs-interview-questions/822346d7e77c1d14d3f1a9bcee0a1450a94c0647/java/src/main/java/codeeval/easy/wordtodigit/input.txt -------------------------------------------------------------------------------- /java/src/main/java/coderbyte/FirstFactorial.java: -------------------------------------------------------------------------------- 1 | package coderbyte; 2 | 3 | /** 4 | * https://www.coderbyte.com/editor/guest:First%20Factorial:Java 5 | */ 6 | public final class FirstFactorial { 7 | public static int firstFactorial(int num) { 8 | int fact = 1; 9 | for (int i = 2; i <= num; i++) { 10 | fact *= i; 11 | } 12 | 13 | return fact; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /java/src/main/java/codingbat/ap1/BigHeights.java: -------------------------------------------------------------------------------- 1 | package codingbat.ap1; 2 | 3 | /** 4 | * https://codingbat.com/prob/p197710 5 | */ 6 | public final class BigHeights { 7 | public int bigHeights(int[] heights, int start, int end) { 8 | int sum = 0; 9 | for (int i = start; i < end; i++) { 10 | sum += Math.abs(heights[i + 1] - heights[i]) >= 5 ? 1 : 0; 11 | } 12 | return sum; 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /java/src/main/java/codingbat/ap1/CommonTwo.java: -------------------------------------------------------------------------------- 1 | package codingbat.ap1; 2 | 3 | import java.util.Arrays; 4 | import java.util.HashSet; 5 | import java.util.Set; 6 | 7 | /** 8 | * https://codingbat.com/prob/p100369 9 | */ 10 | public final class CommonTwo { 11 | public int commonTwo(String[] a, String[] b) { 12 | Set a1 = new HashSet<>(Arrays.asList(a)); 13 | Set b1 = new HashSet<>(Arrays.asList(b)); 14 | 15 | int count = 0; 16 | for (String s : a1) { 17 | for (String value : b1) { 18 | if (s.equals(value)) { 19 | count++; 20 | } 21 | } 22 | } 23 | 24 | return count; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /java/src/main/java/codingbat/ap1/CopyEndy.java: -------------------------------------------------------------------------------- 1 | package codingbat.ap1; 2 | 3 | /** 4 | * https://codingbat.com/prob/p130124 5 | */ 6 | public final class CopyEndy { 7 | public int[] copyEndy(int[] nums, int count) { 8 | int[] test = new int[count]; 9 | int copy = 0; 10 | for (int num : nums) { 11 | if ((num >= 0 && num <= 10) || (num >= 90 && num <= 100)) { 12 | test[copy++] = num; 13 | if (copy == count) { 14 | break; 15 | } 16 | } 17 | } 18 | 19 | return test; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /java/src/main/java/codingbat/ap1/CopyEvens.java: -------------------------------------------------------------------------------- 1 | package codingbat.ap1; 2 | 3 | /** 4 | * https://codingbat.com/prob/p134174 5 | */ 6 | public final class CopyEvens { 7 | public int[] copyEvens(int[] nums, int count) { 8 | int[] array = new int[count]; 9 | int evens = 0; 10 | 11 | for (int num : nums) { 12 | if (evens < count && num % 2 == 0) { 13 | array[evens] = num; 14 | evens++; 15 | } 16 | } 17 | 18 | return array; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /java/src/main/java/codingbat/ap1/DividesSelf.java: -------------------------------------------------------------------------------- 1 | package codingbat.ap1; 2 | 3 | /** 4 | * https://codingbat.com/prob/p165941 5 | */ 6 | public final class DividesSelf { 7 | public boolean dividesSelf(int n) { 8 | int remainder = 0; 9 | int temp = 0; 10 | 11 | temp = n; 12 | 13 | while (n != 0) { 14 | remainder = n % 10; 15 | if (remainder == 0) { 16 | return false; 17 | } 18 | 19 | if (temp % remainder != 0) { 20 | return false; 21 | } 22 | n = n / 10; 23 | } 24 | 25 | return true; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /java/src/main/java/codingbat/ap1/HasOne.java: -------------------------------------------------------------------------------- 1 | package codingbat.ap1; 2 | 3 | /** 4 | * https://codingbat.com/prob/p191212 5 | */ 6 | public final class HasOne { 7 | public boolean hasOne(int n) { 8 | String number = String.valueOf(n); 9 | char[] temp = number.toCharArray(); 10 | for (int i = 0; i < number.length(); i++) { 11 | if (temp[i] == '1') { 12 | return true; 13 | } 14 | } 15 | 16 | return false; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /java/src/main/java/codingbat/ap1/MatchUp.java: -------------------------------------------------------------------------------- 1 | package codingbat.ap1; 2 | 3 | /** 4 | * https://codingbat.com/prob/p139677 5 | */ 6 | public final class MatchUp { 7 | public int matchUp(String[] a, String[] b) { 8 | int count = 0; 9 | for (int i = 0; i < a.length; i++) { 10 | if (!a[i].isEmpty() 11 | && !b[i].isEmpty() 12 | && a[i].charAt(0) == b[i].charAt(0)) { 13 | count++; 14 | } 15 | } 16 | 17 | return count; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /java/src/main/java/codingbat/ap1/ScoreUp.java: -------------------------------------------------------------------------------- 1 | package codingbat.ap1; 2 | 3 | /** 4 | * https://codingbat.com/prob/p180365 5 | */ 6 | public final class ScoreUp { 7 | public int scoreUp(String[] key, String[] answers) { 8 | int score = 0; 9 | for (int i = 0; i < key.length; i++) { 10 | if (key[i].equals(answers[i])) { 11 | score += 4; 12 | } else if (answers[i].equals("?")) { 13 | // no op 14 | } else if (!key[i].equals(answers[i])) { 15 | score--; 16 | } 17 | } 18 | return score; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /java/src/main/java/codingbat/ap1/Scores100.java: -------------------------------------------------------------------------------- 1 | package codingbat.ap1; 2 | 3 | /** 4 | * https://codingbat.com/prob/p179487 5 | */ 6 | public final class Scores100 { 7 | public boolean scores100(int[] scores) { 8 | for (int i = 0; i <= scores.length; i++) { 9 | if (i + 1 < scores.length && scores[i] == 100 && scores[i + 1] == 100) { 10 | return true; 11 | } 12 | } 13 | 14 | return false; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /java/src/main/java/codingbat/ap1/ScoresAverage.java: -------------------------------------------------------------------------------- 1 | package codingbat.ap1; 2 | 3 | /** 4 | * https://codingbat.com/prob/p123837 5 | */ 6 | public final class ScoresAverage { 7 | public int scoresAverage(int[] scores) { 8 | int sum = 0; 9 | int sum2 = 0; 10 | int length = scores.length / 2; 11 | for (int i = 0; i < scores.length; i++) { 12 | if (i < length) { 13 | sum += scores[i]; 14 | } 15 | if (i >= length) { 16 | sum2 += scores[i]; 17 | } 18 | } 19 | 20 | return sum > sum2 ? sum / length : sum2 / length; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /java/src/main/java/codingbat/ap1/ScoresClump.java: -------------------------------------------------------------------------------- 1 | package codingbat.ap1; 2 | 3 | /** 4 | * https://codingbat.com/prob/p194530 5 | */ 6 | public final class ScoresClump { 7 | public boolean scoresClump(int[] scores) { 8 | for (int i = 0; i < scores.length; i++) { 9 | if (i + 2 < scores.length && scores[i + 2] - scores[i] <= 2) { 10 | return true; 11 | } 12 | } 13 | return false; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /java/src/main/java/codingbat/ap1/ScoresIncreasing.java: -------------------------------------------------------------------------------- 1 | package codingbat.ap1; 2 | 3 | /** 4 | * https://codingbat.com/prob/p146974 5 | */ 6 | public final class ScoresIncreasing { 7 | public boolean scoresIncreasing(int[] scores) { 8 | for (int i = 0; i < scores.length; i++) { 9 | if (i + 1 < scores.length && !(scores[i] <= scores[i + 1])) { 10 | return false; 11 | } 12 | } 13 | return true; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /java/src/main/java/codingbat/ap1/SumHeights.java: -------------------------------------------------------------------------------- 1 | package codingbat.ap1; 2 | 3 | /** 4 | * https://codingbat.com/prob/p148138 5 | */ 6 | public final class SumHeights { 7 | public int sumHeights(int[] heights, int start, int end) { 8 | int sum = 0; 9 | for (int i = start; i < end; i++) { 10 | sum += Math.abs(heights[i] - heights[i + 1]); 11 | } 12 | return sum; 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /java/src/main/java/codingbat/ap1/SumHeights2.java: -------------------------------------------------------------------------------- 1 | package codingbat.ap1; 2 | 3 | /** 4 | * https://codingbat.com/prob/p157900 5 | */ 6 | public final class SumHeights2 { 7 | public int sumHeights2(int[] heights, int start, int end) { 8 | int sum = 0; 9 | for (int i = start; i < end; i++) { 10 | sum += heights[i + 1] - heights[i] > 0 11 | ? Math.abs(heights[i] - heights[i + 1]) * 2 12 | : Math.abs(heights[i] - heights[i + 1]); 13 | } 14 | return sum; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /java/src/main/java/codingbat/ap1/UserCompare.java: -------------------------------------------------------------------------------- 1 | package codingbat.ap1; 2 | 3 | /** 4 | * https://codingbat.com/prob/p143482 5 | */ 6 | public final class UserCompare { 7 | public int userCompare(String aName, int aId, String bName, int bId) { 8 | int compare = aName.compareTo(bName); 9 | if (compare > 0) { 10 | return 1; 11 | } 12 | 13 | if (compare == 0) { 14 | return Integer.compare(aId, bId); 15 | } 16 | 17 | return -1; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /java/src/main/java/codingbat/ap1/WordsCount.java: -------------------------------------------------------------------------------- 1 | package codingbat.ap1; 2 | 3 | /** 4 | * https://codingbat.com/prob/p124620 5 | */ 6 | public final class WordsCount { 7 | public int wordsCount(String[] words, int len) { 8 | int count = 0; 9 | 10 | for (String word : words) { 11 | if (word.length() == len) { 12 | count++; 13 | } 14 | } 15 | 16 | return count; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /java/src/main/java/codingbat/ap1/WordsFront.java: -------------------------------------------------------------------------------- 1 | package codingbat.ap1; 2 | 3 | /** 4 | * https://codingbat.com/prob/p183837 5 | */ 6 | public final class WordsFront { 7 | public String[] wordsFront(String[] words, int n) { 8 | String[] temp = new String[n]; 9 | 10 | for (int i = 0; i < n; i++) { 11 | if (i < words.length) { 12 | temp[i] = words[i]; 13 | } 14 | } 15 | 16 | return temp; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /java/src/main/java/codingbat/ap1/WordsWithout.java: -------------------------------------------------------------------------------- 1 | package codingbat.ap1; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | /** 7 | * https://codingbat.com/prob/p121236 8 | */ 9 | public final class WordsWithout { 10 | public String[] wordsWithout(String[] words, String target) { 11 | List test = new ArrayList<>(); 12 | for (String word : words) { 13 | if (!word.equals(target)) { 14 | test.add(word); 15 | } 16 | } 17 | 18 | return test.toArray(new String[0]); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /java/src/main/java/codingbat/ap1/WordsWithoutList.java: -------------------------------------------------------------------------------- 1 | package codingbat.ap1; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | /** 7 | * https://codingbat.com/prob/p183407 8 | */ 9 | public final class WordsWithoutList { 10 | public List wordsWithoutList(String[] words, int len) { 11 | List temp = new ArrayList<>(len); 12 | 13 | for (String word : words) { 14 | if (word.length() != len) { 15 | temp.add(word); 16 | } 17 | } 18 | 19 | return temp; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /java/src/main/java/codingbat/functional1/AddStar.java: -------------------------------------------------------------------------------- 1 | package codingbat.functional1; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | import java.util.stream.Collectors; 6 | 7 | /** 8 | * https://codingbat.com/prob/p170181 9 | */ 10 | public final class AddStar { 11 | public List addStar(List strings) { 12 | return strings.stream() 13 | .map(n -> n + "*") 14 | .collect(Collectors.toCollection(ArrayList::new)); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /java/src/main/java/codingbat/functional1/Copies3.java: -------------------------------------------------------------------------------- 1 | package codingbat.functional1; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | import java.util.stream.Collectors; 6 | 7 | /** 8 | * https://codingbat.com/prob/p181634 9 | */ 10 | public final class Copies3 { 11 | public List copies3(List strings) { 12 | return strings.stream() 13 | .map(n -> n + n + n) 14 | .collect(Collectors.toCollection(ArrayList::new)); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /java/src/main/java/codingbat/functional1/Doubling.java: -------------------------------------------------------------------------------- 1 | package codingbat.functional1; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | import java.util.stream.Collectors; 6 | 7 | /** 8 | * https://codingbat.com/prob/p117665 9 | */ 10 | public final class Doubling { 11 | public List doubling(List nums) { 12 | return nums.stream().map(n -> n * 2).collect(Collectors.toCollection(ArrayList::new)); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /java/src/main/java/codingbat/functional1/Lower.java: -------------------------------------------------------------------------------- 1 | package codingbat.functional1; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | import java.util.Locale; 6 | import java.util.stream.Collectors; 7 | 8 | /** 9 | * https://codingbat.com/prob/p186894 10 | */ 11 | public final class Lower { 12 | public List lower(List strings) { 13 | return strings.stream() 14 | .map(n -> n.toLowerCase(Locale.getDefault())) 15 | .collect(Collectors.toCollection(ArrayList::new)); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /java/src/main/java/codingbat/functional1/Math1.java: -------------------------------------------------------------------------------- 1 | package codingbat.functional1; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | import java.util.stream.Collectors; 6 | 7 | /** 8 | * https://codingbat.com/prob/p103869 9 | */ 10 | public final class Math1 { 11 | public List math1(List nums) { 12 | return nums.stream() 13 | .map(n -> (n + 1) * 10) 14 | .collect(Collectors.toCollection(ArrayList::new)); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /java/src/main/java/codingbat/functional1/MoreY.java: -------------------------------------------------------------------------------- 1 | package codingbat.functional1; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | import java.util.stream.Collectors; 6 | 7 | /** 8 | * https://codingbat.com/prob/p177528 9 | */ 10 | public final class MoreY { 11 | public List moreY(List strings) { 12 | return strings.stream() 13 | .map(n -> "y" + n + "y") 14 | .collect(Collectors.toCollection(ArrayList::new)); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /java/src/main/java/codingbat/functional1/NoX.java: -------------------------------------------------------------------------------- 1 | package codingbat.functional1; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | import java.util.stream.Collectors; 6 | 7 | /** 8 | * https://codingbat.com/prob/p105967 9 | */ 10 | public final class NoX { 11 | public List noX(List strings) { 12 | return strings.stream() 13 | .map(n -> n.replaceAll("x", "")) 14 | .collect(Collectors.toCollection(ArrayList::new)); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /java/src/main/java/codingbat/functional1/RightDigit.java: -------------------------------------------------------------------------------- 1 | package codingbat.functional1; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | import java.util.stream.Collectors; 6 | 7 | /** 8 | * https://codingbat.com/prob/p152194 9 | */ 10 | public final class RightDigit { 11 | public List rightDigit(List nums) { 12 | return nums.stream() 13 | .map(n -> n % 10) 14 | .collect(Collectors.toCollection(ArrayList::new)); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /java/src/main/java/codingbat/functional1/Square.java: -------------------------------------------------------------------------------- 1 | package codingbat.functional1; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | import java.util.stream.Collectors; 6 | 7 | /** 8 | * https://codingbat.com/prob/p139586 9 | */ 10 | public final class Square { 11 | public List square(List nums) { 12 | return nums.stream() 13 | .map(n -> n * n) 14 | .collect(Collectors.toCollection(ArrayList::new)); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /java/src/main/java/codingbat/functional2/NoNeg.java: -------------------------------------------------------------------------------- 1 | package codingbat.functional2; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | import java.util.stream.Collectors; 6 | 7 | /** 8 | * https://codingbat.com/prob/p103456 9 | */ 10 | public final class NoNeg { 11 | public List noNeg(List nums) { 12 | return nums.stream() 13 | .filter(n -> n > 0 || n == 0) 14 | .collect(Collectors.toCollection(ArrayList::new)); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /java/src/main/java/codingbat/functional2/NoZ.java: -------------------------------------------------------------------------------- 1 | package codingbat.functional2; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | import java.util.stream.Collectors; 6 | 7 | /** 8 | * https://codingbat.com/prob/p105671 9 | */ 10 | public final class NoZ { 11 | public List noZ(List strings) { 12 | return strings.stream() 13 | .filter(s -> !s.contains("z")) 14 | .collect(Collectors.toCollection(ArrayList::new)); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /java/src/main/java/codingbat/map1/MapAB.java: -------------------------------------------------------------------------------- 1 | package codingbat.map1; 2 | 3 | import java.util.HashMap; 4 | import java.util.Map; 5 | 6 | /** 7 | * https://codingbat.com/prob/p107259 8 | */ 9 | public final class MapAB { 10 | public Map mapAB(Map map) { 11 | if (map == null) { 12 | return new HashMap<>(); 13 | } 14 | 15 | if (map.containsKey("a") && map.containsKey("b")) { 16 | map.put("ab", map.get("a") + map.get("b")); 17 | } 18 | 19 | return map; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /java/src/main/java/codingbat/map1/MapAB2.java: -------------------------------------------------------------------------------- 1 | package codingbat.map1; 2 | 3 | import java.util.HashMap; 4 | import java.util.Map; 5 | 6 | /** 7 | * https://codingbat.com/prob/p115011 8 | */ 9 | public final class MapAB2 { 10 | public Map mapAB2(Map map) { 11 | if (map == null) { 12 | return new HashMap<>(); 13 | } 14 | 15 | if (map.containsKey("a") 16 | && map.containsKey("b") 17 | && map.get("a").equals(map.get("b"))) { 18 | map.remove("a"); 19 | map.remove("b"); 20 | } 21 | 22 | return map; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /java/src/main/java/codingbat/map1/MapAB3.java: -------------------------------------------------------------------------------- 1 | package codingbat.map1; 2 | 3 | import java.util.HashMap; 4 | import java.util.Map; 5 | 6 | /** 7 | * https://codingbat.com/prob/p115012 8 | */ 9 | public final class MapAB3 { 10 | public Map mapAB3(Map map) { 11 | if (map == null) { 12 | return new HashMap<>(); 13 | } 14 | 15 | if (map.containsKey("a") && !map.containsKey("b")) { 16 | map.put("b", map.get("a")); 17 | } else if (map.containsKey("b") && !map.containsKey("a")) { 18 | map.put("a", map.get("b")); 19 | } 20 | 21 | return map; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /java/src/main/java/codingbat/map1/MapBully.java: -------------------------------------------------------------------------------- 1 | package codingbat.map1; 2 | 3 | import java.util.HashMap; 4 | import java.util.Map; 5 | 6 | /** 7 | * https://codingbat.com/prob/p197888 8 | */ 9 | public final class MapBully { 10 | public Map mapBully(Map map) { 11 | if (map == null) { 12 | return new HashMap<>(); 13 | } 14 | 15 | if (map.containsKey("a")) { 16 | map.put("b", map.get("a")); 17 | map.put("a", ""); 18 | } 19 | 20 | return map; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /java/src/main/java/codingbat/map1/MapShare.java: -------------------------------------------------------------------------------- 1 | package codingbat.map1; 2 | 3 | import java.util.HashMap; 4 | import java.util.Map; 5 | 6 | /** 7 | * https://codingbat.com/prob/p148813 8 | */ 9 | public final class MapShare { 10 | public Map mapShare(Map map) { 11 | if (map == null) { 12 | return new HashMap<>(); 13 | } 14 | 15 | if (map.containsKey("a")) { 16 | map.put("b", map.get("a")); 17 | } 18 | 19 | map.remove("c"); 20 | 21 | return map; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /java/src/main/java/codingbat/map1/Topping1.java: -------------------------------------------------------------------------------- 1 | package codingbat.map1; 2 | 3 | import java.util.HashMap; 4 | import java.util.Map; 5 | 6 | /** 7 | * https://codingbat.com/prob/p182712 8 | */ 9 | public final class Topping1 { 10 | public Map topping1(Map map) { 11 | if (map == null) { 12 | return new HashMap<>(); 13 | } 14 | 15 | if (map.containsKey("ice cream")) { 16 | map.put("ice cream", "cherry"); 17 | } 18 | 19 | map.put("bread", "butter"); 20 | 21 | return map; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /java/src/main/java/codingbat/map1/Topping2.java: -------------------------------------------------------------------------------- 1 | package codingbat.map1; 2 | 3 | import java.util.HashMap; 4 | import java.util.Map; 5 | 6 | /** 7 | * https://codingbat.com/prob/p196458 8 | */ 9 | public final class Topping2 { 10 | public Map topping2(Map map) { 11 | if (map == null) { 12 | return new HashMap<>(); 13 | } 14 | 15 | if (map.containsKey("ice cream")) { 16 | map.put("yogurt", map.get("ice cream")); 17 | } 18 | 19 | if (map.containsKey("spinach")) { 20 | map.put("spinach", "nuts"); 21 | } 22 | 23 | return map; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /java/src/main/java/codingbat/map1/Topping3.java: -------------------------------------------------------------------------------- 1 | package codingbat.map1; 2 | 3 | import java.util.HashMap; 4 | import java.util.Map; 5 | 6 | /** 7 | * https://codingbat.com/prob/p128461 8 | */ 9 | public final class Topping3 { 10 | public Map topping3(Map map) { 11 | if (map == null) { 12 | return new HashMap<>(); 13 | } 14 | 15 | if (map.containsKey("potato")) { 16 | map.put("fries", map.get("potato")); 17 | } 18 | 19 | if (map.containsKey("salad")) { 20 | map.put("spinach", map.get("salad")); 21 | } 22 | 23 | return map; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /java/src/main/java/codingbat/map2/FirstChar.java: -------------------------------------------------------------------------------- 1 | package codingbat.map2; 2 | 3 | import java.util.HashMap; 4 | import java.util.Map; 5 | 6 | /** 7 | * https://codingbat.com/prob/p168493 8 | */ 9 | public final class FirstChar { 10 | public Map firstChar(String[] strings) { 11 | Map map = new HashMap<>(); 12 | if (strings == null) { 13 | return map; 14 | } 15 | 16 | for (String string : strings) { 17 | String character = String.valueOf(string.charAt(0)); 18 | map.put(character, map.getOrDefault(character, "") + string); 19 | } 20 | 21 | return map; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /java/src/main/java/codingbat/map2/Pairs.java: -------------------------------------------------------------------------------- 1 | package codingbat.map2; 2 | 3 | import java.util.HashMap; 4 | import java.util.Map; 5 | 6 | /** 7 | * https://codingbat.com/prob/p126332 8 | */ 9 | public final class Pairs { 10 | public Map pairs(String[] strings) { 11 | Map map = new HashMap<>(); 12 | if (strings == null) { 13 | return map; 14 | } 15 | 16 | for (String string : strings) { 17 | map.put(String.valueOf(string.charAt(0)), 18 | String.valueOf(string.charAt(string.length() - 1))); 19 | } 20 | 21 | return map; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /java/src/main/java/codingbat/map2/Word0.java: -------------------------------------------------------------------------------- 1 | package codingbat.map2; 2 | 3 | import java.util.HashMap; 4 | import java.util.Map; 5 | 6 | /** 7 | * https://codingbat.com/prob/p152303 8 | */ 9 | public final class Word0 { 10 | public Map word0(String[] strings) { 11 | Map map = new HashMap<>(); 12 | if (strings == null) { 13 | return map; 14 | } 15 | 16 | for (String string : strings) { 17 | map.put(string, 0); 18 | } 19 | 20 | return map; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /java/src/main/java/codingbat/map2/WordCount.java: -------------------------------------------------------------------------------- 1 | package codingbat.map2; 2 | 3 | import java.util.HashMap; 4 | import java.util.Map; 5 | 6 | /** 7 | * https://codingbat.com/prob/p117630 8 | */ 9 | public final class WordCount { 10 | public Map wordCount(String[] strings) { 11 | Map map = new HashMap<>(); 12 | if (strings == null) { 13 | return map; 14 | } 15 | 16 | for (String string : strings) { 17 | map.put(string, map.getOrDefault(string, 0) + 1); 18 | } 19 | 20 | return map; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /java/src/main/java/codingbat/map2/WordLen.java: -------------------------------------------------------------------------------- 1 | package codingbat.map2; 2 | 3 | import java.util.HashMap; 4 | import java.util.Map; 5 | 6 | /** 7 | * https://codingbat.com/prob/p125327 8 | */ 9 | public final class WordLen { 10 | public Map wordLen(String[] strings) { 11 | Map map = new HashMap<>(); 12 | if (strings == null) { 13 | return map; 14 | } 15 | 16 | for (String string : strings) { 17 | map.put(string, string.length()); 18 | } 19 | 20 | return map; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /java/src/main/java/codingbat/recursion1/AllStar.java: -------------------------------------------------------------------------------- 1 | package codingbat.recursion1; 2 | 3 | /** 4 | * https://codingbat.com/prob/p183394 5 | */ 6 | public final class AllStar { 7 | public String allStar(String str) { 8 | if (str == null || str.isEmpty()) { 9 | return ""; 10 | } 11 | 12 | if (str.length() < 2) { 13 | return str.substring(0, 1); 14 | } 15 | 16 | return str.charAt(0) + "*" + allStar(str.substring(1)); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /java/src/main/java/codingbat/recursion1/Array11.java: -------------------------------------------------------------------------------- 1 | package codingbat.recursion1; 2 | 3 | /** 4 | * https://codingbat.com/prob/p135988 5 | */ 6 | public final class Array11 { 7 | public int array11(int[] nums, int index) { 8 | if (nums.length == 0 || nums.length == index) { 9 | return 0; 10 | } 11 | 12 | if (nums[index] == 11) { 13 | return array11(nums, index + 1) + 1; 14 | } else { 15 | return array11(nums, index + 1); 16 | } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /java/src/main/java/codingbat/recursion1/Array6.java: -------------------------------------------------------------------------------- 1 | package codingbat.recursion1; 2 | 3 | /** 4 | * https://codingbat.com/prob/p108997 5 | */ 6 | public final class Array6 { 7 | public boolean array6(int[] nums, int index) { 8 | if (nums == null || nums.length == 0 || nums.length == index) { 9 | return false; 10 | } 11 | 12 | return nums[index] == 6 || array6(nums, index + 1); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /java/src/main/java/codingbat/recursion1/BunnyEars.java: -------------------------------------------------------------------------------- 1 | package codingbat.recursion1; 2 | 3 | /** 4 | * https://codingbat.com/prob/p183649 5 | */ 6 | public final class BunnyEars { 7 | public int bunnyEars(int bunnies) { 8 | if (bunnies == 0) { 9 | return 0; 10 | } 11 | 12 | return bunnyEars(bunnies - 1) + 2; 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /java/src/main/java/codingbat/recursion1/BunnyEars2.java: -------------------------------------------------------------------------------- 1 | package codingbat.recursion1; 2 | 3 | /** 4 | * https://codingbat.com/prob/p107330 5 | */ 6 | public final class BunnyEars2 { 7 | public int bunnyEars2(int bunnies) { 8 | if (bunnies <= 0) { 9 | return 0; 10 | } 11 | 12 | if (bunnies % 2 == 0) { 13 | return bunnyEars2(bunnies - 1) + 3; 14 | } else { 15 | return bunnyEars2(bunnies - 1) + 2; 16 | } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /java/src/main/java/codingbat/recursion1/ChangePi.java: -------------------------------------------------------------------------------- 1 | package codingbat.recursion1; 2 | 3 | /** 4 | * https://codingbat.com/prob/p170924 5 | */ 6 | public final class ChangePi { 7 | public String changePi(String str) { 8 | if (str == null || str.isEmpty()) { 9 | return ""; 10 | } 11 | 12 | if (str.length() >= 2 && "pi".equals(str.substring(0, 2))) { 13 | return "3.14" + changePi(str.substring(2)); 14 | } else { 15 | return str.charAt(0) + changePi(str.substring(1)); 16 | } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /java/src/main/java/codingbat/recursion1/ChangeXY.java: -------------------------------------------------------------------------------- 1 | package codingbat.recursion1; 2 | 3 | /** 4 | * https://codingbat.com/prob/p101372 5 | */ 6 | public final class ChangeXY { 7 | public String changeXY(String str) { 8 | if (str == null || str.isEmpty()) { 9 | return ""; 10 | } 11 | 12 | if ("x".equals(str.substring(0, 1))) { 13 | return "y" + changeXY(str.substring(1)); 14 | } else { 15 | return str.charAt(0) + changeXY(str.substring(1)); 16 | } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /java/src/main/java/codingbat/recursion1/Count11.java: -------------------------------------------------------------------------------- 1 | package codingbat.recursion1; 2 | 3 | /** 4 | * https://codingbat.com/prob/p167015 5 | */ 6 | public final class Count11 { 7 | public int count11(String str) { 8 | if (str == null || str.isEmpty()) { 9 | return 0; 10 | } 11 | 12 | if (str.length() >= 2 && "11".equals(str.substring(0, 2))) { 13 | return count11(str.substring(2)) + 1; 14 | } else { 15 | return count11(str.substring(1)); 16 | } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /java/src/main/java/codingbat/recursion1/Count7.java: -------------------------------------------------------------------------------- 1 | package codingbat.recursion1; 2 | 3 | /** 4 | * https://codingbat.com/prob/p101409 5 | */ 6 | public final class Count7 { 7 | public int count7(int n) { 8 | if (n <= 0) { 9 | return 0; 10 | } 11 | 12 | if (n % 10 == 7) { 13 | return count7(n / 10) + 1; 14 | } else { 15 | return count7(n / 10); 16 | } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /java/src/main/java/codingbat/recursion1/CountAbc.java: -------------------------------------------------------------------------------- 1 | package codingbat.recursion1; 2 | 3 | /** 4 | * https://codingbat.com/prob/p161124 5 | */ 6 | public final class CountAbc { 7 | public int countAbc(String str) { 8 | if (str == null || str.length() < 3) { 9 | return 0; 10 | } 11 | 12 | if ("abc".equals(str.substring(0, 3)) || "aba".equals(str.substring(0, 3))) { 13 | return countAbc(str.substring(1)) + 1; 14 | } else { 15 | return countAbc(str.substring(1)); 16 | } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /java/src/main/java/codingbat/recursion1/CountX.java: -------------------------------------------------------------------------------- 1 | package codingbat.recursion1; 2 | 3 | /** 4 | * https://codingbat.com/prob/p170371 5 | */ 6 | public final class CountX { 7 | public int countX(String str) { 8 | if (str == null || str.isEmpty()) { 9 | return 0; 10 | } 11 | 12 | if ("x".equals(str.substring(str.length() - 1))) { 13 | return countX(str.substring(0, str.length() - 1)) + 1; 14 | } else { 15 | return countX(str.substring(0, str.length() - 1)); 16 | } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /java/src/main/java/codingbat/recursion1/Factorial.java: -------------------------------------------------------------------------------- 1 | package codingbat.recursion1; 2 | 3 | /** 4 | * https://codingbat.com/prob/p154669 5 | */ 6 | public final class Factorial { 7 | public int factorial(int n) { 8 | if (n < 2) { 9 | return 1; 10 | } 11 | 12 | return factorial(n - 1) * n; 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /java/src/main/java/codingbat/recursion1/Fibonacci.java: -------------------------------------------------------------------------------- 1 | package codingbat.recursion1; 2 | 3 | /** 4 | * https://codingbat.com/prob/p120015 5 | */ 6 | public final class Fibonacci { 7 | public int fibonacci(int n) { 8 | if (n < 1) { 9 | return 0; 10 | } 11 | 12 | if (n < 2) { 13 | return 1; 14 | } 15 | 16 | return fibonacci(n - 1) + fibonacci(n - 2); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /java/src/main/java/codingbat/recursion1/NestParen.java: -------------------------------------------------------------------------------- 1 | package codingbat.recursion1; 2 | 3 | /** 4 | * https://codingbat.com/prob/p183174 5 | */ 6 | public final class NestParen { 7 | public boolean nestParen(String str) { 8 | if (str == null) { 9 | return false; 10 | } 11 | 12 | if (str.isEmpty()) { 13 | return true; 14 | } 15 | 16 | if (str.length() >= 2 17 | && "(".equals(str.substring(0, 1)) 18 | && ")".equals(str.substring(str.length() - 1))) { 19 | return nestParen(str.substring(1, str.length() - 1)); 20 | } else { 21 | return false; 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /java/src/main/java/codingbat/recursion1/NoX.java: -------------------------------------------------------------------------------- 1 | package codingbat.recursion1; 2 | 3 | /** 4 | * https://codingbat.com/prob/p118230 5 | */ 6 | public final class NoX { 7 | public String noX(String str) { 8 | if (str == null || str.isEmpty()) { 9 | return ""; 10 | } 11 | 12 | if ("x".equals(str.substring(0, 1))) { 13 | return noX(str.substring(1)); 14 | } else { 15 | return str.charAt(0) + noX(str.substring(1)); 16 | } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /java/src/main/java/codingbat/recursion1/PairStar.java: -------------------------------------------------------------------------------- 1 | package codingbat.recursion1; 2 | 3 | /** 4 | * https://codingbat.com/prob/p158175 5 | */ 6 | public final class PairStar { 7 | public String pairStar(String str) { 8 | if (str == null || str.isEmpty()) { 9 | return ""; 10 | } 11 | 12 | if (str.length() >= 2 && str.substring(0, 1).equals(str.substring(1, 2))) { 13 | return str.charAt(0) + "*" + pairStar(str.substring(1)); 14 | } else { 15 | return str.charAt(0) + pairStar(str.substring(1)); 16 | } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /java/src/main/java/codingbat/recursion1/PowerN.java: -------------------------------------------------------------------------------- 1 | package codingbat.recursion1; 2 | 3 | /** 4 | * https://codingbat.com/prob/p158888 5 | */ 6 | public final class PowerN { 7 | public int powerN(int base, int n) { 8 | if (base <= 0) { 9 | return 0; 10 | } 11 | 12 | if (n <= 0) { 13 | return 1; 14 | } 15 | 16 | return powerN(base, n - 1) * base; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /java/src/main/java/codingbat/recursion1/StrCount.java: -------------------------------------------------------------------------------- 1 | package codingbat.recursion1; 2 | 3 | /** 4 | * https://codingbat.com/prob/p186177 5 | */ 6 | public final class StrCount { 7 | public int strCount(String str, String sub) { 8 | if (str == null 9 | || str.isEmpty() 10 | || sub == null 11 | || sub.isEmpty() 12 | || str.length() < sub.length()) { 13 | return 0; 14 | } 15 | 16 | if (str.startsWith(sub)) { 17 | return strCount(str.substring(sub.length()), sub) + 1; 18 | } else { 19 | return strCount(str.substring(1), sub); 20 | } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /java/src/main/java/codingbat/recursion1/StringClean.java: -------------------------------------------------------------------------------- 1 | package codingbat.recursion1; 2 | 3 | /** 4 | * https://codingbat.com/prob/p104029 5 | */ 6 | public final class StringClean { 7 | public String stringClean(String str) { 8 | if (str == null || str.isEmpty()) { 9 | return ""; 10 | } 11 | 12 | if (str.length() >= 2 && str.substring(0, 1).equals(str.substring(1, 2))) { 13 | return stringClean(str.substring(1)); 14 | } else { 15 | return str.charAt(0) + stringClean(str.substring(1)); 16 | } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /java/src/main/java/codingbat/recursion1/SumDigits.java: -------------------------------------------------------------------------------- 1 | package codingbat.recursion1; 2 | 3 | /** 4 | * https://codingbat.com/prob/p163932 5 | */ 6 | public final class SumDigits { 7 | public int sumDigits(int n) { 8 | if (n <= 0) { 9 | return 0; 10 | } 11 | 12 | return sumDigits(n / 10) + n % 10; 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /java/src/main/java/codingbat/warmup1/BackAround.java: -------------------------------------------------------------------------------- 1 | package codingbat.warmup1; 2 | 3 | /** 4 | * https://codingbat.com/prob/p161642 5 | */ 6 | public final class BackAround { 7 | public String backAround(String str) { 8 | char[] array = str.toCharArray(); 9 | return array[str.length() - 1] + str + array[str.length() - 1]; 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /java/src/main/java/codingbat/warmup1/Close10.java: -------------------------------------------------------------------------------- 1 | package codingbat.warmup1; 2 | 3 | /** 4 | * https://codingbat.com/prob/p172021 5 | */ 6 | public final class Close10 { 7 | public int close10(int a, int b) { 8 | int left = Math.abs(a - 10); 9 | int right = Math.abs(b - 10); 10 | 11 | if (left == right) { 12 | return 0; 13 | } else if (left > right) { 14 | return b; 15 | } else { 16 | return a; 17 | } 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /java/src/main/java/codingbat/warmup1/DelDel.java: -------------------------------------------------------------------------------- 1 | package codingbat.warmup1; 2 | 3 | /** 4 | * https://codingbat.com/prob/p100905 5 | */ 6 | public final class DelDel { 7 | public String delDel(String str) { 8 | if (str == null) { 9 | return ""; 10 | } 11 | 12 | if (str.length() >= 4 && str.startsWith("del", 1)) { 13 | return str.charAt(0) + str.substring(4); 14 | } else { 15 | return str; 16 | } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /java/src/main/java/codingbat/warmup1/Diff21.java: -------------------------------------------------------------------------------- 1 | package codingbat.warmup1; 2 | 3 | /** 4 | * https://codingbat.com/prob/p116624 5 | */ 6 | public final class Diff21 { 7 | public int diff21(int n) { 8 | int difference = Math.abs(21 - n); 9 | return n < 22 ? difference : difference * 2; 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /java/src/main/java/codingbat/warmup1/EveryNth.java: -------------------------------------------------------------------------------- 1 | package codingbat.warmup1; 2 | 3 | /** 4 | * https://codingbat.com/prob/p196441 5 | */ 6 | public final class EveryNth { 7 | public String everyNth(String str, int n) { 8 | StringBuilder stringBuilder = new StringBuilder(); 9 | if (str == null || str.isEmpty()) { 10 | return stringBuilder.toString(); 11 | } 12 | 13 | for (int i = 0; i < str.length(); i++) { 14 | if (i % n == 0) { 15 | stringBuilder.append(str.charAt(i)); 16 | } 17 | } 18 | 19 | return stringBuilder.toString(); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /java/src/main/java/codingbat/warmup1/Front22.java: -------------------------------------------------------------------------------- 1 | package codingbat.warmup1; 2 | 3 | /** 4 | * https://codingbat.com/prob/p183592 5 | */ 6 | public final class Front22 { 7 | public String front22(String str) { 8 | if (str == null) { 9 | return ""; 10 | } 11 | 12 | if (str.length() < 2) { 13 | return str + str + str; 14 | } else { 15 | String front = str.substring(0, 2); 16 | return front + str + front; 17 | } 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /java/src/main/java/codingbat/warmup1/HasTeen.java: -------------------------------------------------------------------------------- 1 | package codingbat.warmup1; 2 | 3 | /** 4 | * https://codingbat.com/prob/p178986 5 | */ 6 | public final class HasTeen { 7 | public boolean hasTeen(int a, int b, int c) { 8 | return (a <= 19 && a >= 13) || (b <= 19 && b >= 13) || (c <= 19 && c >= 13); 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /java/src/main/java/codingbat/warmup1/IcyHot.java: -------------------------------------------------------------------------------- 1 | package codingbat.warmup1; 2 | 3 | /** 4 | * https://codingbat.com/prob/p192082 5 | */ 6 | public final class IcyHot { 7 | public boolean icyHot(int temp1, int temp2) { 8 | return (temp1 < 0 && temp2 > 100) || (temp2 < 0 && temp1 > 100); 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /java/src/main/java/codingbat/warmup1/In1020.java: -------------------------------------------------------------------------------- 1 | package codingbat.warmup1; 2 | 3 | /** 4 | * https://codingbat.com/prob/p144535 5 | */ 6 | public final class In1020 { 7 | public boolean in1020(int a, int b) { 8 | return (a <= 20 && a >= 10) || (b <= 20 && b >= 10); 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /java/src/main/java/codingbat/warmup1/In3050.java: -------------------------------------------------------------------------------- 1 | package codingbat.warmup1; 2 | 3 | /** 4 | * https://codingbat.com/prob/p132134 5 | */ 6 | public final class In3050 { 7 | public boolean in3050(int a, int b) { 8 | return ((a <= 40 && a >= 30) 9 | && (b <= 40 && b >= 30)) 10 | || ((a <= 50 && a >= 40) 11 | && (b <= 50 && b >= 40)); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /java/src/main/java/codingbat/warmup1/IntMax.java: -------------------------------------------------------------------------------- 1 | package codingbat.warmup1; 2 | 3 | /** 4 | * https://codingbat.com/prob/p101887 5 | */ 6 | public final class IntMax { 7 | public int intMax(int a, int b, int c) { 8 | if (a > b && a > c) { 9 | return a; 10 | } else if (b > a && b > c) { 11 | return b; 12 | } else if (c > a && c > b) { 13 | return c; 14 | } else { 15 | return 0; 16 | } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /java/src/main/java/codingbat/warmup1/LastDigit.java: -------------------------------------------------------------------------------- 1 | package codingbat.warmup1; 2 | 3 | /** 4 | * https://codingbat.com/prob/p125339 5 | */ 6 | public final class LastDigit { 7 | public boolean lastDigit(int a, int b) { 8 | return (a % 10) == (b % 10); 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /java/src/main/java/codingbat/warmup1/LoneTeen.java: -------------------------------------------------------------------------------- 1 | package codingbat.warmup1; 2 | 3 | /** 4 | * https://codingbat.com/prob/p165701 5 | */ 6 | public final class LoneTeen { 7 | public boolean loneTeen(int a, int b) { 8 | return ((a > 19 || a < 13) 9 | || (b > 19 || b < 13)) 10 | && ((a <= 19 && a >= 13) 11 | || (b <= 19 && b >= 13)); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /java/src/main/java/codingbat/warmup1/Makes10.java: -------------------------------------------------------------------------------- 1 | package codingbat.warmup1; 2 | 3 | /** 4 | * https://codingbat.com/prob/p182873 5 | */ 6 | public final class Makes10 { 7 | public boolean makes10(int a, int b) { 8 | return (a == 10 || b == 10) || a + b == 10; 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /java/src/main/java/codingbat/warmup1/Max1020.java: -------------------------------------------------------------------------------- 1 | package codingbat.warmup1; 2 | 3 | /** 4 | * https://codingbat.com/prob/p177372 5 | */ 6 | public final class Max1020 { 7 | public int max1020(int a, int b) { 8 | if ((a <= 20 && a >= 10) && (b <= 20 && b >= 10)) { 9 | return Math.max(a, b); 10 | } else if (!(a <= 20 && a >= 10) && (b <= 20 && b >= 10)) { 11 | return b; 12 | } else if (a <= 20 && a >= 10) { 13 | return a; 14 | } 15 | return 0; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /java/src/main/java/codingbat/warmup1/MissingChar.java: -------------------------------------------------------------------------------- 1 | package codingbat.warmup1; 2 | 3 | /** 4 | * https://codingbat.com/prob/p190570 5 | */ 6 | public final class MissingChar { 7 | public String missingChar(String str, int n) { 8 | StringBuilder temp = new StringBuilder(); 9 | for (int i = 0; i < str.length(); i++) { 10 | if (n != i) { 11 | temp.append(str.charAt(i)); 12 | } 13 | } 14 | 15 | return temp.toString(); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /java/src/main/java/codingbat/warmup1/MixStart.java: -------------------------------------------------------------------------------- 1 | package codingbat.warmup1; 2 | 3 | /** 4 | * https://codingbat.com/prob/p151713 5 | */ 6 | public final class MixStart { 7 | public boolean mixStart(String str) { 8 | return str != null && str.length() >= 3 && str.startsWith("ix", 1); 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /java/src/main/java/codingbat/warmup1/MonkeyTrouble.java: -------------------------------------------------------------------------------- 1 | package codingbat.warmup1; 2 | 3 | /** 4 | * https://codingbat.com/prob/p181646 5 | */ 6 | public final class MonkeyTrouble { 7 | public boolean monkeyTrouble(boolean aSmile, boolean bSmile) { 8 | return (aSmile && bSmile) || (!aSmile && !bSmile); 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /java/src/main/java/codingbat/warmup1/NearHundred.java: -------------------------------------------------------------------------------- 1 | package codingbat.warmup1; 2 | 3 | /** 4 | * https://codingbat.com/prob/p184004 5 | */ 6 | public final class NearHundred { 7 | public boolean nearHundred(int n) { 8 | return (Math.abs(100 - n) <= 10 || Math.abs(200 - n) <= 10); 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /java/src/main/java/codingbat/warmup1/NotString.java: -------------------------------------------------------------------------------- 1 | package codingbat.warmup1; 2 | 3 | /** 4 | * https://codingbat.com/prob/p191914 5 | */ 6 | public final class NotString { 7 | public String notString(String str) { 8 | if (str.startsWith("not")) { 9 | return str; 10 | } else { 11 | return "not " + str; 12 | } 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /java/src/main/java/codingbat/warmup1/Or35.java: -------------------------------------------------------------------------------- 1 | package codingbat.warmup1; 2 | 3 | /** 4 | * https://codingbat.com/prob/p112564 5 | */ 6 | public final class Or35 { 7 | public boolean or35(int n) { 8 | return n > 0 && ((n % 3) == 0 || (n % 5) == 0); 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /java/src/main/java/codingbat/warmup1/ParrotTrouble.java: -------------------------------------------------------------------------------- 1 | package codingbat.warmup1; 2 | 3 | /** 4 | * https://codingbat.com/prob/p140449 5 | */ 6 | public final class ParrotTrouble { 7 | public boolean parrotTrouble(boolean talking, int hour) { 8 | return (hour < 7 || hour > 20) && talking; 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /java/src/main/java/codingbat/warmup1/PosNeg.java: -------------------------------------------------------------------------------- 1 | package codingbat.warmup1; 2 | 3 | /** 4 | * https://codingbat.com/prob/p159227 5 | */ 6 | public final class PosNeg { 7 | public boolean posNeg(int a, int b, boolean negative) { 8 | if (negative && a < 0 && b < 0) { 9 | return true; 10 | } 11 | 12 | return !negative && ((a > 0 && b < 0) || (a < 0 && b > 0)); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /java/src/main/java/codingbat/warmup1/SleepIn.java: -------------------------------------------------------------------------------- 1 | package codingbat.warmup1; 2 | 3 | /** 4 | * https://codingbat.com/prob/p187868 5 | */ 6 | public final class SleepIn { 7 | public boolean sleepIn(boolean weekday, boolean vacation) { 8 | return !weekday || vacation; 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /java/src/main/java/codingbat/warmup1/StartHi.java: -------------------------------------------------------------------------------- 1 | package codingbat.warmup1; 2 | 3 | /** 4 | * https://codingbat.com/prob/p191022 5 | */ 6 | public final class StartHi { 7 | public boolean startHi(String str) { 8 | return str != null && str.startsWith("hi"); 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /java/src/main/java/codingbat/warmup1/StartOz.java: -------------------------------------------------------------------------------- 1 | package codingbat.warmup1; 2 | 3 | /** 4 | * https://codingbat.com/prob/p199720 5 | */ 6 | public final class StartOz { 7 | public String startOz(String str) { 8 | if (str == null) { 9 | return ""; 10 | } 11 | 12 | if (str.startsWith("oz")) { 13 | return "oz"; 14 | } else if (!str.isEmpty() && str.charAt(0) == 'o') { 15 | return "o"; 16 | } else if (!str.isEmpty() && str.charAt(1) == 'z') { 17 | return "z"; 18 | } else { 19 | return ""; 20 | } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /java/src/main/java/codingbat/warmup1/StringE.java: -------------------------------------------------------------------------------- 1 | package codingbat.warmup1; 2 | 3 | /** 4 | * https://codingbat.com/prob/p173784 5 | */ 6 | public final class StringE { 7 | public boolean stringE(String str) { 8 | if (str == null) { 9 | return false; 10 | } 11 | 12 | char[] array = str.toCharArray(); 13 | int count = 0; 14 | for (int i = 0; i < str.length(); i++) { 15 | if (array[i] == 'e') { 16 | count++; 17 | } 18 | } 19 | 20 | return count >= 1 && count <= 3; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /java/src/main/java/codingbat/warmup1/SumDouble.java: -------------------------------------------------------------------------------- 1 | package codingbat.warmup1; 2 | 3 | /** 4 | * https://codingbat.com/prob/p154485 5 | */ 6 | public final class SumDouble { 7 | public int sumDouble(int a, int b) { 8 | if (a == b) { 9 | return 2 * (a + b); 10 | } else { 11 | return a + b; 12 | } 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /java/src/main/java/leetcode/ExploreInsertIntoABinarySearchTree.java: -------------------------------------------------------------------------------- 1 | package leetcode; 2 | 3 | import leetcode.api.TreeNode; 4 | 5 | /** 6 | * https://leetcode.com/explore/learn/card/introduction-to-data-structure-binary-search-tree/141/basic-operations-in-a-bst/1003/ 7 | */ 8 | public final class ExploreInsertIntoABinarySearchTree { 9 | public TreeNode insertIntoBST(TreeNode root, int val) { 10 | if (root == null) { 11 | return new TreeNode(val); 12 | } 13 | 14 | if (val > root.val) { 15 | root.right = insertIntoBST(root.right, val); 16 | } else { 17 | root.left = insertIntoBST(root.left, val); 18 | } 19 | 20 | return root; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /java/src/main/java/leetcode/Problem100SameTree.java: -------------------------------------------------------------------------------- 1 | package leetcode; 2 | 3 | import leetcode.api.TreeNode; 4 | 5 | /** 6 | * https://leetcode.com/problems/same-tree 7 | * https://leetcode.com/explore/learn/card/recursion-ii/503/recursion-to-iteration/2894/ 8 | */ 9 | public final class Problem100SameTree { 10 | public boolean isSameTree(TreeNode p, TreeNode q) { 11 | if (p == null && q == null) { 12 | return true; 13 | } 14 | 15 | if (p == null || q == null) { 16 | return false; 17 | } 18 | 19 | return p.val == q.val 20 | && isSameTree(p.left, q.left) 21 | && isSameTree(p.right, q.right); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /java/src/main/java/leetcode/Problem104MaximumDepthOfBinaryTree.java: -------------------------------------------------------------------------------- 1 | package leetcode; 2 | 3 | import leetcode.api.TreeNode; 4 | 5 | /** 6 | * https://leetcode.com/problems/maximum-depth-of-binary-tree 7 | * https://leetcode.com/explore/learn/card/recursion-i/256/complexity-analysis/2375/ 8 | */ 9 | public final class Problem104MaximumDepthOfBinaryTree { 10 | public int maxDepth(TreeNode root) { 11 | if (root == null) { 12 | return 0; 13 | } 14 | 15 | return Math.max(maxDepth(root.left), maxDepth(root.right)) + 1; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /java/src/main/java/leetcode/Problem1085SumOfDigitsInTheMinimumNumber.java: -------------------------------------------------------------------------------- 1 | package leetcode; 2 | 3 | /** 4 | * https://leetcode.com/problems/sum-of-digits-in-the-minimum-number/ 5 | */ 6 | public final class Problem1085SumOfDigitsInTheMinimumNumber { 7 | public int sumOfDigits(int[] A) { 8 | if (A == null || A.length == 0) { 9 | return -1; 10 | } 11 | 12 | int min = A[0]; 13 | for (int nums : A) { 14 | min = Math.min(min, nums); 15 | } 16 | 17 | int sum = 0; 18 | while (min != 0) { 19 | sum += min % 10; 20 | min /= 10; 21 | } 22 | 23 | return sum % 2 == 0 ? 1 : 0; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /java/src/main/java/leetcode/Problem1089DuplicateZeros.java: -------------------------------------------------------------------------------- 1 | package leetcode; 2 | 3 | /** 4 | * https://leetcode.com/problems/duplicate-zeros/ 5 | */ 6 | public final class Problem1089DuplicateZeros { 7 | public void duplicateZeros(int[] arr) { 8 | if (arr == null || arr.length == 0) { 9 | return; 10 | } 11 | 12 | for (int i = arr.length - 1; i > 0; i--) { 13 | if (arr[i] == 0 && i + 1 < arr.length) { 14 | System.arraycopy(arr, i, arr, i + 1, arr.length - i - 1); 15 | } 16 | } 17 | 18 | if (arr[0] == 0) { 19 | System.arraycopy(arr, 0, arr, 1, arr.length - 1); 20 | } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /java/src/main/java/leetcode/Problem111MinimumDepthOfBinaryTree.java: -------------------------------------------------------------------------------- 1 | package leetcode; 2 | 3 | import leetcode.api.TreeNode; 4 | 5 | /** 6 | * https://leetcode.com/problems/minimum-depth-of-binary-tree 7 | */ 8 | public final class Problem111MinimumDepthOfBinaryTree { 9 | public int minDepth(TreeNode root) { 10 | if (root == null) { 11 | return 0; 12 | } 13 | 14 | if (root.left == null) { 15 | return minDepth(root.right) + 1; 16 | } 17 | 18 | if (root.right == null) { 19 | return minDepth(root.left) + 1; 20 | } 21 | 22 | return Math.min(minDepth(root.left), minDepth(root.right)) + 1; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /java/src/main/java/leetcode/Problem1134ArmstrongNumber.java: -------------------------------------------------------------------------------- 1 | package leetcode; 2 | 3 | /** 4 | * https://leetcode.com/problems/armstrong-number/ 5 | */ 6 | public final class Problem1134ArmstrongNumber { 7 | public boolean isArmstrong(int N) { 8 | int length = (int) (Math.log10(N) + 1); 9 | int copy = N; 10 | int sum = 0; 11 | 12 | while (N != 0) { 13 | int lastDigit = N % 10; 14 | 15 | sum += (int) Math.pow(lastDigit, length); 16 | 17 | N /= 10; 18 | } 19 | 20 | return copy == sum; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /java/src/main/java/leetcode/Problem114FlattenBinaryTreeToLinkedList.java: -------------------------------------------------------------------------------- 1 | package leetcode; 2 | 3 | import leetcode.api.TreeNode; 4 | 5 | /** 6 | * https://leetcode.com/problems/flatten-binary-tree-to-linked-list 7 | */ 8 | public final class Problem114FlattenBinaryTreeToLinkedList { 9 | public void flatten(TreeNode root) { 10 | flatten(root, null); 11 | } 12 | 13 | private TreeNode flatten(TreeNode root, TreeNode prev) { 14 | if (root == null) { 15 | return prev; 16 | } 17 | 18 | prev = flatten(root.right, prev); 19 | prev = flatten(root.left, prev); 20 | root.right = prev; 21 | root.left = null; 22 | 23 | return root; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /java/src/main/java/leetcode/Problem1265PrintImmutableLinkedListInReverse.java: -------------------------------------------------------------------------------- 1 | package leetcode; 2 | 3 | import leetcode.api.ImmutableListNode; 4 | 5 | /** 6 | * https://leetcode.com/problems/print-immutable-linked-list-in-reverse/ 7 | */ 8 | public final class Problem1265PrintImmutableLinkedListInReverse { 9 | public void printLinkedListInReverse(ImmutableListNode head) { 10 | if (head == null) { 11 | return; 12 | } 13 | 14 | printLinkedListInReverse(head.getNext()); 15 | head.printValue(); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /java/src/main/java/leetcode/Problem1281SubtractTheProductAndSumOfDigitsOfAnInteger.java: -------------------------------------------------------------------------------- 1 | package leetcode; 2 | 3 | /** 4 | * https://leetcode.com/problems/subtract-the-product-and-sum-of-digits-of-an-integer/ 5 | */ 6 | public final class Problem1281SubtractTheProductAndSumOfDigitsOfAnInteger { 7 | public int subtractProductAndSum(int n) { 8 | int product = 1; 9 | int sum = 0; 10 | 11 | while (n != 0) { 12 | int lastDigit = n % 10; 13 | 14 | product *= lastDigit; 15 | sum += lastDigit; 16 | 17 | n /= 10; 18 | } 19 | 20 | return product - sum; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /java/src/main/java/leetcode/Problem1304FindNUniqueIntegersSumUpToZero.java: -------------------------------------------------------------------------------- 1 | package leetcode; 2 | 3 | /** 4 | * https://leetcode.com/problems/find-n-unique-integers-sum-up-to-zero/ 5 | */ 6 | public final class Problem1304FindNUniqueIntegersSumUpToZero { 7 | // Pattern 8 | // n = 1, [0] 9 | // n = 2, [-1, 1] 10 | // n = 3, [-2, 0, 2] 11 | // n = 4, [-3, -1, 1, 2] 12 | // n = 5, [-4, -2, 0, 2, 4] 13 | public int[] sumZero(int n) { 14 | if (n <= 0) { 15 | return new int[0]; 16 | } 17 | 18 | int[] array = new int[n]; 19 | 20 | for (int i = 0; i < n; i++) { 21 | array[i] = i * 2 - n + 1; 22 | } 23 | 24 | return array; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /java/src/main/java/leetcode/Problem1317ConvertIntegerToTheSumOfTwoNoZeroIntegers.java: -------------------------------------------------------------------------------- 1 | package leetcode; 2 | 3 | /** 4 | * https://leetcode.com/problems/convert-integer-to-the-sum-of-two-no-zero-integers/ 5 | */ 6 | public final class Problem1317ConvertIntegerToTheSumOfTwoNoZeroIntegers { 7 | public int[] getNoZeroIntegers(int n) { 8 | if (n < 2) { 9 | return new int[0]; 10 | } 11 | 12 | for (int i = 1; i <= n / 2; i++) { 13 | int diff = n - i; 14 | if (!String.valueOf(i).contains("0") 15 | && !String.valueOf(diff).contains("0")) { 16 | return new int[] {i, diff}; 17 | } 18 | } 19 | 20 | return new int[0]; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /java/src/main/java/leetcode/Problem1342NumberOfStepsToReduceANumberToZero.java: -------------------------------------------------------------------------------- 1 | package leetcode; 2 | 3 | /** 4 | * https://leetcode.com/problems/number-of-steps-to-reduce-a-number-to-zero/ 5 | */ 6 | public final class Problem1342NumberOfStepsToReduceANumberToZero { 7 | public int numberOfSteps(int num) { 8 | if (num <= 0) { 9 | return 0; 10 | } 11 | 12 | int count = 0; 13 | 14 | while (num != 0) { 15 | if (num % 2 == 0) { 16 | num /= 2; 17 | } else { 18 | num--; 19 | } 20 | count++; 21 | } 22 | 23 | return count; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /java/src/main/java/leetcode/Problem136SingleNumber.java: -------------------------------------------------------------------------------- 1 | package leetcode; 2 | 3 | /** 4 | * https://leetcode.com/problems/single-number 5 | */ 6 | public final class Problem136SingleNumber { 7 | // Time - O(N), Space - O(1) 8 | public int singleNumber(int[] nums) { 9 | if (nums == null) { 10 | return 0; 11 | } 12 | 13 | int check = 0; 14 | 15 | for (int num : nums) { 16 | check ^= num; 17 | } 18 | 19 | return check; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /java/src/main/java/leetcode/Problem137SingleNumberII.java: -------------------------------------------------------------------------------- 1 | package leetcode; 2 | 3 | /** 4 | * https://leetcode.com/problems/single-number-ii/ 5 | */ 6 | public final class Problem137SingleNumberII { 7 | public int singleNumber(int[] nums) { 8 | int checker = 0; 9 | int checker2 = 0; 10 | 11 | for (int num : nums) { 12 | checker = (checker ^ num) & ~checker2; 13 | checker2 = (checker2 ^ num) & ~checker; 14 | } 15 | 16 | return checker; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /java/src/main/java/leetcode/Problem1486XOROperationInAnArray.java: -------------------------------------------------------------------------------- 1 | package leetcode; 2 | 3 | /** 4 | * https://leetcode.com/problems/xor-operation-in-an-array/ 5 | */ 6 | public final class Problem1486XOROperationInAnArray { 7 | public int xorOperation(int n, int start) { 8 | if (n < 1 || start < 0) { 9 | return -1; 10 | } 11 | 12 | int toReturn = 0; 13 | for (int i = 0; i < n; i++) { 14 | toReturn ^= (start + 2 * i); 15 | } 16 | 17 | return toReturn; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /java/src/main/java/leetcode/Problem14LongestCommonPrefix.java: -------------------------------------------------------------------------------- 1 | package leetcode; 2 | 3 | /** 4 | * https://leetcode.com/problems/longest-common-prefix 5 | * https://leetcode.com/explore/learn/card/array-and-string/203/introduction-to-string/1162/ 6 | */ 7 | public final class Problem14LongestCommonPrefix { 8 | public String longestCommonPrefix(String[] strs) { 9 | if (strs == null || strs.length == 0) { 10 | return ""; 11 | } 12 | 13 | String prefix = strs[0]; 14 | for (String str : strs) { 15 | while (str.indexOf(prefix) != 0) { 16 | prefix = prefix.substring(0, prefix.length() - 1); 17 | } 18 | } 19 | 20 | return prefix; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /java/src/main/java/leetcode/Problem1523CountOddNumbersInAnIntervalRange.java: -------------------------------------------------------------------------------- 1 | package leetcode; 2 | 3 | /** 4 | * https://leetcode.com/problems/count-odd-numbers-in-an-interval-range/ 5 | */ 6 | public final class Problem1523CountOddNumbersInAnIntervalRange { 7 | public int countOdds(int low, int high) { 8 | if (low < 0 || high < 0 || low > high) { 9 | return -1; 10 | } 11 | 12 | if (low % 2 != 0 && high % 2 != 0) { 13 | return (high - low) / 2 + 1; 14 | } else { 15 | return (high - low + 1) / 2; 16 | } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /java/src/main/java/leetcode/Problem162FindPeakElement.java: -------------------------------------------------------------------------------- 1 | package leetcode; 2 | 3 | /** 4 | * https://leetcode.com/problems/find-peak-element 5 | */ 6 | public final class Problem162FindPeakElement { 7 | public int findPeakElement(int[] nums) { 8 | if (nums == null || nums.length == 0) { 9 | return -1; 10 | } 11 | 12 | int low = 0; 13 | int high = nums.length - 1; 14 | 15 | while (low < high) { 16 | int mid = low + (high - low) / 2; 17 | if (nums[mid] > nums[mid + 1]) { 18 | high = mid; 19 | } else { 20 | low = mid + 1; 21 | } 22 | } 23 | 24 | return low; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /java/src/main/java/leetcode/Problem168ExcelSheetColumnTitle.java: -------------------------------------------------------------------------------- 1 | package leetcode; 2 | 3 | /** 4 | * https://leetcode.com/problems/excel-sheet-column-title/submissions/ 5 | */ 6 | public final class Problem168ExcelSheetColumnTitle { 7 | public String convertToTitle(int n) { 8 | StringBuilder stringBuilder = new StringBuilder(); 9 | while (n > 0) { 10 | n--; 11 | char character = (char) ('A' + (n % 26)); 12 | stringBuilder.insert(0, character); 13 | n /= 26; 14 | } 15 | 16 | return stringBuilder.toString(); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /java/src/main/java/leetcode/Problem169MajorityElement.java: -------------------------------------------------------------------------------- 1 | package leetcode; 2 | 3 | import java.util.HashMap; 4 | import java.util.Map; 5 | 6 | /** 7 | * https://leetcode.com/problems/majority-element/ 8 | */ 9 | public final class Problem169MajorityElement { 10 | public int majorityElement(final int[] nums) { 11 | int majority = nums.length / 2; 12 | Map map = new HashMap<>(); 13 | 14 | for (int number : nums) { 15 | map.put(number, map.getOrDefault(number, 0) + 1); 16 | 17 | if (map.getOrDefault(number, 0) > majority) { 18 | return number; 19 | } 20 | } 21 | 22 | return -1; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /java/src/main/java/leetcode/Problem171ExcelSheetColumnNumber.java: -------------------------------------------------------------------------------- 1 | package leetcode; 2 | 3 | /** 4 | * https://leetcode.com/problems/excel-sheet-column-number/ 5 | */ 6 | public final class Problem171ExcelSheetColumnNumber { 7 | public int titleToNumber(String s) { 8 | int n = 0; 9 | for (int i = 0; i < s.length(); i++) { 10 | n *= 26; 11 | n += ((int) s.charAt(i) - 'A') + 1; 12 | } 13 | return n; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /java/src/main/java/leetcode/Problem172FactorialTrailingZeroes.java: -------------------------------------------------------------------------------- 1 | package leetcode; 2 | 3 | /** 4 | * https://leetcode.com/problems/factorial-trailing-zeroes 5 | */ 6 | public final class Problem172FactorialTrailingZeroes { 7 | public int trailingZeroes(int n) { 8 | int zeroes = 0; 9 | while (n > 4) { 10 | zeroes += n / 5; 11 | n /= 5; 12 | } 13 | 14 | return zeroes; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /java/src/main/java/leetcode/Problem190ReverseBits.java: -------------------------------------------------------------------------------- 1 | package leetcode; 2 | 3 | /** 4 | * https://leetcode.com/problems/reverse-bits/ 5 | */ 6 | public final class Problem190ReverseBits { 7 | public int reverseBits(long num) { 8 | int reverse = 0; 9 | for (int i = 0; i < 32; i++) { 10 | reverse = (int) (reverse + (num & 1)); 11 | num >>>= 1; 12 | if (i < 31) { 13 | reverse <<= 1; 14 | } 15 | } 16 | return reverse; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /java/src/main/java/leetcode/Problem191NumberOf1Bits.java: -------------------------------------------------------------------------------- 1 | package leetcode; 2 | 3 | /** 4 | * https://leetcode.com/problems/number-of-1-bits/ 5 | */ 6 | public final class Problem191NumberOf1Bits { 7 | public int hammingWeight(int n) { 8 | int distance = 0; 9 | int currentValue = n; 10 | 11 | while (currentValue != 0) { 12 | distance++; 13 | currentValue &= currentValue - 1; 14 | } 15 | 16 | return distance; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /java/src/main/java/leetcode/Problem231PowerOfTwo.java: -------------------------------------------------------------------------------- 1 | package leetcode; 2 | 3 | /** 4 | * https://leetcode.com/problems/power-of-two/ 5 | */ 6 | public final class Problem231PowerOfTwo { 7 | public boolean isPowerOfTwo(int n) { 8 | while ((n % 2) == 0 && n > 1) { 9 | n = n / 2; 10 | } 11 | 12 | return n == 1; 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /java/src/main/java/leetcode/Problem237DeleteNodeInALinkedList.java: -------------------------------------------------------------------------------- 1 | package leetcode; 2 | 3 | import leetcode.api.ListNode; 4 | 5 | /** 6 | * https://leetcode.com/problems/delete-node-in-a-linked-list 7 | */ 8 | public final class Problem237DeleteNodeInALinkedList { 9 | // Time - O(1), Space - (1) 10 | public void deleteNode(ListNode node) { 11 | if (node == null) { 12 | return; 13 | } 14 | 15 | node.val = node.next.val; 16 | node.next = node.next.next; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /java/src/main/java/leetcode/Problem258AddDigits.java: -------------------------------------------------------------------------------- 1 | package leetcode; 2 | 3 | /** 4 | * https://leetcode.com/problems/add-digits/ 5 | */ 6 | public final class Problem258AddDigits { 7 | public int addDigits(int num) { 8 | return 1 + ((num - 1) % 9); 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /java/src/main/java/leetcode/Problem263UglyNumber.java: -------------------------------------------------------------------------------- 1 | package leetcode; 2 | 3 | /** 4 | * https://leetcode.com/problems/ugly-number/ 5 | */ 6 | public final class Problem263UglyNumber { 7 | public boolean isUgly(int num) { 8 | if (num < 1) { 9 | return false; 10 | } 11 | 12 | for (int i = 2; i < 6; i++) { 13 | while (num % i == 0) { 14 | num /= i; 15 | } 16 | } 17 | 18 | return num == 1; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /java/src/main/java/leetcode/Problem278FirstBadVersion.java: -------------------------------------------------------------------------------- 1 | package leetcode; 2 | 3 | /** 4 | * https://leetcode.com/problems/first-bad-version 5 | */ 6 | public final class Problem278FirstBadVersion { 7 | public int firstBadVersion(int n) { 8 | int start = 1; 9 | int end = n; 10 | 11 | while (start < end) { 12 | int mid = start + (end - start) / 2; 13 | if (isBadVersion(mid)) { 14 | end = mid; 15 | } else { 16 | start = mid + 1; 17 | } 18 | } 19 | 20 | return start; 21 | } 22 | 23 | // stub - part of question 24 | private boolean isBadVersion(int mid) { 25 | return false; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /java/src/main/java/leetcode/Problem27RemoveElement.java: -------------------------------------------------------------------------------- 1 | package leetcode; 2 | 3 | /** 4 | * https://leetcode.com/problems/remove-element 5 | * https://leetcode.com/explore/learn/card/array-and-string/205/array-two-pointer-technique/1151/ 6 | */ 7 | public final class Problem27RemoveElement { 8 | // Time - O(N), Space - O(1) 9 | public int removeElement(int[] nums, int val) { 10 | if (nums == null || nums.length == 0) { 11 | return 0; 12 | } 13 | 14 | int count = 0; 15 | for (int i = 0; i < nums.length; i++) { 16 | if (nums[i] != val) { 17 | nums[count++] = nums[i]; 18 | } 19 | } 20 | 21 | return count; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /java/src/main/java/leetcode/Problem292NimGame.java: -------------------------------------------------------------------------------- 1 | package leetcode; 2 | 3 | /** 4 | * https://leetcode.com/problems/nim-game 5 | */ 6 | public final class Problem292NimGame { 7 | // Time - O(1), Space - O(1) 8 | public boolean canWinNim(int n) { 9 | return n >= 0 && n % 4 != 0; 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /java/src/main/java/leetcode/Problem326PowerOfThree.java: -------------------------------------------------------------------------------- 1 | package leetcode; 2 | 3 | /** 4 | * https://leetcode.com/problems/power-of-three/ 5 | * https://leetcode.com/explore/interview/card/top-interview-questions-easy/102/math/745/ 6 | */ 7 | public final class Problem326PowerOfThree { 8 | public boolean isPowerOfThree(int n) { 9 | while ((n % 3) == 0 && n > 1) { 10 | n = n / 3; 11 | } 12 | 13 | return n == 1; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /java/src/main/java/leetcode/Problem338CountingBits.java: -------------------------------------------------------------------------------- 1 | package leetcode; 2 | 3 | /** 4 | * https://leetcode.com/problems/counting-bits/submissions/ 5 | */ 6 | public final class Problem338CountingBits { 7 | public int[] countBits(int num) { 8 | int[] numbers = new int[num + 1]; 9 | 10 | for (int i = 0; i <= num; i++) { 11 | numbers[i] = countOnes(i); 12 | } 13 | 14 | return numbers; 15 | } 16 | 17 | private int countOnes(int num) { 18 | int count = 0; 19 | while (num > 0) { 20 | if ((num % 2) == 1) { 21 | count++; 22 | } 23 | 24 | num = num / 2; 25 | } 26 | 27 | return count; 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /java/src/main/java/leetcode/Problem342PowerOfFour.java: -------------------------------------------------------------------------------- 1 | package leetcode; 2 | 3 | /** 4 | * https://leetcode.com/problems/power-of-four/ 5 | */ 6 | public final class Problem342PowerOfFour { 7 | public boolean isPowerOfFour(int num) { 8 | while ((num % 4) == 0 && num > 1) { 9 | num = num / 4; 10 | } 11 | 12 | return num == 1; 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /java/src/main/java/leetcode/Problem367ValidPerfectSquare.java: -------------------------------------------------------------------------------- 1 | package leetcode; 2 | 3 | /** 4 | * https://leetcode.com/problems/valid-perfect-square 5 | */ 6 | public final class Problem367ValidPerfectSquare { 7 | public boolean isPerfectSquare(int num) { 8 | int low = 1; 9 | int high = num; 10 | 11 | while (low <= high) { 12 | long mid = (long) (low + high) >>> 1; 13 | if (mid * mid == num) { 14 | return true; 15 | } else if (mid * mid < num) { 16 | low = (int) mid + 1; 17 | } else { 18 | high = (int) mid - 1; 19 | } 20 | } 21 | 22 | return false; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /java/src/main/java/leetcode/Problem371SumOfTwoIntegers.java: -------------------------------------------------------------------------------- 1 | package leetcode; 2 | 3 | /** 4 | * https://leetcode.com/problems/sum-of-two-integers 5 | */ 6 | public final class Problem371SumOfTwoIntegers { 7 | // Time - O(N), Space - O(1) 8 | public int getSum(int a, int b) { 9 | while (b != 0) { 10 | // carry contains common bits of a and b 11 | int carry = a & b; 12 | 13 | // sum of a and b bits where at least one of the bits is not set 14 | a ^= b; 15 | 16 | // shifted by 1 for required sum 17 | b = carry << 1; 18 | } 19 | 20 | return a; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /java/src/main/java/leetcode/Problem374GuessNumberHigherOrLower.java: -------------------------------------------------------------------------------- 1 | package leetcode; 2 | 3 | /** 4 | * https://leetcode.com/problems/guess-number-higher-or-lower 5 | */ 6 | public final class Problem374GuessNumberHigherOrLower { 7 | public int guessNumber(int n) { 8 | int low = 1; 9 | int high = n; 10 | 11 | while (low < high) { 12 | int mid = low + (high - low) / 2; 13 | 14 | if (guess(mid) == 1) { 15 | low = mid + 1; 16 | } else { 17 | high = mid; 18 | } 19 | } 20 | 21 | return low; 22 | } 23 | 24 | // stub - part of question 25 | private int guess(int n) { 26 | return -1; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /java/src/main/java/leetcode/Problem434NumberOfSegmentsInAString.java: -------------------------------------------------------------------------------- 1 | package leetcode; 2 | 3 | /** 4 | * https://leetcode.com/problems/number-of-segments-in-a-string/ 5 | */ 6 | public final class Problem434NumberOfSegmentsInAString { 7 | public int countSegments(String s) { 8 | if (s == null || s.isEmpty()) { 9 | return 0; 10 | } 11 | 12 | s = s.trim(); 13 | return s.isEmpty() ? 0 : s.split("\\s+", -1).length; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /java/src/main/java/leetcode/Problem461HammingDistance.java: -------------------------------------------------------------------------------- 1 | package leetcode; 2 | 3 | /** 4 | * https://leetcode.com/problems/hamming-distance/ 5 | */ 6 | public final class Problem461HammingDistance { 7 | public int hammingDistance(int x, int y) { 8 | int distance = 0; 9 | int currentValue = x ^ y; 10 | 11 | while (currentValue != 0) { 12 | distance++; 13 | 14 | currentValue &= currentValue - 1; 15 | } 16 | 17 | return distance; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /java/src/main/java/leetcode/Problem476NumberComplement.java: -------------------------------------------------------------------------------- 1 | package leetcode; 2 | 3 | /** 4 | * https://leetcode.com/problems/number-complement/ 5 | */ 6 | public final class Problem476NumberComplement { 7 | public int findComplement(int num) { 8 | return ~num & ((Integer.highestOneBit(num) << 1) - 1); 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /java/src/main/java/leetcode/Problem507PerfectNumber.java: -------------------------------------------------------------------------------- 1 | package leetcode; 2 | 3 | /** 4 | * https://leetcode.com/problems/perfect-number/ 5 | */ 6 | public final class Problem507PerfectNumber { 7 | public boolean checkPerfectNumber(int num) { 8 | int sum = 1; 9 | for (int i = 2; i * i <= num; i++) { 10 | if (num % i == 0) { 11 | sum += i + num / i; 12 | } 13 | } 14 | 15 | return sum == num && num != 1; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /java/src/main/java/leetcode/Problem50Pow.java: -------------------------------------------------------------------------------- 1 | package leetcode; 2 | 3 | /** 4 | * https://leetcode.com/problems/powx-n 5 | * https://leetcode.com/explore/learn/card/recursion-i/256/complexity-analysis/2380/ 6 | */ 7 | public final class Problem50Pow { 8 | // recursion 9 | public double myPow(double x, int n) { 10 | if (n < 0) { 11 | x = 1 / x; 12 | 13 | if (n == Integer.MIN_VALUE) { 14 | return x * myPow(x, Integer.MAX_VALUE); 15 | } else { 16 | n = -n; 17 | } 18 | } 19 | 20 | if (n == 0) { 21 | return 1; 22 | } 23 | 24 | return n % 2 == 0 ? myPow(x * x, n / 2) : x * myPow(x * x, n / 2); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /java/src/main/java/leetcode/Problem53MaximumSubarray.java: -------------------------------------------------------------------------------- 1 | package leetcode; 2 | 3 | /** 4 | * https://leetcode.com/problems/maximum-subarray 5 | */ 6 | public final class Problem53MaximumSubarray { 7 | // Time - O(N), Space - O(1) 8 | public int maxSubArray(int[] nums) { 9 | if (nums == null || nums.length == 0) { 10 | return 0; 11 | } 12 | 13 | int max = nums[0]; 14 | int temp = nums[0]; 15 | 16 | for (int i = 1; i < nums.length; i++) { 17 | int currentNumber = nums[i]; 18 | temp = Math.max(temp + currentNumber, currentNumber); 19 | max = Math.max(max, temp); 20 | } 21 | 22 | return max; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /java/src/main/java/leetcode/Problem55JumpGame.java: -------------------------------------------------------------------------------- 1 | package leetcode; 2 | 3 | /** 4 | * https://leetcode.com/problems/jump-game 5 | */ 6 | public final class Problem55JumpGame { 7 | // Time - O(N), Space - O(1) 8 | public boolean canJump(int[] nums) { 9 | if (nums == null || nums.length == 0) { 10 | return false; 11 | } 12 | 13 | int max = 0; 14 | for (int i = 0; i < nums.length; i++) { 15 | if (i > max) { 16 | return false; 17 | } 18 | 19 | max = Math.max(nums[i] + i, max); 20 | } 21 | 22 | return true; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /java/src/main/java/leetcode/Problem575DistributeCandies.java: -------------------------------------------------------------------------------- 1 | package leetcode; 2 | 3 | import java.util.HashSet; 4 | import java.util.Set; 5 | 6 | /** 7 | * https://leetcode.com/problems/distribute-candies/submissions/ 8 | */ 9 | public final class Problem575DistributeCandies { 10 | public int distributeCandies(int[] candyType) { 11 | if (candyType == null || candyType.length == 0) { 12 | return 0; 13 | } 14 | 15 | Set kinds = new HashSet<>(); 16 | for (int candy : candyType) { 17 | kinds.add(candy); 18 | } 19 | 20 | return Math.min(kinds.size(), candyType.length / 2); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /java/src/main/java/leetcode/Problem617MergeTwoBinaryTrees.java: -------------------------------------------------------------------------------- 1 | package leetcode; 2 | 3 | import leetcode.api.TreeNode; 4 | 5 | public final class Problem617MergeTwoBinaryTrees { 6 | public TreeNode mergeTrees(TreeNode t1, TreeNode t2) { 7 | if (t1 == null && t2 == null) { 8 | return null; 9 | } 10 | 11 | if (t1 == null) { 12 | return t2; 13 | } 14 | 15 | if (t2 == null) { 16 | return t1; 17 | } 18 | 19 | t1.val += t2.val; 20 | t1.left = mergeTrees(t1.left, t2.left); 21 | t1.right = mergeTrees(t1.right, t2.right); 22 | 23 | return t1; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /java/src/main/java/leetcode/Problem633SumOfSquareNumbers.java: -------------------------------------------------------------------------------- 1 | package leetcode; 2 | 3 | /** 4 | * https://leetcode.com/problems/sum-of-square-numbers/ 5 | */ 6 | public final class Problem633SumOfSquareNumbers { 7 | public boolean judgeSquareSum(int c) { 8 | for (int i = 0; i <= Math.sqrt(c); i++) { 9 | double sqrt = Math.sqrt(c - i * i); 10 | if (Math.floor(sqrt) == sqrt) { 11 | return true; 12 | } 13 | } 14 | 15 | return false; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /java/src/main/java/leetcode/Problem69Sqrt.java: -------------------------------------------------------------------------------- 1 | package leetcode; 2 | 3 | /** 4 | * https://leetcode.com/problems/sqrtx 5 | */ 6 | public final class Problem69Sqrt { 7 | public int mySqrt(int x) { 8 | if (x == 0 || x == 1) { 9 | return x; 10 | } 11 | 12 | long low = 0; 13 | long high = x / 2L + 1L; 14 | 15 | while (low <= high) { 16 | long mid = low + (high - low) / 2; 17 | if (mid * mid == x) { 18 | return (int) mid; 19 | } else if (mid * mid < x) { 20 | low = mid + 1; 21 | } else { 22 | high = mid - 1; 23 | } 24 | } 25 | 26 | return (int) high; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /java/src/main/java/leetcode/Problem709ToLowerCase.java: -------------------------------------------------------------------------------- 1 | package leetcode; 2 | 3 | /** 4 | * https://leetcode.com/problems/to-lower-case 5 | */ 6 | public final class Problem709ToLowerCase { 7 | // Time - O(N), Space - O(1) 8 | public String toLowerCase(String str) { 9 | if (str == null || str.isEmpty()) { 10 | return ""; 11 | } 12 | 13 | char[] word = str.toCharArray(); 14 | for (int i = 0; i < word.length; i++) { 15 | word[i] = Character.toLowerCase(word[i]); 16 | } 17 | 18 | return new String(word); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /java/src/main/java/leetcode/Problem796RotateString.java: -------------------------------------------------------------------------------- 1 | package leetcode; 2 | 3 | /** 4 | * https://leetcode.com/problems/rotate-string 5 | */ 6 | public final class Problem796RotateString { 7 | // Time - O(N), Space - O(1) 8 | public boolean rotateString(String A, String B) { 9 | if (A == null && B == null) { 10 | return true; 11 | } 12 | 13 | if ((A == null || B == null) || A.length() != B.length()) { 14 | return false; 15 | } 16 | 17 | String rotated = A + A; 18 | return rotated.contains(B); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /java/src/main/java/leetcode/Problem80RemoveDuplicatesFromSortedArrayII.java: -------------------------------------------------------------------------------- 1 | package leetcode; 2 | 3 | /** 4 | * https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii 5 | */ 6 | public final class Problem80RemoveDuplicatesFromSortedArrayII { 7 | // Time - O(N), Space - O(1) 8 | public int removeDuplicates(int[] nums) { 9 | int count = 0; 10 | for (int num : nums) { 11 | if (count < 2 || num > nums[count - 2]) { 12 | nums[count++] = num; 13 | } 14 | } 15 | 16 | return count; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /java/src/main/java/leetcode/Problem867TransposeMatrix.java: -------------------------------------------------------------------------------- 1 | package leetcode; 2 | 3 | /** 4 | * https://leetcode.com/problems/transpose-matrix/ 5 | */ 6 | public final class Problem867TransposeMatrix { 7 | public int[][] transpose(int[][] A) { 8 | if (A == null || A.length == 0 || A[0].length == 0) { 9 | return new int[0][]; 10 | } 11 | 12 | int[][] matrix = new int[A[0].length][A.length]; 13 | 14 | for (int i = 0; i < A[0].length; i++) { 15 | for (int j = 0; j < A.length; j++) { 16 | matrix[i][j] = A[j][i]; 17 | } 18 | } 19 | 20 | return matrix; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /java/src/main/java/leetcode/Problem876MiddleOfTheLinkedList.java: -------------------------------------------------------------------------------- 1 | package leetcode; 2 | 3 | import leetcode.api.ListNode; 4 | 5 | /** 6 | * https://leetcode.com/problems/middle-of-the-linked-list/ 7 | */ 8 | public final class Problem876MiddleOfTheLinkedList { 9 | // Time - O(N), Space - O(1) 10 | public ListNode middleNode(ListNode head) { 11 | if (head == null) { 12 | return null; 13 | } 14 | 15 | ListNode slow = head; 16 | ListNode fast = head; 17 | 18 | while (fast != null && fast.next != null) { 19 | slow = slow.next; 20 | fast = fast.next.next; 21 | } 22 | 23 | return slow; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /java/src/main/java/leetcode/Problem938RangeSumOfBST.java: -------------------------------------------------------------------------------- 1 | package leetcode; 2 | 3 | import leetcode.api.TreeNode; 4 | 5 | /** 6 | * https://leetcode.com/problems/range-sum-of-bst/ 7 | */ 8 | public final class Problem938RangeSumOfBST { 9 | public int rangeSumBST(TreeNode root, int L, int R) { 10 | if (root == null) { 11 | return 0; 12 | } 13 | 14 | int value = 0; 15 | if (root.val >= L && root.val <= R) { 16 | value = root.val; 17 | } 18 | 19 | return rangeSumBST(root.left, L, R) + rangeSumBST(root.right, L, R) + value; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /java/src/main/java/leetcode/Problem965UnivaluedBinaryTree.java: -------------------------------------------------------------------------------- 1 | package leetcode; 2 | 3 | import leetcode.api.TreeNode; 4 | 5 | /** 6 | * https://leetcode.com/problems/univalued-binary-tree/ 7 | */ 8 | public final class Problem965UnivaluedBinaryTree { 9 | public boolean isUnivalTree(TreeNode root) { 10 | return check(root, root.val); 11 | } 12 | 13 | public boolean check(TreeNode root, int value) { 14 | if (root == null) { 15 | return true; 16 | } 17 | 18 | if (root.val != value) { 19 | return false; 20 | } 21 | 22 | return check(root.left, value) && check(root.right, value); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /java/src/main/java/leetcode/api/ImmutableListNode.java: -------------------------------------------------------------------------------- 1 | package leetcode.api; 2 | 3 | public interface ImmutableListNode { 4 | void printValue(); // print the value of this node. 5 | 6 | ImmutableListNode getNext(); // return the next node. 7 | } 8 | -------------------------------------------------------------------------------- /java/src/main/java/leetcode/api/NestedInteger.java: -------------------------------------------------------------------------------- 1 | package leetcode.api; 2 | 3 | import java.util.List; 4 | 5 | public interface NestedInteger { 6 | boolean isInteger(); 7 | 8 | Integer getInteger(); 9 | 10 | void setInteger(int value); 11 | 12 | void add(NestedInteger ni); 13 | 14 | List getList(); 15 | } 16 | -------------------------------------------------------------------------------- /java/src/main/java/projecteuler/Number002.java: -------------------------------------------------------------------------------- 1 | package projecteuler; 2 | 3 | public final class Number002 { 4 | public static long getEvenFibonacciNumbers(long number) { 5 | long first = 1; 6 | long second = 1; 7 | long sum = 0; 8 | 9 | while (sum < number) { 10 | long next = first + second; 11 | second = first; 12 | first = next; 13 | 14 | if (next % 2 == 0) { 15 | sum += next; 16 | } 17 | } 18 | 19 | return sum; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /java/src/main/java/questions/BstContains.java: -------------------------------------------------------------------------------- 1 | package questions; 2 | 3 | import api.TreeNode; 4 | 5 | public final class BstContains { 6 | public static boolean contains(TreeNode root, int value) { 7 | if (root == null) { 8 | return false; 9 | } 10 | 11 | if (value == root.value) { 12 | return true; 13 | } 14 | 15 | if (value > root.value) { 16 | return contains(root.right, value); 17 | } 18 | 19 | if (value < root.value) { 20 | return contains(root.left, value); 21 | } 22 | 23 | return false; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /java/src/main/java/questions/BstFindMax.java: -------------------------------------------------------------------------------- 1 | package questions; 2 | 3 | import api.TreeNode; 4 | 5 | public final class BstFindMax { 6 | public static TreeNode findMaxNode(TreeNode node) { 7 | while (node.right != null) { 8 | node = node.right; 9 | } 10 | 11 | return node; 12 | } 13 | 14 | public static int findMaxValue(TreeNode node) { 15 | return findMaxNode(node).value; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /java/src/main/java/questions/BstFindMin.java: -------------------------------------------------------------------------------- 1 | package questions; 2 | 3 | import api.TreeNode; 4 | 5 | public final class BstFindMin { 6 | public static TreeNode findMinNode(TreeNode root) { 7 | while (root.left != null) { 8 | root = root.left; 9 | } 10 | 11 | return root; 12 | } 13 | 14 | public static int findMinValue(TreeNode root) { 15 | return findMinNode(root).value; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /java/src/main/java/questions/BstHeight.java: -------------------------------------------------------------------------------- 1 | package questions; 2 | 3 | import api.TreeNode; 4 | 5 | public final class BstHeight { 6 | public static int getHeight(TreeNode node) { 7 | if (node == null) { 8 | return -1; 9 | } 10 | 11 | int left = getHeight(node.left); 12 | int right = getHeight(node.right); 13 | 14 | return Math.max(left, right) + 1; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /java/src/main/java/questions/BstInOrder.java: -------------------------------------------------------------------------------- 1 | package questions; 2 | 3 | import api.TreeNode; 4 | 5 | public final class BstInOrder { 6 | public static void printInOrder(TreeNode node) { 7 | if (node == null) { 8 | return; 9 | } 10 | 11 | printInOrder(node.left); 12 | System.out.print(node.value + " "); 13 | printInOrder(node.right); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /java/src/main/java/questions/MaxNonContSubArray.java: -------------------------------------------------------------------------------- 1 | package questions; 2 | 3 | public final class MaxNonContSubArray { 4 | public static int maxNonContSubArray(int[] array) { 5 | if (array == null) { 6 | return -1; 7 | } 8 | 9 | int sum = 0; 10 | int max = Integer.MIN_VALUE; 11 | 12 | for (int a : array) { 13 | if (a >= 0) { 14 | sum += a; 15 | } 16 | 17 | if (a > max) { 18 | max = a; 19 | } 20 | } 21 | 22 | if (sum == 0) { 23 | sum = max; 24 | } 25 | 26 | return sum; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /java/src/main/java/questions/NaturalNumbersSum.java: -------------------------------------------------------------------------------- 1 | package questions; 2 | 3 | public final class NaturalNumbersSum { 4 | // Time - O(1), Space - O(1) 5 | public static int getSumNaturalNumbers(int number) { 6 | return (number * (number + 1)) / 2; 7 | } 8 | 9 | // Time - O(N), Space - O(1) 10 | public static int getSumNaturalNumbers2(int number) { 11 | int sum = 0; 12 | for (int i = 1; i <= number; i++) { 13 | sum += i; 14 | } 15 | 16 | return sum; 17 | } 18 | 19 | // Time - O(1), Space - O(1) 20 | public static int getSumNaturalNumbers(int[] numbers) { 21 | int length = numbers.length; 22 | return (length * (length + 1)) / 2; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /java/src/main/java/questions/RemoveEveryOther.java: -------------------------------------------------------------------------------- 1 | package questions; 2 | 3 | import api.Node; 4 | 5 | public final class RemoveEveryOther { 6 | // Time - O(N), Space - O(1) 7 | public static void deleteEveryOtherRecursive(Node node) { 8 | if (node != null && node.next != null) { 9 | node.next = node.next.next; 10 | deleteEveryOtherRecursive(node.next); 11 | } 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /java/src/main/java/questions/TrailingZeros.java: -------------------------------------------------------------------------------- 1 | package questions; 2 | 3 | public final class TrailingZeros { 4 | public static long getTrailingZeros(long number) { 5 | long numZeros = 0; 6 | 7 | while (number != 0) { 8 | long temp = number % 10; 9 | number /= 10; 10 | if (temp == 0) { 11 | numZeros++; 12 | } else { 13 | break; 14 | } 15 | } 16 | 17 | return numZeros; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /java/src/test/groovy/api/ColorSpec.groovy: -------------------------------------------------------------------------------- 1 | package api 2 | 3 | import spock.lang.Specification 4 | 5 | final class ColorSpec extends Specification { 6 | def 'values'() { 7 | expect: 8 | Color.valueOf(Color.BLUE.toString()) 9 | Color.values().length == 3 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /java/src/test/groovy/api/PointSpec.groovy: -------------------------------------------------------------------------------- 1 | package api 2 | 3 | import nl.jqno.equalsverifier.EqualsVerifier 4 | import spock.lang.Specification 5 | 6 | final class PointSpec extends Specification { 7 | private def sut = new Point(321, 123) 8 | 9 | def 'getX'() { 10 | expect: 11 | sut.getX() == 321 12 | } 13 | 14 | def 'getY'() { 15 | expect: 16 | sut.getY() == 123 17 | } 18 | 19 | def 'equals/hashCode'() { 20 | when: 21 | EqualsVerifier.forClass(Point.class) 22 | .verify() 23 | 24 | then: 25 | noExceptionThrown() 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /java/src/test/groovy/codingbat/recursion1/AllStarTest.groovy: -------------------------------------------------------------------------------- 1 | package codingbat.recursion1 2 | 3 | import spock.lang.Specification 4 | import spock.lang.Unroll 5 | 6 | final class AllStarTest extends Specification { 7 | @Unroll def 'allStar(#str) = #answer'() { 8 | given: 9 | def sut = new AllStar() 10 | 11 | expect: 12 | sut.allStar(str) == answer 13 | 14 | where: 15 | str || answer 16 | 'hello' || 'h*e*l*l*o' 17 | 'abc' || 'a*b*c' 18 | 'ab' || 'a*b' 19 | '' || '' 20 | '3.14' || '3*.*1*4' 21 | 'Chocolate' || 'C*h*o*c*o*l*a*t*e' 22 | '1234' || '1*2*3*4' 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /java/src/test/groovy/leetcode/ExploreBinarySearchSpec.groovy: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import spock.lang.Specification 4 | import spock.lang.Unroll 5 | 6 | final class ExploreBinarySearchSpec extends Specification { 7 | @Unroll def 'search(#nums, #target) = #answer'() { 8 | given: 9 | def sut = new Problem704BinarySearch() 10 | 11 | expect: 12 | sut.search(nums as int[], target) == answer 13 | 14 | where: 15 | nums | target || answer 16 | [-1, 0, 3, 5, 9, 12] | 9 || 4 17 | [-1, 0, 3, 5, 9, 12] | 2 || -1 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /java/src/test/groovy/leetcode/Problem1047RemoveAllAdjacentDuplicatesInStringSpec.groovy: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import spock.lang.Specification 4 | import spock.lang.Unroll 5 | 6 | class Problem1047RemoveAllAdjacentDuplicatesInStringSpec extends Specification { 7 | @Unroll 8 | def 'removeDuplicates(#S) = #answer'() { 9 | given: 10 | def sut = new Problem1047RemoveAllAdjacentDuplicatesInString() 11 | 12 | expect: 13 | sut.removeDuplicates(S) == answer 14 | sut.removeDuplicates2(S) == answer 15 | sut.removeDuplicates3(S) == answer 16 | 17 | where: 18 | S | answer 19 | "abbaca" | "ca" 20 | "azxxzy" | "ay" 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /java/src/test/groovy/leetcode/Problem1219PathWithMaximumGoldSpec.groovy: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import spock.lang.Specification 4 | import spock.lang.Unroll 5 | 6 | final class Problem1219PathWithMaximumGoldSpec extends Specification { 7 | @Unroll 8 | def 'getMaximumGold(#n) = #answer'() { 9 | given: 10 | def sut = new Problem1219PathWithMaximumGold() 11 | 12 | expect: 13 | sut.getMaximumGold(n as int[][]) == answer 14 | 15 | where: 16 | n || answer 17 | [[0, 6, 0], [5, 8, 7], [0, 9, 0]] || 24 18 | [[1, 0, 7], [2, 0, 6], [3, 4, 5], [0, 3, 0], [9, 0, 20]] || 28 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /java/src/test/groovy/leetcode/Problem121BestTimeToBuyAndSellStockSpec.groovy: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import spock.lang.Specification 4 | import spock.lang.Unroll 5 | 6 | final class Problem121BestTimeToBuyAndSellStockSpec extends Specification { 7 | @Unroll def 'maxProfit(#prices) = #answer'() { 8 | given: 9 | def sut = new Problem121BestTimeToBuyAndSellStock() 10 | 11 | expect: 12 | sut.maxProfit(prices as int[]) == answer 13 | 14 | where: 15 | prices || answer 16 | [7, 1, 5, 3, 6, 4] || 5 17 | [7, 6, 4, 3, 1] || 0 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /java/src/test/groovy/leetcode/Problem125ValidPalindromeSpec.groovy: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import spock.lang.Specification 4 | import spock.lang.Unroll 5 | 6 | final class Problem125ValidPalindromeSpec extends Specification { 7 | @Unroll def 'isPalindrome(#a) = #answer'() { 8 | given: 9 | def sut = new Problem125ValidPalindrome() 10 | 11 | expect: 12 | sut.isPalindrome(a) == answer 13 | 14 | where: 15 | a || answer 16 | null || false 17 | 'A man, a plan, a canal: Panama' || true 18 | 'race a car' || false 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /java/src/test/groovy/leetcode/Problem1295FindNumbersWithEvenNumberOfDigitsSpec.groovy: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import spock.lang.Specification 4 | import spock.lang.Unroll 5 | 6 | final class Problem1295FindNumbersWithEvenNumberOfDigitsSpec extends Specification { 7 | @Unroll def 'findNumbers(#nums) = #answer'() { 8 | given: 9 | def sut = new Problem1295FindNumbersWithEvenNumberOfDigits() 10 | 11 | expect: 12 | sut.findNumbers(nums as int[]) == answer 13 | sut.findNumbers2(nums as int[]) == answer 14 | 15 | where: 16 | nums || answer 17 | [12, 345, 2, 6, 7896] || 2 18 | [555, 901, 482, 1771] || 1 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /java/src/test/groovy/leetcode/Problem12IntegerToRomanSpec.groovy: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import spock.lang.Specification 4 | import spock.lang.Unroll 5 | 6 | final class Problem12IntegerToRomanSpec extends Specification { 7 | @Unroll 8 | def 'intToRoman(#num) = #answer'() { 9 | given: 10 | def sut = new Problem12IntegerToRoman() 11 | 12 | expect: 13 | sut.intToRoman(num) == answer 14 | sut.intToRoman2(num) == answer 15 | 16 | where: 17 | num || answer 18 | 3 || 'III' 19 | 4 || 'IV' 20 | 9 || 'IX' 21 | 58 || 'LVIII' 22 | 1994 || 'MCMXCIV' 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /java/src/test/groovy/leetcode/Problem136SingleNumberSpec.groovy: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import spock.lang.Specification 4 | import spock.lang.Unroll 5 | 6 | final class Problem136SingleNumberSpec extends Specification { 7 | @Unroll def 'singleNumber(#nums) = #answer'() { 8 | given: 9 | def sut = new Problem136SingleNumber() 10 | 11 | expect: 12 | sut.singleNumber(nums as int[]) == answer 13 | 14 | where: 15 | nums || answer 16 | null || 0 17 | [1, 1, 2, 2, 3, 4, 4, 5, 5] || 3 18 | [2, 2, 1] || 1 19 | [4, 1, 2, 1, 2] || 4 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /java/src/test/groovy/leetcode/Problem13RomanToIntegerSpec.groovy: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import spock.lang.Specification 4 | import spock.lang.Unroll 5 | 6 | final class Problem13RomanToIntegerSpec extends Specification { 7 | @Unroll def 'romanToInt(#s) = #answer'() { 8 | given: 9 | def sut = new Problem13RomanToInteger() 10 | 11 | expect: 12 | sut.romanToInt(s) == answer 13 | 14 | where: 15 | s || answer 16 | 'III' || 3 17 | 'IV' || 4 18 | 'IX' || 9 19 | 'LVIII' || 58 20 | 'MCMXCIV' || 1994 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /java/src/test/groovy/leetcode/Problem1486XOROperationInAnArraySpec.groovy: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import spock.lang.Specification 4 | import spock.lang.Unroll 5 | 6 | final class Problem1486XOROperationInAnArraySpec extends Specification { 7 | @Unroll def 'xorOperation(#n, #start) = #answer'() { 8 | given: 9 | def sut = new Problem1486XOROperationInAnArray() 10 | 11 | expect: 12 | sut.xorOperation(n, start) == answer 13 | 14 | where: 15 | n | start || answer 16 | 5 | 0 || 8 17 | 4 | 3 || 8 18 | 1 | 7 || 7 19 | 10 | 5 || 2 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /java/src/test/groovy/leetcode/Problem14LongestCommonPrefixSpec.groovy: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import spock.lang.Specification 4 | import spock.lang.Unroll 5 | 6 | final class Problem14LongestCommonPrefixSpec extends Specification { 7 | @Unroll def 'longestCommonPrefix(#strs) = #answer'() { 8 | given: 9 | def sut = new Problem14LongestCommonPrefix() 10 | 11 | expect: 12 | sut.longestCommonPrefix(strs as String[]) == answer 13 | 14 | where: 15 | strs || answer 16 | ['flower', 'flow', 'flight'] || 'fl' 17 | ['dog', 'racecar', 'car'] || '' 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /java/src/test/groovy/leetcode/Problem150EvaluateReversePolishNotationSpec.groovy: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import spock.lang.Specification 4 | import spock.lang.Unroll 5 | 6 | final class Problem150EvaluateReversePolishNotationSpec extends Specification { 7 | @Unroll def 'evalRPN(#tokens) = #answer'() { 8 | given: 9 | def sut = new Problem150EvaluateReversePolishNotation() 10 | 11 | expect: 12 | sut.evalRPN(tokens as String[]) == answer 13 | 14 | where: 15 | tokens || answer 16 | ['2', '1', '+', '3', '*'] || 9 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /java/src/test/groovy/leetcode/Problem151ReverseWordsInAStringSpec.groovy: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import spock.lang.Specification 4 | import spock.lang.Unroll 5 | 6 | final class Problem151ReverseWordsInAStringSpec extends Specification { 7 | @Unroll def 'reverseWords(#s) = #answer'() { 8 | given: 9 | def sut = new Problem151ReverseWordsInAString() 10 | 11 | expect: 12 | sut.reverseWords(s) == answer 13 | 14 | where: 15 | s || answer 16 | 'the sky is blue' || 'blue is sky the' 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /java/src/test/groovy/leetcode/Problem1523CountOddNumbersInAnIntervalRangeSpec.groovy: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import spock.lang.Specification 4 | import spock.lang.Unroll 5 | 6 | final class Problem1523CountOddNumbersInAnIntervalRangeSpec extends Specification { 7 | @Unroll def 'countOdds(#low, #high) = #answer'() { 8 | given: 9 | def sut = new Problem1523CountOddNumbersInAnIntervalRange() 10 | 11 | expect: 12 | sut.countOdds(low, high) == answer 13 | 14 | where: 15 | low | high || answer 16 | 327296043 | 769434803 || 221069381 17 | 3 | 7 || 3 18 | 8 | 10 || 1 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /java/src/test/groovy/leetcode/Problem153FindMinimumInRotatedSortedArraySpec.groovy: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import spock.lang.Specification 4 | import spock.lang.Unroll 5 | 6 | final class Problem153FindMinimumInRotatedSortedArraySpec extends Specification { 7 | @Unroll def 'longestCommonPrefix(#nums) = #answer'() { 8 | given: 9 | def sut = new Problem153FindMinimumInRotatedSortedArray() 10 | 11 | expect: 12 | sut.findMin(nums as int[]) == answer 13 | 14 | where: 15 | nums || answer 16 | [3, 4, 5, 1, 2] || 1 17 | [4, 5, 6, 7, 0, 1, 2] || 0 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /java/src/test/groovy/leetcode/Problem153SumSpec.groovy: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import spock.lang.Specification 4 | import spock.lang.Unroll 5 | 6 | final class Problem153SumSpec extends Specification { 7 | @Unroll def 'threeSum(#nums) = #answer'() { 8 | given: 9 | def sut = new Problem153Sum() 10 | 11 | expect: 12 | sut.threeSum(nums as int[]) == answer 13 | 14 | where: 15 | nums || answer 16 | [-1, 0, 1, 2, -1, -4] || [[-1, -1, 2], 17 | [-1, 0, 1]] 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /java/src/test/groovy/leetcode/Problem162FindPeakElementSpec.groovy: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import spock.lang.Specification 4 | import spock.lang.Unroll 5 | 6 | final class Problem162FindPeakElementSpec extends Specification { 7 | @Unroll def 'findPeakElement(#nums) = #answer'() { 8 | given: 9 | def sut = new Problem162FindPeakElement() 10 | 11 | expect: 12 | sut.findPeakElement(nums as int[]) == answer 13 | 14 | where: 15 | nums || answer 16 | [1, 2, 3, 1] || 2 17 | [1, 2, 1, 3, 5, 6, 4] || 5 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /java/src/test/groovy/leetcode/Problem165CompareVersionNumbersSpec.groovy: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import spock.lang.Specification 4 | import spock.lang.Unroll 5 | 6 | final class Problem165CompareVersionNumbersSpec extends Specification { 7 | @Unroll def 'compareVersion(#version1, #version2) = #answer'() { 8 | given: 9 | def sut = new Problem165CompareVersionNumbers() 10 | 11 | expect: 12 | sut.compareVersion(version1, version2) == answer 13 | 14 | where: 15 | version1 | version2 || answer 16 | '0.1' | '1.1' || -1 17 | '1.0.1' | '1' || 1 18 | '7.5.2.4' | '7.5.3' || -1 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /java/src/test/groovy/leetcode/Problem172FactorialTrailingZeroesSpec.groovy: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import spock.lang.Specification 4 | import spock.lang.Unroll 5 | 6 | final class Problem172FactorialTrailingZeroesSpec extends Specification { 7 | @Unroll def 'trailingZeroes(#a) = #answer'() { 8 | given: 9 | def sut = new Problem172FactorialTrailingZeroes() 10 | 11 | expect: 12 | sut.trailingZeroes(a) == answer 13 | 14 | where: 15 | a || answer 16 | 0 || 0 17 | 123000 || 30747 18 | 3 || 0 19 | 5 || 1 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /java/src/test/groovy/leetcode/Problem1748SumOfUniqueElementsSpec.groovy: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import spock.lang.Specification 4 | import spock.lang.Unroll 5 | 6 | final class Problem1748SumOfUniqueElementsSpec extends Specification { 7 | @Unroll def 'sumOfUnique(#nums) = #answer'() { 8 | given: 9 | def sut = new Problem1748SumOfUniqueElements() 10 | 11 | expect: 12 | sut.sumOfUnique(nums as int[]) == answer 13 | sut.sumOfUnique2(nums as int[]) == answer 14 | 15 | where: 16 | nums || answer 17 | [1, 2, 3, 2] || 4 18 | [1, 1, 1, 1, 1] || 0 19 | [1, 2, 3, 4, 5] || 15 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /java/src/test/groovy/leetcode/Problem179LargestNumberSpec.groovy: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import spock.lang.Specification 4 | import spock.lang.Unroll 5 | 6 | final class Problem179LargestNumberSpec extends Specification { 7 | @Unroll def 'largestNumber(#nums) = #answer'() { 8 | given: 9 | def sut = new Problem179LargestNumber() 10 | 11 | expect: 12 | sut.largestNumber(nums as int[]) == answer 13 | 14 | where: 15 | nums || answer 16 | [10, 2] || '210' 17 | [3, 30, 34, 5, 9] || '9534330' 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /java/src/test/groovy/leetcode/Problem17LetterCombinationsOfAPhoneNumberSpec.groovy: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import spock.lang.Specification 4 | import spock.lang.Unroll 5 | 6 | final class Problem17LetterCombinationsOfAPhoneNumberSpec extends Specification { 7 | @Unroll def 'letterCombinations(#digits) = #answer'() { 8 | given: 9 | def sut = new Problem17LetterCombinationsOfAPhoneNumber() 10 | 11 | expect: 12 | sut.letterCombinations(digits) == answer 13 | 14 | where: 15 | digits || answer 16 | '23' || ['ad', 'ae', 'af', 'bd', 'be', 'bf', 'cd', 'ce', 'cf'] 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /java/src/test/groovy/leetcode/Problem184SumSpec.groovy: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import spock.lang.Specification 4 | import spock.lang.Unroll 5 | 6 | final class Problem184SumSpec extends Specification { 7 | @Unroll def 'fourSum(#nums, #target) = #answer'() { 8 | given: 9 | def sut = new Problem184Sum() 10 | 11 | expect: 12 | sut.fourSum(nums as int[], target) == answer 13 | 14 | where: 15 | nums | target || answer 16 | [1, 0, -1, 0, -2, 2] | 0 || [[-2, -1, 1, 2], 17 | [-2, 0, 0, 2], 18 | [-1, 0, 0, 1]] 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /java/src/test/groovy/leetcode/Problem189RotateArraySpec.groovy: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import spock.lang.Specification 4 | import spock.lang.Unroll 5 | 6 | final class Problem189RotateArraySpec extends Specification { 7 | @Unroll def 'rotate(#nums, #k) = #answer'() { 8 | given: 9 | def sut = new Problem189RotateArray() 10 | 11 | expect: 12 | sut.rotate(nums as int[], k as int) 13 | 14 | where: 15 | nums | k || answer 16 | [1, 2, 3, 4, 5, 6, 7] | 3 || [5, 6, 7, 1, 2, 3, 4] 17 | [-1, -100, 3, 99] | 2 || [3, 99, -1, -100] 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /java/src/test/groovy/leetcode/Problem1TwoSumSpec.groovy: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import spock.lang.Specification 4 | import spock.lang.Unroll 5 | 6 | final class Problem1TwoSumSpec extends Specification { 7 | @Unroll def 'twoSum(#nums, #target) = #answer'() { 8 | given: 9 | def sut = new Problem1TwoSum() 10 | 11 | expect: 12 | sut.twoSum(nums as int[], target) == answer as int[] 13 | 14 | where: 15 | nums | target || answer 16 | null | 0 || [] 17 | [] | 0 || [] 18 | [1, 1] | 0 || [] 19 | [3, 2, 4] | 6 || [1, 2] 20 | [2, 7, 11, 15] | 9 || [0, 1] 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /java/src/test/groovy/leetcode/Problem20ValidParenthesesSpec.groovy: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import spock.lang.Specification 4 | import spock.lang.Unroll 5 | 6 | final class Problem20ValidParenthesesSpec extends Specification { 7 | @Unroll def 'isValid(#s) = #answer'() { 8 | given: 9 | def sut = new Problem20ValidParentheses() 10 | 11 | expect: 12 | sut.isValid(s) == answer 13 | 14 | where: 15 | s || answer 16 | '' || true 17 | '()' || true 18 | '()[]{}' || true 19 | '(]' || false 20 | '([)]' || false 21 | '{[]}' || true 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /java/src/test/groovy/leetcode/Problem215KthLargestElementInAnArraySpec.groovy: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import spock.lang.Specification 4 | import spock.lang.Unroll 5 | 6 | final class Problem215KthLargestElementInAnArraySpec extends Specification { 7 | @Unroll def 'findKthLargest(#nums, #k) = #answer'() { 8 | given: 9 | def sut = new Problem215KthLargestElementInAnArray() 10 | 11 | expect: 12 | sut.findKthLargest(nums as int[], k) == answer 13 | 14 | where: 15 | nums | k || answer 16 | [3, 2, 1, 5, 6, 4] | 2 || 5 17 | [3, 2, 3, 1, 2, 4, 5, 5, 6] | 4 || 4 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /java/src/test/groovy/leetcode/Problem22GenerateParenthesesSpec.groovy: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import spock.lang.Specification 4 | import spock.lang.Unroll 5 | 6 | final class Problem22GenerateParenthesesSpec extends Specification { 7 | @Unroll def 'generateParenthesis(#n) = #answer'() { 8 | given: 9 | def sut = new Problem22GenerateParentheses() 10 | 11 | expect: 12 | sut.generateParenthesis(n) == answer 13 | 14 | where: 15 | n || answer 16 | 0 || [''] 17 | 1 || ['()'] 18 | 2 || ['(())', '()()'] 19 | 3 || ['((()))', '(()())', '(())()', '()(())', '()()()'] 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /java/src/test/groovy/leetcode/Problem238ProductOfArrayExceptSelfSpec.groovy: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import spock.lang.Ignore 4 | import spock.lang.Specification 5 | import spock.lang.Unroll 6 | 7 | final class Problem238ProductOfArrayExceptSelfSpec extends Specification { 8 | @Ignore // Finish Problem238ProductOfArrayExceptSelf 9 | @Unroll def 'productExceptSelf(#nums) = #answer'() { 10 | given: 11 | def sut = new Problem238ProductOfArrayExceptSelf() 12 | 13 | expect: 14 | sut.productExceptSelf(nums as int[]) 15 | 16 | where: 17 | nums || answer 18 | null || [] 19 | [1, 2, 3, 4] || [24, 12, 8, 6] 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /java/src/test/groovy/leetcode/Problem242ValidAnagramSpec.groovy: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import spock.lang.Specification 4 | import spock.lang.Unroll 5 | 6 | final class Problem242ValidAnagramSpec extends Specification { 7 | @Unroll def 'isAnagram(#s, #t) = #answer'() { 8 | given: 9 | def sut = new Problem242ValidAnagram() 10 | 11 | expect: 12 | sut.isAnagram(s, t) == answer 13 | 14 | where: 15 | s | t || answer 16 | 'anagram' | 'nagaram' || true 17 | 'rat' | 'cat' || false 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /java/src/test/groovy/leetcode/Problem252MeetingRoomsSpec.groovy: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import leetcode.api.Interval 4 | import spock.lang.Specification 5 | import spock.lang.Unroll 6 | 7 | final class Problem252MeetingRoomsSpec extends Specification { 8 | @Unroll def 'canAttendMeetings(#intervals) = #answer'() { 9 | given: 10 | def sut = new Problem252MeetingRooms() 11 | 12 | expect: 13 | sut.canAttendMeetings(intervals as Interval[]) == answer 14 | 15 | where: 16 | intervals || answer 17 | [new Interval(0, 30), 18 | new Interval(5, 10), 19 | new Interval(15, 20)] || false 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /java/src/test/groovy/leetcode/Problem253MeetingRoomsIISpec.groovy: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import leetcode.api.Interval 4 | import spock.lang.Specification 5 | import spock.lang.Unroll 6 | 7 | final class Problem253MeetingRoomsIISpec extends Specification { 8 | @Unroll def 'canAttendMeetings(#intervals) = #answer'() { 9 | given: 10 | def sut = new Problem253MeetingRoomsII() 11 | 12 | expect: 13 | sut.minMeetingRooms(intervals as Interval[]) == answer 14 | 15 | where: 16 | intervals || answer 17 | [new Interval(0, 30), 18 | new Interval(5, 10), 19 | new Interval(15, 20)] || 2 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /java/src/test/groovy/leetcode/Problem268MissingNumberSpec.groovy: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import spock.lang.Specification 4 | import spock.lang.Unroll 5 | 6 | final class Problem268MissingNumberSpec extends Specification { 7 | @Unroll 8 | def 'missingNumber(#nums) = #answer'() { 9 | given: 10 | def sut = new Problem268MissingNumber() 11 | 12 | expect: 13 | sut.missingNumber(nums as int[]) == answer 14 | sut.missingNumber2(nums as int[]) == answer 15 | 16 | where: 17 | nums || answer 18 | [3, 0, 1] || 2 19 | [0, 1] || 2 20 | [9, 6, 4, 2, 3, 5, 7, 0, 1] || 8 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /java/src/test/groovy/leetcode/Problem273IntegerToEnglishWordsSpec.groovy: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import spock.lang.Specification 4 | import spock.lang.Unroll 5 | 6 | final class Problem273IntegerToEnglishWordsSpec extends Specification { 7 | @Unroll def 'numberToWords(#num) = #answer'() { 8 | given: 9 | def sut = new Problem273IntegerToEnglishWords() 10 | 11 | expect: 12 | sut.numberToWords(num) == answer 13 | 14 | where: 15 | num || answer 16 | 123 || 'One Hundred Twenty Three' 17 | 12345 || 'Twelve Thousand Three Hundred Forty Five' 18 | 1234567 || 'One Million Two Hundred Thirty Four Thousand Five Hundred Sixty Seven' 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /java/src/test/groovy/leetcode/Problem27RemoveElementSpec.groovy: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import spock.lang.Specification 4 | import spock.lang.Unroll 5 | 6 | final class Problem27RemoveElementSpec extends Specification { 7 | @Unroll def 'removeElement(#nums, #val) = #answer'() { 8 | given: 9 | def sut = new Problem27RemoveElement() 10 | 11 | expect: 12 | sut.removeElement(nums as int[], val) == answer 13 | 14 | where: 15 | nums | val || answer 16 | [3, 2, 2, 3] | 3 || 2 17 | [0, 1, 2, 2, 3, 0, 4, 2] | 2 || 5 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /java/src/test/groovy/leetcode/Problem28ImplementStrStrSpec.groovy: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import spock.lang.Specification 4 | import spock.lang.Unroll 5 | 6 | final class Problem28ImplementStrStrSpec extends Specification { 7 | @Unroll def 'strStr(#haystack, #needle) = #answer'() { 8 | given: 9 | def sut = new Problem28ImplementStrStr() 10 | 11 | expect: 12 | sut.strStr(haystack, needle) == answer 13 | 14 | where: 15 | haystack | needle || answer 16 | 'hello' | 'll' || 2 17 | 'aaaaa' | 'bba' || -1 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /java/src/test/groovy/leetcode/Problem290WordPatternSpec.groovy: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import spock.lang.Specification 4 | import spock.lang.Unroll 5 | 6 | final class Problem290WordPatternSpec extends Specification { 7 | @Unroll def 'wordPattern(#pattern, #str) = #answer'() { 8 | given: 9 | def sut = new Problem290WordPattern() 10 | 11 | expect: 12 | sut.wordPattern(pattern, str) == answer 13 | 14 | where: 15 | pattern | str || answer 16 | 'abba' | 'dog cat cat dog' || true 17 | 'abba' | 'dog cat cat fish' || false 18 | 'aaaa' | 'dog cat cat dog' || false 19 | 'abba' | 'dog dog dog dog' || false 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /java/src/test/groovy/leetcode/Problem292NimGameSpec.groovy: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import spock.lang.Specification 4 | import spock.lang.Unroll 5 | 6 | final class Problem292NimGameSpec extends Specification { 7 | @Unroll def 'canWinNim(#n) = #answer'() { 8 | given: 9 | def sut = new Problem292NimGame() 10 | 11 | expect: 12 | sut.canWinNim(n) == answer 13 | 14 | where: 15 | n || answer 16 | 1 || true 17 | 2 || true 18 | 3 || true 19 | 4 || false 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /java/src/test/groovy/leetcode/Problem33SearchInRotatedSortedArraySpec.groovy: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import spock.lang.Specification 4 | import spock.lang.Unroll 5 | 6 | final class Problem33SearchInRotatedSortedArraySpec extends Specification { 7 | @Unroll def 'longestCommonPrefix(#nums, #target) = #answer'() { 8 | given: 9 | def sut = new Problem33SearchInRotatedSortedArray() 10 | 11 | expect: 12 | sut.search(nums as int[], target) == answer 13 | 14 | where: 15 | nums | target || answer 16 | [4, 5, 6, 7, 0, 1, 2] | 0 || 4 17 | [4, 5, 6, 7, 0, 1, 2] | 3 || -1 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /java/src/test/groovy/leetcode/Problem344ReverseStringSpec.groovy: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import spock.lang.Specification 4 | import spock.lang.Unroll 5 | 6 | final class Problem344ReverseStringSpec extends Specification { 7 | @Unroll def 'reverseString(#s) = #answer'() { 8 | given: 9 | def sut = new Problem344ReverseString() 10 | 11 | expect: 12 | sut.reverseStringAnswer(s as char[]) == answer 13 | sut.reverseStringAnswer2(s as char[]) == answer 14 | 15 | where: 16 | s || answer 17 | 'hello' || 'olleh' 18 | 'the lazy fox jumped over the fence' || 'ecnef eht revo depmuj xof yzal eht' 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /java/src/test/groovy/leetcode/Problem345ReverseVowelsOfAStringSpec.groovy: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import spock.lang.Specification 4 | import spock.lang.Unroll 5 | 6 | final class Problem345ReverseVowelsOfAStringSpec extends Specification { 7 | @Unroll 8 | def 'reverseVowels(#s) = #result'() { 9 | given: 10 | def sut = new Problem345ReverseVowelsOfAString() 11 | 12 | expect: 13 | sut.reverseVowels(s) == result 14 | sut.reverseVowels2(s) == result 15 | 16 | where: 17 | s | result 18 | "hello" | "holle" 19 | "leetcode" | "leotcede" 20 | "aA" | "Aa" 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /java/src/test/groovy/leetcode/Problem347TopKFrequentElementsSpec.groovy: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import spock.lang.Specification 4 | import spock.lang.Unroll 5 | 6 | final class Problem347TopKFrequentElementsSpec extends Specification { 7 | @Unroll def 'topKFrequent(#nums, #k) = #answer'() { 8 | given: 9 | def sut = new Problem347TopKFrequentElements() 10 | 11 | expect: 12 | sut.topKFrequent(nums as int[], k) == answer 13 | 14 | where: 15 | nums | k || answer 16 | [1, 1, 1, 2, 2, 3] | 2 || [1, 2] 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /java/src/test/groovy/leetcode/Problem349IntersectionOfTwoArraysSpec.groovy: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import spock.lang.Specification 4 | import spock.lang.Unroll 5 | 6 | final class Problem349IntersectionOfTwoArraysSpec extends Specification { 7 | @Unroll def 'intersection(#nums1, #nums2) = #answer'() { 8 | given: 9 | def sut = new Problem349IntersectionOfTwoArrays() 10 | 11 | expect: 12 | sut.intersection(nums1 as int[], nums2 as int[]) == answer as int[] 13 | 14 | where: 15 | nums1 | nums2 || answer 16 | [1, 2, 2, 1] | [2, 2] || [2] 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /java/src/test/groovy/leetcode/Problem350IntersectionOfTwoArraysIISpec.groovy: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import spock.lang.Specification 4 | import spock.lang.Unroll 5 | 6 | final class Problem350IntersectionOfTwoArraysIISpec extends Specification { 7 | @Unroll def 'intersect(#nums1, #nums2) = #answer'() { 8 | given: 9 | def sut = new Problem350IntersectionOfTwoArraysII() 10 | 11 | expect: 12 | sut.intersect(nums1 as int[], nums2 as int[]) == answer as int[] 13 | 14 | where: 15 | nums1 | nums2 || answer 16 | [1, 2, 2, 1] | [2, 2] || [2, 2] 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /java/src/test/groovy/leetcode/Problem35SearchInsertPositionSpec.groovy: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import spock.lang.Specification 4 | import spock.lang.Unroll 5 | 6 | final class Problem35SearchInsertPositionSpec extends Specification { 7 | @Unroll def 'searchInsert(#nums, #target) = #answer'() { 8 | given: 9 | def sut = new Problem35SearchInsertPosition() 10 | 11 | expect: 12 | sut.searchInsert(nums as int[], target) == answer 13 | 14 | where: 15 | nums | target || answer 16 | [1, 3, 5, 6] | 5 || 2 17 | [1, 3, 5, 6] | 2 || 1 18 | [1, 3, 5, 6] | 7 || 4 19 | [1, 3, 5, 6] | 0 || 0 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /java/src/test/groovy/leetcode/Problem367ValidPerfectSquareSpec.groovy: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import spock.lang.Specification 4 | import spock.lang.Unroll 5 | 6 | final class Problem367ValidPerfectSquareSpec extends Specification { 7 | @Unroll def 'isPerfectSquare(#num) = #answer'() { 8 | given: 9 | def sut = new Problem367ValidPerfectSquare() 10 | 11 | expect: 12 | sut.isPerfectSquare(num) == answer 13 | 14 | where: 15 | num || answer 16 | 16 || true 17 | 14 || false 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /java/src/test/groovy/leetcode/Problem371SumOfTwoIntegersSpec.groovy: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import spock.lang.Specification 4 | import spock.lang.Unroll 5 | 6 | final class Problem371SumOfTwoIntegersSpec extends Specification { 7 | @Unroll def 'getSum(#a, #b) = #answer'() { 8 | given: 9 | def sut = new Problem371SumOfTwoIntegers() 10 | 11 | expect: 12 | sut.getSum(a, b) == answer 13 | 14 | where: 15 | a | b || answer 16 | 1 | 2 || 3 17 | 0 | 0 || 0 18 | 1 | 2 || 3 19 | 15 | 32 || 47 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /java/src/test/groovy/leetcode/Problem387FirstUniqueCharacterInAStringSpec.groovy: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import spock.lang.Specification 4 | import spock.lang.Unroll 5 | 6 | final class Problem387FirstUniqueCharacterInAStringSpec extends Specification { 7 | @Unroll def 'firstUniqChar(#s) = #answer'() { 8 | given: 9 | def sut = new Problem387FirstUniqueCharacterInAString() 10 | 11 | expect: 12 | sut.firstUniqChar(s) == answer 13 | 14 | where: 15 | s || answer 16 | 'leetcode' || 0 17 | 'loveleetcode' || 2 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /java/src/test/groovy/leetcode/Problem405ConvertANumberToHexadecimalSpec.groovy: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import spock.lang.Specification 4 | import spock.lang.Unroll 5 | 6 | final class Problem405ConvertANumberToHexadecimalSpec extends Specification { 7 | @Unroll def 'toHex(#num) = #answer'() { 8 | given: 9 | def sut = new Problem405ConvertANumberToHexadecimal() 10 | 11 | expect: 12 | sut.toHex(num) == answer 13 | 14 | where: 15 | num || answer 16 | 26 || '1a' 17 | -1 || 'ffffffff' 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /java/src/test/groovy/leetcode/Problem414ThirdMaximumNumberSpec.groovy: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import spock.lang.Specification 4 | import spock.lang.Unroll 5 | 6 | final class Problem414ThirdMaximumNumberSpec extends Specification { 7 | @Unroll def 'thirdMax(#nums) = #answer'() { 8 | given: 9 | def sut = new Problem414ThirdMaximumNumber() 10 | 11 | expect: 12 | sut.thirdMax(nums as int[]) == answer 13 | 14 | where: 15 | nums || answer 16 | [3, 2, 1] || 1 17 | [1, 2] || 2 18 | [2, 2, 3, 1] || 1 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /java/src/test/groovy/leetcode/Problem42TrappingRainWaterSpec.groovy: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import spock.lang.Specification 4 | import spock.lang.Unroll 5 | 6 | final class Problem42TrappingRainWaterSpec extends Specification { 7 | @Unroll def 'trap(#height) = #answer'() { 8 | given: 9 | def sut = new Problem42TrappingRainWater() 10 | 11 | expect: 12 | sut.trap(height as int[]) == answer 13 | 14 | where: 15 | height || answer 16 | [0, 1, 0, 2, 1, 0, 1, 3, 2, 1, 2, 1] || 6 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /java/src/test/groovy/leetcode/Problem451SortCharactersByFrequencySpec.groovy: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import spock.lang.Specification 4 | import spock.lang.Unroll 5 | 6 | final class Problem451SortCharactersByFrequencySpec extends Specification { 7 | @Unroll def 'frequencySort(#s) = #answer'() { 8 | given: 9 | def sut = new Problem451SortCharactersByFrequency() 10 | 11 | expect: 12 | sut.frequencySort(s) == answer 13 | 14 | where: 15 | s || answer 16 | "tree" || "eert" 17 | "cccaaa" || "aaaccc" 18 | "Aabb" || "bbAa" 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /java/src/test/groovy/leetcode/Problem4544SumIISpec.groovy: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import spock.lang.Specification 4 | import spock.lang.Unroll 5 | 6 | final class Problem4544SumIISpec extends Specification { 7 | @Unroll def 'fourSumCount(#A, #B, #C, #D) = #answer'() { 8 | given: 9 | def sut = new Problem4544SumII() 10 | 11 | expect: 12 | sut.fourSumCount(A as int[], B as int[], C as int[], D as int[]) == answer 13 | 14 | where: 15 | A | B | C | D || answer 16 | [1, 2] | [-2, -1] | [-1, 2] | [0, 2] || 2 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /java/src/test/groovy/leetcode/Problem485MaxConsecutiveOnesSpec.groovy: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import spock.lang.Specification 4 | import spock.lang.Unroll 5 | 6 | final class Problem485MaxConsecutiveOnesSpec extends Specification { 7 | @Unroll def 'findMaxConsecutiveOnes(#nums) = #answer'() { 8 | given: 9 | def sut = new Problem485MaxConsecutiveOnes() 10 | 11 | expect: 12 | sut.findMaxConsecutiveOnes(nums as int[]) == answer 13 | 14 | where: 15 | nums || answer 16 | null || 0 17 | [1, 1, 0, 1, 1, 1] || 3 18 | [1, 0, 1, 1, 0, 1] || 2 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /java/src/test/groovy/leetcode/Problem500KeyboardRowSpec.groovy: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import spock.lang.Specification 4 | import spock.lang.Unroll 5 | 6 | final class Problem500KeyboardRowSpec extends Specification { 7 | @Unroll def 'findWords(#words) = #answer'() { 8 | given: 9 | def sut = new Problem500KeyboardRow() 10 | 11 | expect: 12 | sut.findWords(words as String[]) == answer as String[] 13 | 14 | where: 15 | words || answer 16 | null || [] 17 | ['Hello', 'Alaska', 'Dad', 'Peace'] || ['Alaska', 'Dad'] 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /java/src/test/groovy/leetcode/Problem504Base7Spec.groovy: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import spock.lang.Specification 4 | import spock.lang.Unroll 5 | 6 | final class Problem504Base7Spec extends Specification { 7 | @Unroll def 'convertToBase7(#num) = #answer'() { 8 | given: 9 | def sut = new Problem504Base7() 10 | 11 | expect: 12 | sut.convertToBase7(num) == answer 13 | 14 | where: 15 | num || answer 16 | 100 || '202' 17 | -7 || '-10' 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /java/src/test/groovy/leetcode/Problem509FibonacciNumberSpec.groovy: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import spock.lang.Specification 4 | import spock.lang.Unroll 5 | 6 | final class Problem509FibonacciNumberSpec extends Specification { 7 | @Unroll def 'fibonacci(#a) == #b'() { 8 | given: 9 | def sut = new Problem509FibonacciNumber() 10 | 11 | expect: 12 | sut.fib(a) == b 13 | sut.fib2(a) == b 14 | 15 | where: 16 | a || b 17 | -5 || -5 18 | -1 || -1 19 | 0 || 0 20 | 1 || 1 21 | 2 || 1 22 | 3 || 2 23 | 4 || 3 24 | 5 || 5 25 | 6 || 8 26 | 7 || 13 27 | 8 || 21 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /java/src/test/groovy/leetcode/Problem50PowSpec.groovy: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import spock.lang.Specification 4 | import spock.lang.Unroll 5 | 6 | final class Problem50PowSpec extends Specification { 7 | @Unroll def 'myPow(#x, #n) = #answer'() { 8 | given: 9 | def sut = new Problem50Pow() 10 | 11 | expect: 12 | sut.myPow(x, n) == answer.toDouble() 13 | 14 | where: 15 | x | n || answer 16 | 2.00000 | 10 || 1024.00000 17 | // 2.10000 | 3 || 9.26100 // TODO double issue 18 | 2.00000 | -2 || 0.25000 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /java/src/test/groovy/leetcode/Problem520DetectCapitalSpec.groovy: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import spock.lang.Specification 4 | import spock.lang.Unroll 5 | 6 | final class Problem520DetectCapitalSpec extends Specification { 7 | @Unroll def 'detectCapitalUse(#word) = #answer'() { 8 | given: 9 | def sut = new Problem520DetectCapital() 10 | 11 | expect: 12 | sut.detectCapitalUse(word) == answer 13 | 14 | where: 15 | word || answer 16 | 'USA' || true 17 | 'FlaG' || false 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /java/src/test/groovy/leetcode/Problem532KDiffPairsInAnArraySpec.groovy: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import spock.lang.Specification 4 | import spock.lang.Unroll 5 | 6 | final class Problem532KDiffPairsInAnArraySpec extends Specification { 7 | @Unroll def 'findPairs(#nums, #k) = #answer'() { 8 | given: 9 | def sut = new Problem532KDiffPairsInAnArray() 10 | 11 | expect: 12 | sut.findPairs(nums as int[], k) == answer 13 | 14 | where: 15 | nums | k || answer 16 | [3, 1, 4, 1, 5] | 2 || 2 17 | [1, 2, 3, 4, 5] | 1 || 4 18 | [1, 3, 1, 5, 4] | 0 || 1 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /java/src/test/groovy/leetcode/Problem53MaximumSubarraySpec.groovy: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import spock.lang.Specification 4 | import spock.lang.Unroll 5 | 6 | final class Problem53MaximumSubarraySpec extends Specification { 7 | @Unroll def 'maxSubArray(#nums) = #answer'() { 8 | given: 9 | def sut = new Problem53MaximumSubarray() 10 | 11 | expect: 12 | sut.maxSubArray(nums as int[]) == answer 13 | 14 | where: 15 | nums || answer 16 | [-2, 1, -3, 4, -1, 2, 1, -5, 4] || 6 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /java/src/test/groovy/leetcode/Problem541ReverseStringIISpec.groovy: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import spock.lang.Specification 4 | import spock.lang.Unroll 5 | 6 | final class Problem541ReverseStringIISpec extends Specification { 7 | @Unroll def 'reverseStr(#s, #k) = #answer'() { 8 | given: 9 | def sut = new Problem541ReverseStringII() 10 | 11 | expect: 12 | sut.reverseStr(s, k) == answer 13 | 14 | where: 15 | s | k || answer 16 | null | 0 || '' 17 | 'abcdefg' | 2 || 'bacdfeg' 18 | 'abcdef' | 2 || 'bacdfe' 19 | 'abcdef' | 3 || 'cbadef' 20 | // 'abcdefg' | 8 || 'cbadef' 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /java/src/test/groovy/leetcode/Problem557ReverseWordsInAStringIIISpec.groovy: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import spock.lang.Specification 4 | import spock.lang.Unroll 5 | 6 | final class Problem557ReverseWordsInAStringIIISpec extends Specification { 7 | @Unroll def 'reverseWords(#s) = #answer'() { 8 | given: 9 | def sut = new Problem557ReverseWordsInAStringIII() 10 | 11 | expect: 12 | sut.reverseWords(s) == answer 13 | 14 | where: 15 | s || answer 16 | 'Let\'s take LeetCode contest' || 's\'teL ekat edoCteeL tsetnoc' 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /java/src/test/groovy/leetcode/Problem55JumpGameSpec.groovy: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import spock.lang.Specification 4 | import spock.lang.Unroll 5 | 6 | final class Problem55JumpGameSpec extends Specification { 7 | @Unroll def 'canJump(#nums) = #answer'() { 8 | given: 9 | def sut = new Problem55JumpGame() 10 | 11 | expect: 12 | sut.canJump(nums as int[]) == answer 13 | 14 | where: 15 | nums || answer 16 | [2, 3, 1, 1, 4] || true 17 | [3, 2, 1, 0, 4] || false 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /java/src/test/groovy/leetcode/Problem560SubarraySumEqualsKSpec.groovy: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import spock.lang.Specification 4 | import spock.lang.Unroll 5 | 6 | final class Problem560SubarraySumEqualsKSpec extends Specification { 7 | @Unroll def 'subarraySum(#nums, #k) = #answer'() { 8 | given: 9 | def sut = new Problem560SubarraySumEqualsK() 10 | 11 | expect: 12 | sut.subarraySum(nums as int[], k) == answer 13 | 14 | where: 15 | nums | k || answer 16 | [1, 1, 1] | 2 || 2 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /java/src/test/groovy/leetcode/Problem56MergeIntervalsSpec.groovy: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import spock.lang.Specification 4 | import spock.lang.Unroll 5 | 6 | final class Problem56MergeIntervalsSpec extends Specification { 7 | @Unroll def 'merge(#intervals) = #answer'() { 8 | given: 9 | def sut = new Problem56MergeIntervals() 10 | 11 | expect: 12 | sut.merge(intervals as int[][]) == answer as int[][] 13 | 14 | where: 15 | intervals || answer 16 | [[1, 3], [2, 6], [8, 10], [15, 18]] || [[1, 6], [8, 10], [15, 18]] 17 | [[1, 4], [4, 5]] || [[1, 5]] 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /java/src/test/groovy/leetcode/Problem657JudgeRouteCircleSpec.groovy: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import spock.lang.Specification 4 | import spock.lang.Unroll 5 | 6 | final class Problem657JudgeRouteCircleSpec extends Specification { 7 | @Unroll def 'judgeCircle(#moves) = #answer'() { 8 | given: 9 | def sut = new Problem657JudgeRouteCircle() 10 | 11 | expect: 12 | sut.judgeCircle(moves) == answer 13 | 14 | where: 15 | moves || answer 16 | null || false 17 | 'UD' || true 18 | 'LL' || false 19 | 'RLUURDDDLU' || true 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /java/src/test/groovy/leetcode/Problem66PlusOneSpec.groovy: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import spock.lang.Specification 4 | import spock.lang.Unroll 5 | 6 | final class Problem66PlusOneSpec extends Specification { 7 | @Unroll def 'plusOne(#digits) = #answer'() { 8 | given: 9 | def sut = new Problem66PlusOne() 10 | 11 | expect: 12 | sut.plusOne(digits as int[]) == answer as int[] 13 | 14 | where: 15 | digits || answer 16 | [1, 2, 3] || [1, 2, 4] 17 | [4, 3, 2, 1] || [4, 3, 2, 2] 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /java/src/test/groovy/leetcode/Problem67AddBinarySpec.groovy: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import spock.lang.Specification 4 | import spock.lang.Unroll 5 | 6 | final class Problem67AddBinarySpec extends Specification { 7 | @Unroll def 'addBinary(#a, #b) = #answer'() { 8 | given: 9 | def sut = new Problem67AddBinary() 10 | 11 | expect: 12 | sut.addBinary(a, b) == answer 13 | 14 | where: 15 | a | b || answer 16 | '11' | '1' || '100' 17 | '1010' | '1011' || '10101' 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /java/src/test/groovy/leetcode/Problem686RepeatedStringMatchSpec.groovy: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import spock.lang.Specification 4 | import spock.lang.Unroll 5 | 6 | final class Problem686RepeatedStringMatchSpec extends Specification { 7 | @Unroll def 'repeatedStringMatch(#A, #B) = #answer'() { 8 | given: 9 | def sut = new Problem686RepeatedStringMatch() 10 | 11 | expect: 12 | sut.repeatedStringMatch(A, B) == answer 13 | 14 | where: 15 | A | B || answer 16 | 'abcd' | 'cdabcdab' || 3 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /java/src/test/groovy/leetcode/Problem69SqrtSpec.groovy: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import spock.lang.Specification 4 | import spock.lang.Unroll 5 | 6 | final class Problem69SqrtSpec extends Specification { 7 | @Unroll def 'mySqrt(#x) = #answer'() { 8 | given: 9 | def sut = new Problem69Sqrt() 10 | 11 | expect: 12 | sut.mySqrt(x) == answer 13 | 14 | where: 15 | x || answer 16 | 4 || 2 17 | 8 || 2 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /java/src/test/groovy/leetcode/Problem709ToLowerCaseSpec.groovy: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import spock.lang.Specification 4 | import spock.lang.Unroll 5 | 6 | final class Problem709ToLowerCaseSpec extends Specification { 7 | @Unroll def 'toLowerCase(#str) = #answer'() { 8 | given: 9 | def sut = new Problem709ToLowerCase() 10 | 11 | expect: 12 | sut.toLowerCase(str) == answer 13 | 14 | where: 15 | str || answer 16 | 'Hello' || 'hello' 17 | 'here' || 'here' 18 | 'LOVELY' || 'lovely' 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /java/src/test/groovy/leetcode/Problem724FindPivotIndexSpec.groovy: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import spock.lang.Specification 4 | import spock.lang.Unroll 5 | 6 | final class Problem724FindPivotIndexSpec extends Specification { 7 | @Unroll def 'pivotIndex(#nums) = #answer'() { 8 | given: 9 | def sut = new Problem724FindPivotIndex() 10 | 11 | expect: 12 | sut.pivotIndex(nums as int[]) == answer 13 | 14 | where: 15 | nums || answer 16 | [1, 7, 3, 6, 5, 6] || 3 17 | [1, 2, 3] || -1 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /java/src/test/groovy/leetcode/Problem760FindAnagramMappingsSpec.groovy: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import spock.lang.Specification 4 | import spock.lang.Unroll 5 | 6 | final class Problem760FindAnagramMappingsSpec extends Specification { 7 | @Unroll def 'anagramMappings(#A, #B) = #answer'() { 8 | given: 9 | def sut = new Problem760FindAnagramMappings() 10 | 11 | expect: 12 | sut.anagramMappings(A as int[], B as int[]) == answer as int[] 13 | 14 | where: 15 | A | B | answer 16 | [12, 28, 46, 32, 50] | [50, 12, 32, 46, 28] || [1, 4, 3, 2, 0] 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /java/src/test/groovy/leetcode/Problem769MaxChunksToMakeSortedSpec.groovy: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import spock.lang.Specification 4 | import spock.lang.Unroll 5 | 6 | final class Problem769MaxChunksToMakeSortedSpec extends Specification { 7 | @Unroll def 'maxChunksToSorted(#arr) = #answer'() { 8 | given: 9 | def sut = new Problem769MaxChunksToMakeSorted() 10 | 11 | expect: 12 | sut.maxChunksToSorted(arr as int[]) == answer 13 | 14 | where: 15 | arr || answer 16 | [4, 3, 2, 1, 0] || 1 17 | [1, 0, 2, 3, 4] || 4 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /java/src/test/groovy/leetcode/Problem771JewelsAndStonesSpec.groovy: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import spock.lang.Specification 4 | import spock.lang.Unroll 5 | 6 | final class Problem771JewelsAndStonesSpec extends Specification { 7 | @Unroll def 'isValid(#J, #S) = #answer'() { 8 | given: 9 | def sut = new Problem771JewelsAndStones() 10 | 11 | expect: 12 | sut.numJewelsInStones(J, S) == answer 13 | 14 | where: 15 | J | S || answer 16 | 'aA' | 'aAAbbbb' || 3 17 | 'z' | 'ZZ' || 0 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /java/src/test/groovy/leetcode/Problem77CombinationsSpec.groovy: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import spock.lang.Specification 4 | import spock.lang.Unroll 5 | 6 | final class Problem77CombinationsSpec extends Specification { 7 | @Unroll def 'combine(#n, #k) = #answer'() { 8 | given: 9 | def sut = new Problem77Combinations() 10 | 11 | expect: 12 | sut.combine(n, k) == answer 13 | 14 | where: 15 | n | k || answer 16 | 4 | 2 || [[1, 2], 17 | [1, 3], 18 | [1, 4], 19 | [2, 3], 20 | [2, 4], 21 | [3, 4]] 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /java/src/test/groovy/leetcode/Problem796RotateStringSpec.groovy: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import spock.lang.Specification 4 | import spock.lang.Unroll 5 | 6 | final class Problem796RotateStringSpec extends Specification { 7 | @Unroll def 'rotateString(#A, #B) = #answer'() { 8 | given: 9 | def sut = new Problem796RotateString() 10 | 11 | expect: 12 | sut.rotateString(A, B) == answer 13 | 14 | where: 15 | A | B || answer 16 | 'abcde' | 'cdeab' || true 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /java/src/test/groovy/leetcode/Problem80RemoveDuplicatesFromSortedArrayIISpec.groovy: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import spock.lang.Specification 4 | import spock.lang.Unroll 5 | 6 | final class Problem80RemoveDuplicatesFromSortedArrayIISpec extends Specification { 7 | @Unroll def 'removeDuplicates(#nums) = #answer'() { 8 | given: 9 | def sut = new Problem80RemoveDuplicatesFromSortedArrayII() 10 | 11 | expect: 12 | sut.removeDuplicates(nums as int[]) == answer 13 | 14 | where: 15 | nums || answer 16 | [1, 1, 1, 2, 2, 3] || 5 17 | [0, 0, 1, 1, 1, 1, 2, 3, 3] || 7 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /java/src/test/groovy/leetcode/Problem88MergeSortedArraySpec.groovy: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import spock.lang.Specification 4 | import spock.lang.Unroll 5 | 6 | final class Problem88MergeSortedArraySpec extends Specification { 7 | @Unroll def 'merge(#nums1, #m, #nums2, #n) = #answer'() { 8 | given: 9 | def sut = new Problem88MergeSortedArray() 10 | 11 | expect: 12 | sut.merge(nums1 as int[], m, nums2 as int[], n) 13 | 14 | where: 15 | nums1 | m | nums2 | n || answer 16 | [1, 2, 3, 0, 0, 0] | 3 | [2, 5, 6] | 3 || [1, 2, 2, 3, 5, 6] 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /java/src/test/groovy/leetcode/Problem905SortArrayByParitySpec.groovy: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import spock.lang.Specification 4 | import spock.lang.Unroll 5 | 6 | final class Problem905SortArrayByParitySpec extends Specification { 7 | @Unroll def 'sortArrayByParity(#A) = #answer'() { 8 | given: 9 | def sut = new Problem905SortArrayByParity() 10 | 11 | expect: 12 | sut.sortArrayByParity(A as int[]) == answer as int[] 13 | 14 | where: 15 | A || answer 16 | [0] || [0] 17 | [0, 2] || [0, 2] 18 | [3, 1, 2, 4] || [4, 2, 1, 3] 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /java/src/test/groovy/leetcode/Problem90SubsetsIISpec.groovy: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import spock.lang.Specification 4 | import spock.lang.Unroll 5 | 6 | final class Problem90SubsetsIISpec extends Specification { 7 | @Unroll 8 | def 'subsetsWithDup(#nums) = #answer'() { 9 | given: 10 | def sut = new Problem90SubsetsII() 11 | 12 | expect: 13 | sut.subsetsWithDup(nums as int[]).sort() == answer.sort() 14 | 15 | where: 16 | nums || answer 17 | [1, 2, 2] || [[], [1], [1, 2], [1, 2, 2], [2], [2, 2]] 18 | [0] || [[], [0]] 19 | [2, 3, 4] || [[], [2], [2, 3], [2, 3, 4], [2, 4], [3], [3, 4], [4]] 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /java/src/test/groovy/projecteuler/Number001Spec.groovy: -------------------------------------------------------------------------------- 1 | package projecteuler 2 | 3 | import spock.lang.Specification 4 | 5 | final class Number001Spec extends Specification { 6 | def 'getMultiplesOf3And5'() { 7 | expect: 8 | Number001.getMultiplesOf3And5() == 233168 9 | Number001.getMultiplesOf3And52() == 233168 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /java/src/test/groovy/projecteuler/Number002Spec.groovy: -------------------------------------------------------------------------------- 1 | package projecteuler 2 | 3 | import spock.lang.Specification 4 | 5 | final class Number002Spec extends Specification { 6 | def 'getEvenFibonacciNumbers'() { 7 | expect: 8 | Number002.getEvenFibonacciNumbers(4000000) == 4613732 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /java/src/test/groovy/projecteuler/Number003Spec.groovy: -------------------------------------------------------------------------------- 1 | package projecteuler 2 | 3 | import spock.lang.Specification 4 | 5 | final class Number003Spec extends Specification { 6 | def 'getLargestPrimeFactor'() { 7 | expect: 8 | Number003.getLargestPrimeFactor(600851475143L) == 6857 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /java/src/test/groovy/projecteuler/Number004Spec.groovy: -------------------------------------------------------------------------------- 1 | package projecteuler 2 | 3 | import spock.lang.Specification 4 | 5 | final class Number004Spec extends Specification { 6 | def 'getLargestPalindromeProduct'() { 7 | expect: 8 | Number004.getLargestPalindromeProduct() == 906609 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /java/src/test/groovy/questions/PermutationsSpec.groovy: -------------------------------------------------------------------------------- 1 | package questions 2 | 3 | import questions.Permutations 4 | import spock.lang.Specification 5 | import spock.lang.Unroll 6 | 7 | final class PermutationsSpec extends Specification { 8 | @Unroll def 'getStringPermutations(\'\', #a) == #b'() { 9 | expect: 10 | Permutations.getStringPermutations('', a) == b 11 | 12 | where: 13 | a || b 14 | null || [] 15 | '' || [''] 16 | 'abc' || ['abc', 'acb', 'bca', 'bac', 'cab', 'cba'] 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /java/src/test/groovy/questions/ReverseArraySpec.groovy: -------------------------------------------------------------------------------- 1 | package questions 2 | 3 | import questions.ReverseArray 4 | import spock.lang.Specification 5 | import spock.lang.Unroll 6 | 7 | final class ReverseArraySpec extends Specification { 8 | @Unroll def 'reverse - multiple arg(#a, #b, #c) == #d'() { 9 | expect: 10 | ReverseArray.reverse(a as int[], b, c) == d as int[] 11 | 12 | where: 13 | a | b | c || d 14 | null | 2 | 2 || [] 15 | [1] | 2 | 2 || [1] 16 | [1, 2, 3, 4, 5, 6] | 4 | 5 || [1, 2, 3, 4, 6, 5] 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /java/src/test/groovy/questions/SearchAdjacentElementsSpec.groovy: -------------------------------------------------------------------------------- 1 | package questions 2 | 3 | import questions.SearchAdjacentElements 4 | import spock.lang.Specification 5 | import spock.lang.Unroll 6 | 7 | final class SearchAdjacentElementsSpec extends Specification { 8 | @Unroll def 'search(#a, #b) == #c'() { 9 | expect: 10 | SearchAdjacentElements.search(a as int[], b) == c 11 | SearchAdjacentElements.search2(a as int[], b) == c 12 | 13 | where: 14 | a | b || c 15 | [1, 2, 3, 4, 5, 4, 5, 6, 5, 6] | 9 || -1 16 | [1, 2, 3, 4, 5, 4, 5, 6, 5, 6] | 6 || 7 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /java/src/test/groovy/questions/TrailingZerosSpec.groovy: -------------------------------------------------------------------------------- 1 | package questions 2 | 3 | import questions.TrailingZeros 4 | import spock.lang.Specification 5 | import spock.lang.Unroll 6 | 7 | final class TrailingZerosSpec extends Specification { 8 | @Unroll def 'getTrailingZeros(#a) == #b'() { 9 | expect: 10 | TrailingZeros.getTrailingZeros(a) == b 11 | 12 | where: 13 | a || b 14 | 0 || 0 15 | 9 || 0 // none 16 | 5 || 0 // none 17 | 10 || 1 18 | 10011013000 || 3 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /kotlin/src/main/kotlin/api/Color.kt: -------------------------------------------------------------------------------- 1 | package api 2 | 3 | enum class Color { 4 | GREEN, 5 | RED, 6 | BLUE 7 | } 8 | -------------------------------------------------------------------------------- /kotlin/src/main/kotlin/api/GraphNode.kt: -------------------------------------------------------------------------------- 1 | package api 2 | 3 | data class GraphNode(var value: T? = null, var neighbors: List>?) { 4 | var next: GraphNode? = null 5 | val visited: Boolean = false 6 | 7 | constructor() : this(null) 8 | constructor(value: T?): this(value, null) 9 | } 10 | -------------------------------------------------------------------------------- /kotlin/src/main/kotlin/api/Node.kt: -------------------------------------------------------------------------------- 1 | package api 2 | 3 | data class Node(var value: T? = null) { 4 | var next: Node? = null 5 | var previous: Node? = null 6 | 7 | constructor() : this(null) 8 | } 9 | -------------------------------------------------------------------------------- /kotlin/src/main/kotlin/api/Pair.kt: -------------------------------------------------------------------------------- 1 | package api 2 | 3 | data class Pair(val left: L, val right: R) 4 | -------------------------------------------------------------------------------- /kotlin/src/main/kotlin/api/Point.kt: -------------------------------------------------------------------------------- 1 | package api 2 | 3 | data class Point(val x: Int, val y: Int) 4 | -------------------------------------------------------------------------------- /kotlin/src/main/kotlin/api/Queue.kt: -------------------------------------------------------------------------------- 1 | package api 2 | 3 | // FIFO - First in first out 4 | data class Queue(private var value: T?) { 5 | var first: Node? = null 6 | var last: Node? = null 7 | 8 | init { 9 | enqueue(Node(value)) 10 | } 11 | 12 | constructor() : this(null) 13 | 14 | fun enqueue(node: Node) { 15 | if (first == null) { 16 | first = node 17 | last = first 18 | } else { 19 | last?.next = node 20 | last = node 21 | } 22 | } 23 | 24 | fun dequeue() { 25 | if (first != null) { 26 | first = first?.next 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /kotlin/src/main/kotlin/api/Stack.kt: -------------------------------------------------------------------------------- 1 | package api 2 | 3 | data class Stack(private var value: T?) { 4 | var peek: Node? = null 5 | 6 | constructor() : this(null) 7 | 8 | fun push(node: Node?) { 9 | if (node != null) { 10 | node.next = peek 11 | peek = node 12 | } 13 | } 14 | 15 | fun pop() { 16 | if (peek != null) { 17 | peek = peek?.next 18 | } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /kotlin/src/main/kotlin/api/TreeNode.kt: -------------------------------------------------------------------------------- 1 | package api 2 | 3 | data class TreeNode(var value: T? = null) { 4 | var left: TreeNode? = null 5 | var right: TreeNode? = null 6 | 7 | constructor() : this(null) 8 | } 9 | -------------------------------------------------------------------------------- /kotlin/src/main/kotlin/api/TrieNode.kt: -------------------------------------------------------------------------------- 1 | package api 2 | 3 | class TrieNode { 4 | companion object { 5 | private const val ALPHABET_COUNT = 26 6 | } 7 | 8 | var children: Array> 9 | var isEnd: Boolean = false 10 | 11 | constructor() : this(ALPHABET_COUNT) 12 | 13 | constructor(capacity: Int) { 14 | @Suppress("UNCHECKED_CAST") 15 | children = arrayOfNulls>(capacity) as Array> 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /kotlin/src/main/kotlin/api/Trio.kt: -------------------------------------------------------------------------------- 1 | package api 2 | 3 | data class Trio(val first: F, val second: S, val third: T) 4 | -------------------------------------------------------------------------------- /kotlin/src/main/kotlin/api/util/Swap.kt: -------------------------------------------------------------------------------- 1 | package api.util 2 | 3 | class Swap { 4 | companion object { 5 | fun swap(array: Array, left: Int, right: Int) { 6 | val temp: T = array[left] 7 | array[left] = array[right] 8 | array[right] = temp 9 | } 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /kotlin/src/main/kotlin/exercism/HelloWorld.kt: -------------------------------------------------------------------------------- 1 | package exercism 2 | 3 | // Time - O(1), Space - O(1) 4 | fun hello(): String = "Hello, World!" 5 | -------------------------------------------------------------------------------- /kotlin/src/main/kotlin/exercism/ReverseString.kt: -------------------------------------------------------------------------------- 1 | package exercism 2 | 3 | fun reverse(input: String): String = input.reversed() 4 | -------------------------------------------------------------------------------- /kotlin/src/main/kotlin/exercism/Twofer.kt: -------------------------------------------------------------------------------- 1 | package exercism 2 | 3 | // Time - O(1), Space - O(1) 4 | fun twofer(name: String = "you"): String = "One for $name, one for me." 5 | -------------------------------------------------------------------------------- /kotlin/src/main/kotlin/koans/conventions.kt: -------------------------------------------------------------------------------- 1 | package koans 2 | 3 | -------------------------------------------------------------------------------- /kotlin/src/main/kotlin/koans/generics.kt: -------------------------------------------------------------------------------- 1 | package koans 2 | 3 | -------------------------------------------------------------------------------- /kotlin/src/main/kotlin/leetcode/ExploreInsertIntoABinarySearchTree.kt: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | import leetcode.api.TreeNode 4 | 5 | /** 6 | * https://leetcode.com/explore/learn/card/introduction-to-data-structure-binary-search-tree/141/basic-operations-in-a-bst/1003/ 7 | */ 8 | class ExploreInsertIntoABinarySearchTree { 9 | fun insertIntoBST(root: TreeNode? = null, value: Int): TreeNode { 10 | if (root == null) { 11 | return TreeNode(value) 12 | } 13 | 14 | if (value > root.value) { 15 | root.right = insertIntoBST(root.right, value) 16 | } else { 17 | root.left = insertIntoBST(root.left, value) 18 | } 19 | 20 | return root 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /kotlin/src/main/kotlin/leetcode/Problem412FizzBuzz.kt: -------------------------------------------------------------------------------- 1 | package leetcode 2 | 3 | class Problem412FizzBuzz { 4 | // Time - O(N), Space - O(N) 5 | fun fizzBuzz(n: Int): List { 6 | val words = arrayListOf() 7 | for (i in 0 .. n) { 8 | if (i % 3 == 0 && i % 5 ==0) { 9 | words.add("FizzBuzz") 10 | } else if (i % 3 == 0) { 11 | words.add("Fizz") 12 | } else if (i % 5 == 0) { 13 | words.add("Buzz") 14 | } else { 15 | words.add(i.toString()) 16 | } 17 | 18 | } 19 | 20 | return words 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /kotlin/src/main/kotlin/leetcode/api/Interval.kt: -------------------------------------------------------------------------------- 1 | package leetcode.api 2 | 3 | class Interval { 4 | var start: Int = 0 5 | var end: Int = 0 6 | 7 | constructor() { 8 | start = 0 9 | end = 0 10 | } 11 | 12 | constructor(s: Int, e: Int) { 13 | start = s 14 | end = e 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /kotlin/src/main/kotlin/leetcode/api/ListNode.kt: -------------------------------------------------------------------------------- 1 | package leetcode.api 2 | 3 | class ListNode(var value: Int) { 4 | var next: ListNode? = null 5 | } 6 | -------------------------------------------------------------------------------- /kotlin/src/main/kotlin/leetcode/api/TreeLinkNode.kt: -------------------------------------------------------------------------------- 1 | package leetcode.api 2 | 3 | class TreeLinkNode(var value: Int) { 4 | var left: TreeLinkNode? = null 5 | var right: TreeLinkNode? = null 6 | var next: TreeLinkNode? = null 7 | } 8 | -------------------------------------------------------------------------------- /kotlin/src/main/kotlin/leetcode/api/TreeNode.kt: -------------------------------------------------------------------------------- 1 | package leetcode.api 2 | 3 | class TreeNode(var value: Int) { 4 | var left: TreeNode? = null 5 | var right: TreeNode? = null 6 | } 7 | -------------------------------------------------------------------------------- /kotlin/src/main/kotlin/leetcode/api/UndirectedGraphNode.kt: -------------------------------------------------------------------------------- 1 | package leetcode.api 2 | 3 | import java.util.ArrayList 4 | 5 | class UndirectedGraphNode(var label: Int) { 6 | var neighbors = ArrayList() 7 | } 8 | -------------------------------------------------------------------------------- /kotlin/src/test/kotlin/api/ColorSpec.kt: -------------------------------------------------------------------------------- 1 | package api 2 | 3 | import org.spekframework.spek2.Spek 4 | import org.spekframework.spek2.style.specification.describe 5 | import kotlin.test.assertEquals 6 | 7 | object ColorSpec : Spek({ 8 | describe("Color Enum") { 9 | context("'valueOf' String") { 10 | it("should 'valueOf' should handle colors") { 11 | assertEquals(Color.valueOf("BLUE"), Color.BLUE) 12 | } 13 | } 14 | 15 | context("'values' size") { 16 | it("should only have 3 colors") { 17 | assertEquals(Color.entries.size, 3) 18 | } 19 | } 20 | } 21 | }) 22 | -------------------------------------------------------------------------------- /kotlin/src/test/kotlin/api/NodeSpec.kt: -------------------------------------------------------------------------------- 1 | package api 2 | 3 | import org.spekframework.spek2.Spek 4 | import org.spekframework.spek2.style.specification.describe 5 | import kotlin.test.assertEquals 6 | 7 | object NodeSpec : Spek({ 8 | describe("Node for answer coding questions") { 9 | val node = Node() 10 | context("basic usage with no value") { 11 | node.value = 123 12 | it("should be 123") { 13 | assertEquals(node.value, 123) 14 | } 15 | } 16 | 17 | context("basic usage with initial value") { 18 | it("should be 123") { 19 | assertEquals(Color.entries.size, 3) 20 | } 21 | } 22 | } 23 | }) 24 | -------------------------------------------------------------------------------- /kotlin/src/test/kotlin/exercism/HelloWorldTest.kt: -------------------------------------------------------------------------------- 1 | package exercism 2 | 3 | import org.junit.jupiter.api.Test 4 | import kotlin.test.assertEquals 5 | 6 | class HelloWorldTest { 7 | @Test 8 | fun helloWorldTest() { 9 | assertEquals("Hello, World!", hello()) 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /kotlin/src/test/kotlin/exercism/TwoferTest.kt: -------------------------------------------------------------------------------- 1 | package exercism 2 | 3 | import org.junit.jupiter.api.Test 4 | import kotlin.test.assertEquals 5 | 6 | class TwoferTest { 7 | @Test 8 | fun noNameGiven() { 9 | assertEquals("One for you, one for me.", twofer()) 10 | } 11 | 12 | @Test 13 | fun aNameGiven() { 14 | assertEquals("One for Alice, one for me.", twofer("Alice")) 15 | } 16 | 17 | @Test 18 | fun anotherNameGiven() { 19 | assertEquals("One for Bob, one for me.", twofer("Bob")) 20 | } 21 | 22 | @Test 23 | fun emptyStringGiven() { 24 | assertEquals("One for , one for me.", twofer("")) 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /python/build.gradle: -------------------------------------------------------------------------------- 1 | plugins { 2 | id 'com.linkedin.python-sdist' version '0.9.11' 3 | } 4 | 5 | repositories { 6 | pyGradlePyPi() 7 | } 8 | -------------------------------------------------------------------------------- /python/setup.cfg: -------------------------------------------------------------------------------- 1 | [flake8] 2 | ignore = E125,E126,E127,E129,E501 ,E713,W503 3 | # max-line-length = 160 4 | -------------------------------------------------------------------------------- /python/src/main/python/projecteuler/problem14.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | """ 3 | Longest Collatz sequence 4 | https://projecteuler.net/problem=14 5 | """ 6 | 7 | 8 | def sequence(a): 9 | n = 0 10 | while a > 0: 11 | if a == 1: 12 | break 13 | if a % 2 == 0: 14 | a = a / 2 15 | n = n + 1 16 | else: 17 | a = 3 * a + 1 18 | n = n + 1 19 | return n + 1 20 | 21 | 22 | def main(): 23 | start = 0 24 | num = 0 25 | for i in range(13, 1000000): 26 | temp = sequence(i) 27 | if temp > start: 28 | start = temp 29 | num = i 30 | print(num) 31 | 32 | 33 | if __name__ == "__main__": 34 | main() 35 | -------------------------------------------------------------------------------- /python/src/main/python/projecteuler/problem15.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | """ 3 | Lattice paths 4 | https://projecteuler.net/problem=15 5 | """ 6 | 7 | import math 8 | 9 | 10 | def main(): 11 | print(int((math.factorial(20 * 2) / math.factorial(20)) / math.factorial(20))) 12 | 13 | 14 | if __name__ == "__main__": 15 | main() 16 | -------------------------------------------------------------------------------- /python/src/main/python/projecteuler/problem21.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | """ 3 | Amicable numbers 4 | https://projecteuler.net/problem=21 5 | """ 6 | 7 | 8 | def d(n): 9 | q = [] 10 | for i in range(1, n / 2 + 1, 1): 11 | if n % i == 0: 12 | q.append(i) 13 | return sum(q) 14 | 15 | 16 | def main(): 17 | w = [] 18 | for i in range(1, 10000, 1): 19 | if i == d(d(i)) and i != d(i): 20 | print(i) 21 | w.append(i) 22 | print(sum(w)) 23 | 24 | 25 | if __name__ == "__main__": 26 | main() 27 | -------------------------------------------------------------------------------- /python/src/main/python/projecteuler/problem25.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | """ 3 | 1000-digit Fibonacci number 4 | https://projecteuler.net/problem=25 5 | """ 6 | 7 | 8 | def fib(n): 9 | a, b = 0, 1 10 | for i in range(n): 11 | a, b = b, a + b 12 | return a 13 | 14 | 15 | def main(): 16 | i = 0 17 | while len(str(fib(i))) < 1000: 18 | i = i + 1 19 | print(i) 20 | 21 | 22 | if __name__ == "__main__": 23 | main() 24 | -------------------------------------------------------------------------------- /python/src/main/python/projecteuler/problem28.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | """ 3 | Number spiral diagonals 4 | https://projecteuler.net/problem=28 5 | """ 6 | 7 | 8 | def main(): 9 | total = 0 10 | for i in range(1, 1001 + 1): 11 | if i % 2 != 0 and i != 1: 12 | total = total + (i ** 2) + (i ** 2 - i + 1) + (i ** 2 - i * 2 + 2) + (i ** 2 - i * 3 + 3) 13 | print(total + 1) 14 | 15 | 16 | if __name__ == "__main__": 17 | main() 18 | -------------------------------------------------------------------------------- /python/src/main/python/projecteuler/problem3.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | """ 3 | Largest prime factor 4 | https://projecteuler.net/problem=3 5 | """ 6 | 7 | 8 | def main(): 9 | n = 13195 10 | factors = [x for x in range(1, n / 2, 1) if n % x == 0] 11 | print(factors) 12 | # primeFactors = [x for x in factors if for y in range(3, n / 2, 2) if n % i == 0] 13 | # print(primeFactors) 14 | 15 | 16 | if __name__ == "__main__": 17 | main() 18 | -------------------------------------------------------------------------------- /python/src/main/python/projecteuler/problem30.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | """ 3 | Digit fifth powers 4 | https://projecteuler.net/problem=30 5 | """ 6 | 7 | 8 | def numberexponent(n, e): 9 | n = list(n) 10 | total = 0 11 | for i in range(len(n)): 12 | total = total + int(n[i]) ** e 13 | return total 14 | 15 | 16 | def main(): 17 | total = 0 18 | for i in range(10, 1000000): 19 | if numberexponent(str(i), 5) == i: 20 | total = total + i 21 | print(total) 22 | 23 | 24 | if __name__ == "__main__": 25 | main() 26 | -------------------------------------------------------------------------------- /python/src/main/python/projecteuler/problem36.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | """ 3 | Truncatable primes 4 | https://projecteuler.net/problem=37 5 | """ 6 | 7 | 8 | def ispalindrome(n): 9 | return n == n[::-1] 10 | 11 | 12 | def int2bin(n): 13 | return "{0:b}".format(n) 14 | 15 | 16 | def main(): 17 | total = 0 18 | for i in range(1000000): 19 | if ispalindrome(str(i)) and ispalindrome(str(int2bin(i))): 20 | total = total + i 21 | print(total) 22 | 23 | 24 | if __name__ == "__main__": 25 | main() 26 | -------------------------------------------------------------------------------- /python/src/main/python/projecteuler/problem48.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | """ 3 | Self powers 4 | https://projecteuler.net/problem=48 5 | """ 6 | 7 | 8 | def main(): 9 | total = 0 10 | for i in range(1, 1000): 11 | total = total + i ** i 12 | print(str(total)[-10:len(str(total))]) 13 | 14 | 15 | if __name__ == "__main__": 16 | main() 17 | -------------------------------------------------------------------------------- /python/src/main/python/projecteuler/problem53.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | """ 3 | Combinatoric selections 4 | https://projecteuler.net/problem=53 5 | """ 6 | 7 | import math 8 | 9 | 10 | def chose(x, y): 11 | return float(math.factorial(x)) / (math.factorial(y) * math.factorial(x - y)) 12 | 13 | 14 | def main(): 15 | total = 0 16 | for i in range(1, 100 + 1): 17 | for j in range(1, 100 + 1): 18 | if i > j: 19 | if chose(i, j) > 1000000: 20 | total = total + 1 21 | print(total) 22 | 23 | 24 | if __name__ == "__main__": 25 | main() 26 | -------------------------------------------------------------------------------- /python/src/main/python/projecteuler/problem56.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | """ 3 | Powerful digit sum 4 | https://projecteuler.net/problem=56 5 | """ 6 | 7 | 8 | def digitalsum(n): 9 | n = list(str(n)) 10 | total = 0 11 | for i in range(len(n)): 12 | total = total + int(n[i]) 13 | return total 14 | 15 | 16 | def main(): 17 | hold = 0 18 | for i in range(1, 100): 19 | for j in range(1, 100): 20 | temp = digitalsum(i ** j) 21 | if temp > hold: 22 | hold = temp 23 | print(hold) 24 | 25 | 26 | if __name__ == "__main__": 27 | main() 28 | -------------------------------------------------------------------------------- /python/src/main/python/projecteuler/problem63.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | """ 3 | Powerful digit counts 4 | https://projecteuler.net/problem=63 5 | """ 6 | 7 | 8 | def main(): 9 | count = 0 10 | for i in range(1, 30): 11 | for j in range(1, 30): 12 | test = i ** j 13 | if len(str(int(test))) == j: 14 | count = count + 1 15 | print(count) 16 | 17 | 18 | if __name__ == "__main__": 19 | main() 20 | -------------------------------------------------------------------------------- /python/src/main/python/projecteuler/problem73.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | """ 3 | Counting fractions in a range 4 | https://projecteuler.net/problem=73 5 | """ 6 | 7 | 8 | def main(): 9 | q = [] 10 | for i in range(1, 12000): 11 | for j in range(1, 12000 + 1): 12 | if j > i: 13 | temp = float(i) / j 14 | if float(1) / 3 < temp < float(1) / 2: 15 | q.append(temp) 16 | 17 | print(len(list(set(q)))) 18 | 19 | 20 | if __name__ == "__main__": 21 | main() 22 | -------------------------------------------------------------------------------- /renovate.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "https://docs.renovatebot.com/renovate-schema.json", 3 | "extends": [ 4 | "config:recommended" 5 | ], 6 | "semanticCommits": "disabled" 7 | } 8 | --------------------------------------------------------------------------------