├── .github └── FUNDING.yml ├── .gitignore ├── CODE_OF_CONDUCT.md ├── CONTRIBUTING.md ├── LEGAL.md ├── LICENSE ├── README.md ├── assets └── logo.png ├── learn.md └── src ├── Day 01 └── roman.java ├── Day 02 └── palindrome.java ├── Day 03 └── LastWord.java ├── Day 04 └── bestStock.java ├── Day 05 └── integerToRoman.java ├── Day 06 └── needleHaystack.java ├── Day 07 └── ExcelSheet.java ├── Day 08 └── ReverseInteger.java ├── Day 09 └── singleNumber.java ├── Day 10 └── uglyNumber.java ├── Day 11 └── wordPattern.java ├── Day 12 └── VaildParentheses.java ├── Day 13 └── TwoSum.java ├── Day 14 └── CommonPrefix.java ├── Day 15 └── containsDuplicate.java ├── Day 16 └── WordPalidrome.java ├── Day 17 └── removeDuplicates.java ├── Day 18 └── addBinary.java ├── Day 19 └── Candies.java ├── Day 20 └── CanPlaceFlowers.java ├── Day 21 └── ReverseWords.java ├── Day 22 └── CompressCharacters.java ├── Day 23 └── HighestAltitude.java ├── Day 24 └── MergeStrings.java ├── Day 25 └── ReverseVowels.java ├── Day 26 └── DefangIPAddress.java ├── Day 27 └── atoi.java ├── Day 28 └── ReverseString.java ├── Day 29 └── ransome.java ├── Day 30 └── GCDOfStrings.java ├── Day 31 └── moveZeros.java ├── Day 32 └── removeElement.java ├── Day 33 └── HappyNumber.java ├── Day 34 └── ExcelSheetII.java ├── Day 35 └── ContainsDuplicate.java ├── Day 36 └── IsomorphicStrings.java ├── Day 37 └── concentationArray.java ├── Day 38 └── plusOne.java ├── Day 39 └── mergeArray.java ├── Day 40 └── missingNumber.java ├── Day 41 └── maximumProduct.java ├── Day 42 └── findGCD.java ├── Day 43 └── multiplyStrings.java ├── Day 44 └── countAndSay.java ├── Day 45 └── majorityElement.java ├── Day 46 ├── rotateMatrix.java └── rotateMatrix.py ├── Day 47 └── FirstMissingPositive.java ├── Day 48 └── letterCombination.java ├── Day 49 └── insertPosition.java ├── Day 50 └── jumpGame.java ├── Day 51 └── colors.java ├── Day 52 └── search2D.java ├── Day 53 └── bestStockII.java ├── Day 54 └── rabbits.java ├── Day 55 └── CardsInDeck.java ├── Day 56 └── GoodPairs.java ├── Day 57 └── XORTriplets.java ├── Day 58 └── Prime.java ├── Day 59 └── NicePairs.java ├── Day 60 └── BeautyNumber.java ├── Day 61 └── DaysSpentTogether.java ├── Day 62 └── Fraction.java ├── Day 63 └── NumberOfDays.java ├── Extras └── foldermaker.bat └── PROBLEMS.md /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | # These are supported funding model platforms 2 | 3 | github: codeIntrovert #Replace with up to 4 GitHub Sponsors-enabled usernames e.g., [user1, user2] 4 | patreon: # Replace with a single Patreon username 5 | open_collective: # Replace with a single Open Collective username 6 | ko_fi: # Replace with a single Ko-fi username 7 | tidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel 8 | community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry 9 | liberapay: # Replace with a single Liberapay username 10 | issuehunt: # Replace with a single IssueHunt username 11 | otechie: # Replace with a single Otechie username 12 | lfx_crowdfunding: # Replace with a single LFX Crowdfunding project-name e.g., cloud-foundry 13 | custom: # Replace with up to 4 custom sponsorship URLs e.g., ['link1', 'link2'] 14 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.class 2 | *.bat 3 | *.yml 4 | /.vscode -------------------------------------------------------------------------------- /CODE_OF_CONDUCT.md: -------------------------------------------------------------------------------- 1 | # Contributor Covenant Code of Conduct 2 | 3 | ## Our Pledge 4 | 5 | We as members, contributors, and leaders pledge to make participation in our 6 | community a harassment-free experience for everyone, regardless of age, body 7 | size, visible or invisible disability, ethnicity, sex characteristics, gender 8 | identity and expression, level of experience, education, socio-economic status, 9 | nationality, personal appearance, race, religion, or sexual identity 10 | and orientation. 11 | 12 | We pledge to act and interact in ways that contribute to an open, welcoming, 13 | diverse, inclusive, and healthy community. 14 | 15 | ## Our Standards 16 | 17 | Examples of behavior that contributes to a positive environment for our 18 | community include: 19 | 20 | * Demonstrating empathy and kindness toward other people 21 | * Being respectful of differing opinions, viewpoints, and experiences 22 | * Giving and gracefully accepting constructive feedback 23 | * Accepting responsibility and apologizing to those affected by our mistakes, 24 | and learning from the experience 25 | * Focusing on what is best not just for us as individuals, but for the 26 | overall community 27 | 28 | Examples of unacceptable behavior include: 29 | 30 | * The use of sexualized language or imagery, and sexual attention or 31 | advances of any kind 32 | * Trolling, insulting or derogatory comments, and personal or political attacks 33 | * Public or private harassment 34 | * Publishing others' private information, such as a physical or email 35 | address, without their explicit permission 36 | * Other conduct which could reasonably be considered inappropriate in a 37 | professional setting 38 | 39 | ## Enforcement Responsibilities 40 | 41 | Community leaders are responsible for clarifying and enforcing our standards of 42 | acceptable behavior and will take appropriate and fair corrective action in 43 | response to any behavior that they deem inappropriate, threatening, offensive, 44 | or harmful. 45 | 46 | Community leaders have the right and responsibility to remove, edit, or reject 47 | comments, commits, code, wiki edits, issues, and other contributions that are 48 | not aligned to this Code of Conduct, and will communicate reasons for moderation 49 | decisions when appropriate. 50 | 51 | ## Scope 52 | 53 | This Code of Conduct applies within all community spaces, and also applies when 54 | an individual is officially representing the community in public spaces. 55 | Examples of representing our community include using an official e-mail address, 56 | posting via an official social media account, or acting as an appointed 57 | representative at an online or offline event. 58 | 59 | ## Enforcement 60 | 61 | Instances of abusive, harassing, or otherwise unacceptable behavior may be 62 | reported to the community leaders responsible for enforcement at 63 | codecolts@gmail.com. 64 | All complaints will be reviewed and investigated promptly and fairly. 65 | 66 | All community leaders are obligated to respect the privacy and security of the 67 | reporter of any incident. 68 | 69 | ## Enforcement Guidelines 70 | 71 | Community leaders will follow these Community Impact Guidelines in determining 72 | the consequences for any action they deem in violation of this Code of Conduct: 73 | 74 | ### 1. Correction 75 | 76 | **Community Impact**: Use of inappropriate language or other behavior deemed 77 | unprofessional or unwelcome in the community. 78 | 79 | **Consequence**: A private, written warning from community leaders, providing 80 | clarity around the nature of the violation and an explanation of why the 81 | behavior was inappropriate. A public apology may be requested. 82 | 83 | ### 2. Warning 84 | 85 | **Community Impact**: A violation through a single incident or series 86 | of actions. 87 | 88 | **Consequence**: A warning with consequences for continued behavior. No 89 | interaction with the people involved, including unsolicited interaction with 90 | those enforcing the Code of Conduct, for a specified period of time. This 91 | includes avoiding interactions in community spaces as well as external channels 92 | like social media. Violating these terms may lead to a temporary or 93 | permanent ban. 94 | 95 | ### 3. Temporary Ban 96 | 97 | **Community Impact**: A serious violation of community standards, including 98 | sustained inappropriate behavior. 99 | 100 | **Consequence**: A temporary ban from any sort of interaction or public 101 | communication with the community for a specified period of time. No public or 102 | private interaction with the people involved, including unsolicited interaction 103 | with those enforcing the Code of Conduct, is allowed during this period. 104 | Violating these terms may lead to a permanent ban. 105 | 106 | ### 4. Permanent Ban 107 | 108 | **Community Impact**: Demonstrating a pattern of violation of community 109 | standards, including sustained inappropriate behavior, harassment of an 110 | individual, or aggression toward or disparagement of classes of individuals. 111 | 112 | **Consequence**: A permanent ban from any sort of public interaction within 113 | the community. 114 | 115 | ## Attribution 116 | 117 | This Code of Conduct is adapted from the [Contributor Covenant][homepage], 118 | version 2.0, available at 119 | https://www.contributor-covenant.org/version/2/0/code_of_conduct.html. 120 | 121 | Community Impact Guidelines were inspired by [Mozilla's code of conduct 122 | enforcement ladder](https://github.com/mozilla/diversity). 123 | 124 | [homepage]: https://www.contributor-covenant.org 125 | 126 | For answers to common questions about this code of conduct, see the FAQ at 127 | https://www.contributor-covenant.org/faq. Translations are available at 128 | https://www.contributor-covenant.org/translations. 129 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contribution Guidelines 🚀 2 | 3 | Thank you for considering contributing to our JAVA project! We appreciate your interest and support. By contributing, you not only help improve the project but also become a valuable part of our growing community. 4 | 5 | ## Ways to Contribute 🤝 6 | 7 | There are several ways you can contribute to our project: 8 | 9 | ### 1. Bug Reports 🐛 10 | 11 | If you encounter any issues or bugs, please open a GitHub issue and provide detailed information about the problem, including steps to reproduce it. Your feedback is crucial for making our project more robust. 12 | 13 | ### 2. Feature Requests 💡 14 | 15 | Have an idea for a new feature or improvement? Let us know by creating a GitHub issue. We value your input, and your suggestions can help shape the future of the project. 16 | 17 | ### 3. Code Contributions 🖥️ 18 | 19 | Feel free to submit pull requests for bug fixes, new features, or enhancements. Please follow the guidelines below when contributing code: 20 | 21 | - Fork the repository and create a branch for your changes. 22 | - Ensure your code adheres to the project's coding standards. 23 | - Include relevant tests for your changes. 24 | - Update documentation if necessary. 25 | 26 | ### 4. Documentation 📖 27 | 28 | Help improve the project's documentation by fixing typos, clarifying instructions, or adding examples. Clear and concise documentation is essential for a user-friendly experience. 29 | 30 | ## Getting Started 🚀 31 | 32 | If you're new to our project, check out the [README](https://github.com/codeIntrovert/90DaysOfDSA/blob/main/README.md) file for information on setting up the development environment and running the application. 33 | 34 | ## Code of Conduct 🤝 35 | 36 | Please note that we have a [Code of Conduct](https://github.com/codeIntrovert/90DaysOfDSA/blob/main/CODE_OF_CONDUCT.md) in place. Be sure to read and follow it to ensure a positive and inclusive community. 37 | 38 | ## Questions and Feedback ❓ 39 | 40 | If you have questions or need clarification, feel free to reach out via GitHub issues. We welcome your feedback and are here to help. 41 | 42 | ## Thank You! 🙌 43 | 44 | Your contributions are invaluable to the success of this project. We appreciate your time and effort in making our JAVA project even better! 🚀 45 | -------------------------------------------------------------------------------- /LEGAL.md: -------------------------------------------------------------------------------- 1 | # Legal Disclaimer 2 | 3 | *Last Updated: 5/February/2024* 4 | 5 | *Contents of this File are Applicable to All Projects Of IntrovertCoder, Codecolts & CodeIntrovert Lying under same Creator* 6 | 7 | Welcome to CodeColts ("the Website"). This Website is an open-source project designed to serve as a library of free coding video courses available across the web. The goal of CodeColts is to provide a curated collection of educational resources for individuals seeking coding tutorials and related content. Before using this Website, please take a moment to read and understand the following legal disclaimer. 8 | 9 | ## Ownership and Use of Videos 10 | 11 | CodeColts does not claim ownership, control, or rights to any of the video content featured on the Website. All videos are the sole property of their respective owners, creators, and platforms. We are not responsible for the content, accuracy, or legality of the videos, nor do we endorse or promote any specific video creators, courses, or platforms. 12 | 13 | ## Educational and Informational Purpose 14 | 15 | The videos featured on CodeColts are provided for educational and informational purposes only. We make no representations or warranties regarding the accuracy, suitability, or availability of the content. Users of this Website are responsible for independently verifying the information presented in the videos and using their own discretion when accessing and relying on any content. 16 | 17 | ## Non-Commercial Project 18 | 19 | CodeColts is a non-commercial, open-source project. We do not generate revenue from the display or sharing of videos. Our aim is to create a free and accessible resource for coding enthusiasts and learners. 20 | 21 | ## Proper Attribution 22 | 23 | We strive to provide proper attribution to the creators of the videos we feature on this Website. The video titles, descriptions, and source links are intended to credit the original authors and platforms. If you are a video creator or platform owner and believe that your content has been improperly attributed or featured on this Website without your consent, please contact us using the information provided below, and we will promptly address your concerns. 24 | 25 | ## Copyright Compliance 26 | 27 | CodeColts is committed to respecting the intellectual property rights of others. We encourage all content creators and users to adhere to copyright laws and regulations. If you believe that your copyrighted material has been used on this Website without authorization, please contact us with the following information: 28 | 29 | - A description of the copyrighted work in question. 30 | - Your contact information, including name, address, email address, and phone number. 31 | - A statement of your good faith belief that the use of the material is not authorized by the copyright owner, its agent, or the law. 32 | - A statement, under penalty of perjury, that the information provided in your notice is accurate and that you are the copyright owner or authorized to act on the copyright owner's behalf. 33 | 34 | ## Disclaimer of Liability 35 | 36 | CodeColts, its administrators, contributors, and affiliates shall not be held liable for any claims, damages, or losses arising from the use of this Website or the content it features. By using this Website, you agree to do so at your own risk. 37 | 38 | ## Changes to Disclaimer 39 | 40 | This legal disclaimer is subject to change without notice. It is your responsibility to review this disclaimer periodically for any updates or modifications. 41 | 42 | ## Contact Us 43 | 44 | If you have any questions, concerns, or requests related to this legal disclaimer or the content featured on CodeColts, please contact us at [codeColts@gmail.com](mailto:codeColts@gmail.com). 45 | 46 | By using this Website, you acknowledge that you have read and understood this legal disclaimer and agree to comply with its terms and conditions. Thank you for visiting CodeColts. 47 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Attribution-NonCommercial-NoDerivatives 4.0 International 2 | 3 | ======================================================================= 4 | 5 | Creative Commons Corporation ("Creative Commons") is not a law firm and 6 | does not provide legal services or legal advice. Distribution of 7 | Creative Commons public licenses does not create a lawyer-client or 8 | other relationship. Creative Commons makes its licenses and related 9 | information available on an "as-is" basis. Creative Commons gives no 10 | warranties regarding its licenses, any material licensed under their 11 | terms and conditions, or any related information. Creative Commons 12 | disclaims all liability for damages resulting from their use to the 13 | fullest extent possible. 14 | 15 | Using Creative Commons Public Licenses 16 | 17 | Creative Commons public licenses provide a standard set of terms and 18 | conditions that creators and other rights holders may use to share 19 | original works of authorship and other material subject to copyright 20 | and certain other rights specified in the public license below. The 21 | following considerations are for informational purposes only, are not 22 | exhaustive, and do not form part of our licenses. 23 | 24 | Considerations for licensors: Our public licenses are 25 | intended for use by those authorized to give the public 26 | permission to use material in ways otherwise restricted by 27 | copyright and certain other rights. Our licenses are 28 | irrevocable. Licensors should read and understand the terms 29 | and conditions of the license they choose before applying it. 30 | Licensors should also secure all rights necessary before 31 | applying our licenses so that the public can reuse the 32 | material as expected. Licensors should clearly mark any 33 | material not subject to the license. This includes other CC- 34 | licensed material, or material used under an exception or 35 | limitation to copyright. More considerations for licensors: 36 | wiki.creativecommons.org/Considerations_for_licensors 37 | 38 | Considerations for the public: By using one of our public 39 | licenses, a licensor grants the public permission to use the 40 | licensed material under specified terms and conditions. If 41 | the licensor's permission is not necessary for any reason--for 42 | example, because of any applicable exception or limitation to 43 | copyright--then that use is not regulated by the license. Our 44 | licenses grant only permissions under copyright and certain 45 | other rights that a licensor has authority to grant. Use of 46 | the licensed material may still be restricted for other 47 | reasons, including because others have copyright or other 48 | rights in the material. A licensor may make special requests, 49 | such as asking that all changes be marked or described. 50 | Although not required by our licenses, you are encouraged to 51 | respect those requests where reasonable. More_considerations 52 | for the public: 53 | wiki.creativecommons.org/Considerations_for_licensees 54 | 55 | ======================================================================= 56 | 57 | Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 58 | International Public License 59 | 60 | By exercising the Licensed Rights (defined below), You accept and agree 61 | to be bound by the terms and conditions of this Creative Commons 62 | Attribution-NonCommercial-NoDerivatives 4.0 International Public 63 | License ("Public License"). To the extent this Public License may be 64 | interpreted as a contract, You are granted the Licensed Rights in 65 | consideration of Your acceptance of these terms and conditions, and the 66 | Licensor grants You such rights in consideration of benefits the 67 | Licensor receives from making the Licensed Material available under 68 | these terms and conditions. 69 | 70 | 71 | Section 1 -- Definitions. 72 | 73 | a. Adapted Material means material subject to Copyright and Similar 74 | Rights that is derived from or based upon the Licensed Material 75 | and in which the Licensed Material is translated, altered, 76 | arranged, transformed, or otherwise modified in a manner requiring 77 | permission under the Copyright and Similar Rights held by the 78 | Licensor. For purposes of this Public License, where the Licensed 79 | Material is a musical work, performance, or sound recording, 80 | Adapted Material is always produced where the Licensed Material is 81 | synched in timed relation with a moving image. 82 | 83 | b. Copyright and Similar Rights means copyright and/or similar rights 84 | closely related to copyright including, without limitation, 85 | performance, broadcast, sound recording, and Sui Generis Database 86 | Rights, without regard to how the rights are labeled or 87 | categorized. For purposes of this Public License, the rights 88 | specified in Section 2(b)(1)-(2) are not Copyright and Similar 89 | Rights. 90 | 91 | c. Effective Technological Measures means those measures that, in the 92 | absence of proper authority, may not be circumvented under laws 93 | fulfilling obligations under Article 11 of the WIPO Copyright 94 | Treaty adopted on December 20, 1996, and/or similar international 95 | agreements. 96 | 97 | d. Exceptions and Limitations means fair use, fair dealing, and/or 98 | any other exception or limitation to Copyright and Similar Rights 99 | that applies to Your use of the Licensed Material. 100 | 101 | e. Licensed Material means the artistic or literary work, database, 102 | or other material to which the Licensor applied this Public 103 | License. 104 | 105 | f. Licensed Rights means the rights granted to You subject to the 106 | terms and conditions of this Public License, which are limited to 107 | all Copyright and Similar Rights that apply to Your use of the 108 | Licensed Material and that the Licensor has authority to license. 109 | 110 | g. Licensor means the individual(s) or entity(ies) granting rights 111 | under this Public License. 112 | 113 | h. NonCommercial means not primarily intended for or directed towards 114 | commercial advantage or monetary compensation. For purposes of 115 | this Public License, the exchange of the Licensed Material for 116 | other material subject to Copyright and Similar Rights by digital 117 | file-sharing or similar means is NonCommercial provided there is 118 | no payment of monetary compensation in connection with the 119 | exchange. 120 | 121 | i. Share means to provide material to the public by any means or 122 | process that requires permission under the Licensed Rights, such 123 | as reproduction, public display, public performance, distribution, 124 | dissemination, communication, or importation, and to make material 125 | available to the public including in ways that members of the 126 | public may access the material from a place and at a time 127 | individually chosen by them. 128 | 129 | j. Sui Generis Database Rights means rights other than copyright 130 | resulting from Directive 96/9/EC of the European Parliament and of 131 | the Council of 11 March 1996 on the legal protection of databases, 132 | as amended and/or succeeded, as well as other essentially 133 | equivalent rights anywhere in the world. 134 | 135 | k. You means the individual or entity exercising the Licensed Rights 136 | under this Public License. Your has a corresponding meaning. 137 | 138 | 139 | Section 2 -- Scope. 140 | 141 | a. License grant. 142 | 143 | 1. Subject to the terms and conditions of this Public License, 144 | the Licensor hereby grants You a worldwide, royalty-free, 145 | non-sublicensable, non-exclusive, irrevocable license to 146 | exercise the Licensed Rights in the Licensed Material to: 147 | 148 | a. reproduce and Share the Licensed Material, in whole or 149 | in part, for NonCommercial purposes only; and 150 | 151 | b. produce and reproduce, but not Share, Adapted Material 152 | for NonCommercial purposes only. 153 | 154 | 2. Exceptions and Limitations. For the avoidance of doubt, where 155 | Exceptions and Limitations apply to Your use, this Public 156 | License does not apply, and You do not need to comply with 157 | its terms and conditions. 158 | 159 | 3. Term. The term of this Public License is specified in Section 160 | 6(a). 161 | 162 | 4. Media and formats; technical modifications allowed. The 163 | Licensor authorizes You to exercise the Licensed Rights in 164 | all media and formats whether now known or hereafter created, 165 | and to make technical modifications necessary to do so. The 166 | Licensor waives and/or agrees not to assert any right or 167 | authority to forbid You from making technical modifications 168 | necessary to exercise the Licensed Rights, including 169 | technical modifications necessary to circumvent Effective 170 | Technological Measures. For purposes of this Public License, 171 | simply making modifications authorized by this Section 2(a) 172 | (4) never produces Adapted Material. 173 | 174 | 5. Downstream recipients. 175 | 176 | a. Offer from the Licensor -- Licensed Material. Every 177 | recipient of the Licensed Material automatically 178 | receives an offer from the Licensor to exercise the 179 | Licensed Rights under the terms and conditions of this 180 | Public License. 181 | 182 | b. No downstream restrictions. You may not offer or impose 183 | any additional or different terms or conditions on, or 184 | apply any Effective Technological Measures to, the 185 | Licensed Material if doing so restricts exercise of the 186 | Licensed Rights by any recipient of the Licensed 187 | Material. 188 | 189 | 6. No endorsement. Nothing in this Public License constitutes or 190 | may be construed as permission to assert or imply that You 191 | are, or that Your use of the Licensed Material is, connected 192 | with, or sponsored, endorsed, or granted official status by, 193 | the Licensor or others designated to receive attribution as 194 | provided in Section 3(a)(1)(A)(i). 195 | 196 | b. Other rights. 197 | 198 | 1. Moral rights, such as the right of integrity, are not 199 | licensed under this Public License, nor are publicity, 200 | privacy, and/or other similar personality rights; however, to 201 | the extent possible, the Licensor waives and/or agrees not to 202 | assert any such rights held by the Licensor to the limited 203 | extent necessary to allow You to exercise the Licensed 204 | Rights, but not otherwise. 205 | 206 | 2. Patent and trademark rights are not licensed under this 207 | Public License. 208 | 209 | 3. To the extent possible, the Licensor waives any right to 210 | collect royalties from You for the exercise of the Licensed 211 | Rights, whether directly or through a collecting society 212 | under any voluntary or waivable statutory or compulsory 213 | licensing scheme. In all other cases the Licensor expressly 214 | reserves any right to collect such royalties, including when 215 | the Licensed Material is used other than for NonCommercial 216 | purposes. 217 | 218 | 219 | Section 3 -- License Conditions. 220 | 221 | Your exercise of the Licensed Rights is expressly made subject to the 222 | following conditions. 223 | 224 | a. Attribution. 225 | 226 | 1. If You Share the Licensed Material, You must: 227 | 228 | a. retain the following if it is supplied by the Licensor 229 | with the Licensed Material: 230 | 231 | i. identification of the creator(s) of the Licensed 232 | Material and any others designated to receive 233 | attribution, in any reasonable manner requested by 234 | the Licensor (including by pseudonym if 235 | designated); 236 | 237 | ii. a copyright notice; 238 | 239 | iii. a notice that refers to this Public License; 240 | 241 | iv. a notice that refers to the disclaimer of 242 | warranties; 243 | 244 | v. a URI or hyperlink to the Licensed Material to the 245 | extent reasonably practicable; 246 | 247 | b. indicate if You modified the Licensed Material and 248 | retain an indication of any previous modifications; and 249 | 250 | c. indicate the Licensed Material is licensed under this 251 | Public License, and include the text of, or the URI or 252 | hyperlink to, this Public License. 253 | 254 | For the avoidance of doubt, You do not have permission under 255 | this Public License to Share Adapted Material. 256 | 257 | 2. You may satisfy the conditions in Section 3(a)(1) in any 258 | reasonable manner based on the medium, means, and context in 259 | which You Share the Licensed Material. For example, it may be 260 | reasonable to satisfy the conditions by providing a URI or 261 | hyperlink to a resource that includes the required 262 | information. 263 | 264 | 3. If requested by the Licensor, You must remove any of the 265 | information required by Section 3(a)(1)(A) to the extent 266 | reasonably practicable. 267 | 268 | 269 | Section 4 -- Sui Generis Database Rights. 270 | 271 | Where the Licensed Rights include Sui Generis Database Rights that 272 | apply to Your use of the Licensed Material: 273 | 274 | a. for the avoidance of doubt, Section 2(a)(1) grants You the right 275 | to extract, reuse, reproduce, and Share all or a substantial 276 | portion of the contents of the database for NonCommercial purposes 277 | only and provided You do not Share Adapted Material; 278 | 279 | b. if You include all or a substantial portion of the database 280 | contents in a database in which You have Sui Generis Database 281 | Rights, then the database in which You have Sui Generis Database 282 | Rights (but not its individual contents) is Adapted Material; and 283 | 284 | c. You must comply with the conditions in Section 3(a) if You Share 285 | all or a substantial portion of the contents of the database. 286 | 287 | For the avoidance of doubt, this Section 4 supplements and does not 288 | replace Your obligations under this Public License where the Licensed 289 | Rights include other Copyright and Similar Rights. 290 | 291 | 292 | Section 5 -- Disclaimer of Warranties and Limitation of Liability. 293 | 294 | a. UNLESS OTHERWISE SEPARATELY UNDERTAKEN BY THE LICENSOR, TO THE 295 | EXTENT POSSIBLE, THE LICENSOR OFFERS THE LICENSED MATERIAL AS-IS 296 | AND AS-AVAILABLE, AND MAKES NO REPRESENTATIONS OR WARRANTIES OF 297 | ANY KIND CONCERNING THE LICENSED MATERIAL, WHETHER EXPRESS, 298 | IMPLIED, STATUTORY, OR OTHER. THIS INCLUDES, WITHOUT LIMITATION, 299 | WARRANTIES OF TITLE, MERCHANTABILITY, FITNESS FOR A PARTICULAR 300 | PURPOSE, NON-INFRINGEMENT, ABSENCE OF LATENT OR OTHER DEFECTS, 301 | ACCURACY, OR THE PRESENCE OR ABSENCE OF ERRORS, WHETHER OR NOT 302 | KNOWN OR DISCOVERABLE. WHERE DISCLAIMERS OF WARRANTIES ARE NOT 303 | ALLOWED IN FULL OR IN PART, THIS DISCLAIMER MAY NOT APPLY TO YOU. 304 | 305 | b. TO THE EXTENT POSSIBLE, IN NO EVENT WILL THE LICENSOR BE LIABLE 306 | TO YOU ON ANY LEGAL THEORY (INCLUDING, WITHOUT LIMITATION, 307 | NEGLIGENCE) OR OTHERWISE FOR ANY DIRECT, SPECIAL, INDIRECT, 308 | INCIDENTAL, CONSEQUENTIAL, PUNITIVE, EXEMPLARY, OR OTHER LOSSES, 309 | COSTS, EXPENSES, OR DAMAGES ARISING OUT OF THIS PUBLIC LICENSE OR 310 | USE OF THE LICENSED MATERIAL, EVEN IF THE LICENSOR HAS BEEN 311 | ADVISED OF THE POSSIBILITY OF SUCH LOSSES, COSTS, EXPENSES, OR 312 | DAMAGES. WHERE A LIMITATION OF LIABILITY IS NOT ALLOWED IN FULL OR 313 | IN PART, THIS LIMITATION MAY NOT APPLY TO YOU. 314 | 315 | c. The disclaimer of warranties and limitation of liability provided 316 | above shall be interpreted in a manner that, to the extent 317 | possible, most closely approximates an absolute disclaimer and 318 | waiver of all liability. 319 | 320 | 321 | Section 6 -- Term and Termination. 322 | 323 | a. This Public License applies for the term of the Copyright and 324 | Similar Rights licensed here. However, if You fail to comply with 325 | this Public License, then Your rights under this Public License 326 | terminate automatically. 327 | 328 | b. Where Your right to use the Licensed Material has terminated under 329 | Section 6(a), it reinstates: 330 | 331 | 1. automatically as of the date the violation is cured, provided 332 | it is cured within 30 days of Your discovery of the 333 | violation; or 334 | 335 | 2. upon express reinstatement by the Licensor. 336 | 337 | For the avoidance of doubt, this Section 6(b) does not affect any 338 | right the Licensor may have to seek remedies for Your violations 339 | of this Public License. 340 | 341 | c. For the avoidance of doubt, the Licensor may also offer the 342 | Licensed Material under separate terms or conditions or stop 343 | distributing the Licensed Material at any time; however, doing so 344 | will not terminate this Public License. 345 | 346 | d. Sections 1, 5, 6, 7, and 8 survive termination of this Public 347 | License. 348 | 349 | 350 | Section 7 -- Other Terms and Conditions. 351 | 352 | a. The Licensor shall not be bound by any additional or different 353 | terms or conditions communicated by You unless expressly agreed. 354 | 355 | b. Any arrangements, understandings, or agreements regarding the 356 | Licensed Material not stated herein are separate from and 357 | independent of the terms and conditions of this Public License. 358 | 359 | 360 | Section 8 -- Interpretation. 361 | 362 | a. For the avoidance of doubt, this Public License does not, and 363 | shall not be interpreted to, reduce, limit, restrict, or impose 364 | conditions on any use of the Licensed Material that could lawfully 365 | be made without permission under this Public License. 366 | 367 | b. To the extent possible, if any provision of this Public License is 368 | deemed unenforceable, it shall be automatically reformed to the 369 | minimum extent necessary to make it enforceable. If the provision 370 | cannot be reformed, it shall be severed from this Public License 371 | without affecting the enforceability of the remaining terms and 372 | conditions. 373 | 374 | c. No term or condition of this Public License will be waived and no 375 | failure to comply consented to unless expressly agreed to by the 376 | Licensor. 377 | 378 | d. Nothing in this Public License constitutes or may be interpreted 379 | as a limitation upon, or waiver of, any privileges and immunities 380 | that apply to the Licensor or You, including from the legal 381 | processes of any jurisdiction or authority. 382 | 383 | ======================================================================= 384 | 385 | Creative Commons is not a party to its public 386 | licenses. Notwithstanding, Creative Commons may elect to apply one of 387 | its public licenses to material it publishes and in those instances 388 | will be considered the “Licensor.” The text of the Creative Commons 389 | public licenses is dedicated to the public domain under the CC0 Public 390 | Domain Dedication. Except for the limited purpose of indicating that 391 | material is shared under a Creative Commons public license or as 392 | otherwise permitted by the Creative Commons policies published at 393 | creativecommons.org/policies, Creative Commons does not authorize the 394 | use of the trademark "Creative Commons" or any other trademark or logo 395 | of Creative Commons without its prior written consent including, 396 | without limitation, in connection with any unauthorized modifications 397 | to any of its public licenses or any other arrangements, 398 | understandings, or agreements concerning use of licensed material. For 399 | the avoidance of doubt, this paragraph does not form part of the 400 | public licenses. 401 | 402 | Creative Commons may be contacted at creativecommons.org. 403 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 |
3 | 4 | logo 5 | 6 |

7 | Get Job Ready with 90 Leetcode Problems! 8 |

9 |

10 | Starting Soon On My Youtube Channel ! 11 |

12 |

13 | A CodeColts Project 14 |

15 | 16 | 17 | 18 |

19 | 20 | contributors 21 | 22 | 23 | last update 24 | 25 | 26 | forks 27 | 28 | 29 | stars 30 | 31 | Creative Commons License 32 | 33 | [![YouTube Channel Views](https://img.shields.io/youtube/channel/views/UCKkj-sW42Jj2eS6UlvbEgsA?label=IntrovertCoder)](https://www.youtube.com/introvertCoder/) 34 |

35 | 36 |

37 | View Code 38 | · 39 | Documentation 40 | · 41 | Report Bug 42 | · 43 | Request Feature 44 |

45 |
46 | 47 |
48 | 49 | 50 | ## :star2: About the Project 51 | 52 |
53 | Welcome to the "90 Days of DSA" repository! 🎉 This is your one-stop destination for daily coding practice with Data Structures and Algorithms (DSA) problems. Whether you are preparing for technical interviews or just looking to enhance your problem-solving skills, we've got you covered! 54 | 55 |
56 | 57 | 58 | 59 | 60 | 61 |
62 | 63 | ## 📅 Daily Challenge 64 | 65 | Each day, we'll release a new DSA problem along with its solution. You can use this repository to: 66 | 67 | - 🧠 Learn and understand various DSA concepts. 68 | - 💻 Implement solutions in Java. 69 | - 🚀 Sharpen your problem-solving skills. 70 | 71 | Feel free to contribute your own solutions or improve existing ones! Let's learn and grow together as a community. 72 | 73 | ## 🛠️ Getting Started 74 | 75 | 1. Clone the repository to your local machine: 76 | 77 | ```bash 78 | git clone https://github.com/codeintrovert/90DaysOfDSA 79 | ``` 80 | 2. Navigate to the `src` folder 81 | ```bash 82 | cd ./90DaysOfDSA/src 83 | ``` 84 | 85 | 86 | ## :space_invader: Tech Stack 87 | 88 | 89 | - Java 90 | - TypeScript 91 | - Python 92 | 93 | 94 | 95 | 96 | ## :eyes: Live Project Coming Soon! 97 | 104 | 105 | 106 | ## :compass: Roadmap 107 | - Video Series Comming Soon! 108 | * [x] 10 Days Problem Solving Streak 109 | * [x] 20 Days Problem Solving Streak 110 | * [x] 40 Days Problem Solving Streak 111 | * [x] 60 Days Problem Solving Streak 112 | * [ ] 90 Days Problem Solving Streak 113 | 114 | 115 | 116 | ## 🤝 Contributing 117 | 118 | 119 | 120 | 121 | 122 | 123 | Contributions are welcome and encouraged! Whether you find a bug, have a suggestion, or want to add your own solution, please follow the [contribution guidelines](https://github.com/codeIntrovert/90daysOfDSA/blob/main/CONTRIBUTING.md) 124 | 125 | 126 | 127 | ## :warning: License 128 | 129 | The materials herein are all © 2023 Hasan Imam. 130 | 131 | Creative Commons License
This work is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 4.0 Unported License. 132 | 133 | 134 | 135 | ## :handshake: Contact 136 | 137 | Hasan Imam 138 | 139 | Email: codeColts@gmail.com 140 | Instagram: [@introvertCoder](https://instagram.com/introvertcoder) 141 | Youtube: [@introvertcoder](https://www.youtube.com/introvertCoder) 142 | Discord: introvertCoder 143 | 144 | 145 | ## :gem: Acknowledgements 146 | 147 | - [LeetCode](https://leetcode.com/) 148 | - [CodeColts](https://codecolts.vercel.app/) 149 | - [Shields.io](https://shields.io/) 150 | - [Awesome README](https://github.com/matiassingers/awesome-readme) 151 | 152 | ## :pen: Developer's Note: 153 | #### `5/May/2024` 154 | ```html 155 | This GitHub Repo now has over 60 solutions, 156 | We're back! repo will be updated with more solutions and a video series soon! 157 | 158 | Feel free to open new issues or collaborate by fixing existing ones. 159 | Stay tuned on my YouTube Channel for updates. 160 | Happy Coding! 161 | ``` 162 | 163 | [![Youtube: introvertCoder](https://img.shields.io/badge/YouTube-FF0000?style=for-the-badge&logo=youtube&logoColor=white)](https://www.youtube.com/introvertCoder/) 164 | -------------------------------------------------------------------------------- /assets/logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codeIntrovert/90daysOfDSA/7e6685f54e7a941b5b60a84ee647566d5a4c8c35/assets/logo.png -------------------------------------------------------------------------------- /learn.md: -------------------------------------------------------------------------------- 1 | # Welcome to 90DaysOfDSA! 🚀 2 | 3 | Hey there, future problem solvers and coding enthusiasts! Welcome to my GitHub repository, "90DaysOfDSA". Here, we embark on a journey together to dive deep into Data Structures and Algorithms (DSA) by solving 90 LeetCode problems over the span of 90 days, all using Java. 📚💻 4 | 5 | [![Youtube: introvertCoder](https://img.shields.io/badge/YouTube-FF0000?style=for-the-badge&logo=youtube&logoColor=white)](https://www.youtube.com/introvertCoder/) 6 | 7 | 8 | ## What's the Plan? 9 | 10 | Every day for the next 90 days, I'll be posting a new LeetCode problem along with its solution in Java. Each day's solution will be neatly organized under the `./src` directory, categorized by days from Day01 to Day90. Within each day's folder, you'll find the LeetCode problem link and its corresponding Java solution. 📝 11 | 12 | ## Let's Learn Together! 🤓 13 | 14 | Learning DSA doesn't have to be intimidating! Whether you're a seasoned developer or just starting out, this repository is for you. Each solution is accompanied by a YouTube short video, where I'll explain the problem and walk you through the thought process behind the solution in just one video per problem. 📹 15 | 16 | ## Why Java? ☕ 17 | 18 | Java remains one of the most widely used programming languages in the IT industry. By mastering DSA with Java, you're not only enhancing your problem-solving skills but also positioning yourself for success in job interviews and real-world projects. 🌟 19 | 20 | ## How You Can Contribute 🌈 21 | 22 | If you're passionate about DSA and Java like me, I encourage you to join this journey! Feel free to contribute your own solutions, provide feedback, or suggest improvements. Together, we can create a vibrant community of learners and problem solvers. 🙌 23 | 24 | ## Stay Connected! 📱 25 | 26 | Don't miss out on any updates or new videos! Hit the subscribe button on my YouTube channel to stay notified about each new video in this series. Let's learn, grow, and succeed together! 💪 27 | 28 | Happy coding! 🚀 29 | 30 | [![Youtube: introvertCoder](https://img.shields.io/badge/YouTube-FF0000?style=for-the-badge&logo=youtube&logoColor=white)](https://www.youtube.com/introvertCoder/) 31 | -------------------------------------------------------------------------------- /src/Day 01/roman.java: -------------------------------------------------------------------------------- 1 | public class roman { 2 | public static void main(String[] args) { 3 | System.out.println(romanToInt("MVII")); 4 | System.out.println("\n"); 5 | System.out.println(romanSwitch("XI")); 6 | /* 7 | * I= 1 V= 5 X= 10 L= 50 C= 100 D= 500 M= 1000 8 | */ 9 | } 10 | 11 | public static int romanSwitch(String romanInput) { 12 | 13 | int total = 0; 14 | 15 | for (int i = 0; i < romanInput.length(); i++) { 16 | switch (romanInput.charAt(i)) { 17 | case 'I': 18 | total += 1; 19 | break; 20 | case 'V': 21 | total += 5; 22 | break; 23 | case 'X': 24 | total += 10; 25 | break; 26 | case 'L': 27 | total += 50; 28 | break; 29 | case 'C': 30 | total += 100; 31 | break; 32 | case 'D': 33 | total += 500; 34 | break; 35 | case 'M': 36 | total += 1000; 37 | break; 38 | } 39 | } 40 | return total; 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /src/Day 02/palindrome.java: -------------------------------------------------------------------------------- 1 | class palindrome { 2 | public static void main(String[] args) { 3 | 4 | int x = 101; 5 | System.out.println(checkPalindrome(x)); 6 | } 7 | 8 | public static boolean checkPalindrome(int input) { 9 | int number = input; 10 | int reversedNumber = 0; 11 | if (input < 0) { 12 | return false; 13 | } 14 | 15 | // reverse number 16 | while (number != 0) { 17 | int digit = number % 10; 18 | reversedNumber = reversedNumber * 10 + digit; 19 | number = number / 10; 20 | } 21 | 22 | return input == reversedNumber; 23 | } 24 | } -------------------------------------------------------------------------------- /src/Day 03/LastWord.java: -------------------------------------------------------------------------------- 1 | public class LastWord { 2 | 3 | /* 4 | * Here we will be given a string, 5 | * we need to find the lenght of last word 6 | * we use trim() to remove spaces from end and front 7 | * we use String[] words = trimmedString.split("") 8 | * this will split the words on spaces " "; 9 | * and store them in an array of words 10 | */ 11 | public static void main(String[] args) { 12 | 13 | String input = "Hello this is introvertCoder speaking I love cats"; 14 | // should output 4 15 | System.out.println(LastWordFunc(input)); 16 | } 17 | 18 | public static int LastWordFunc(String s) { 19 | 20 | String trimmedString = s.trim(); 21 | 22 | String[] words = trimmedString.split(" "); 23 | int lenght = words[words.length - 1].length(); 24 | 25 | return lenght; 26 | } 27 | 28 | } 29 | -------------------------------------------------------------------------------- /src/Day 04/bestStock.java: -------------------------------------------------------------------------------- 1 | public class bestStock { 2 | public static void main(String[] args) { 3 | int[] prices = { 7, 1, 0, 3, 6, 4 }; 4 | System.out.println(maxProfit(prices)); 5 | } 6 | 7 | public static int maxProfit(int[] prices) { 8 | int buy = Integer.MAX_VALUE; 9 | int profit = 0; 10 | 11 | for (int i = 0; i < prices.length; i++) { 12 | buy = Math.min(buy, prices[i]); 13 | profit = Math.max(profit, prices[i] - buy); 14 | 15 | } 16 | return profit; 17 | } 18 | 19 | } 20 | -------------------------------------------------------------------------------- /src/Day 05/integerToRoman.java: -------------------------------------------------------------------------------- 1 | // https://leetcode.com/problems/integer-to-roman/description/ 2 | 3 | public class integerToRoman { 4 | public static String intToRoman(int num) { 5 | int[] values = { 1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1 }; 6 | String[] symbols = { "M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I" }; 7 | 8 | StringBuilder result = new StringBuilder(); 9 | int i = 0; 10 | 11 | while (num > 0) { 12 | if (num >= values[i]) { 13 | result.append(symbols[i]); 14 | num -= values[i]; 15 | } else { 16 | i++; 17 | } 18 | } 19 | 20 | return result.toString(); 21 | } 22 | 23 | public static void main(String[] args) { 24 | // Example usage 25 | int num1 = 3; 26 | int num2 = 58; 27 | int num3 = 1994; 28 | 29 | System.out.println("Example 1: " + intToRoman(num1)); 30 | System.out.println("Example 2: " + intToRoman(num2)); 31 | System.out.println("Example 3: " + intToRoman(num3)); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/Day 06/needleHaystack.java: -------------------------------------------------------------------------------- 1 | public class needleHaystack { 2 | // https://leetcode.com/problems/find-the-index-of-the-first-occurrence-in-a-string/description/ 3 | public static int strStr(String haystack, String needle) { 4 | int index = haystack.indexOf(needle); 5 | if (index != -1) { // if word is found 6 | return index; 7 | } else { 8 | return -1; // else 9 | } 10 | } 11 | 12 | public static void main(String[] args) { 13 | // Example usage: 14 | String haystack1 = "sadbutsad"; 15 | String needle1 = "but"; 16 | System.out.println(strStr(haystack1, needle1)); // Output: 0 17 | 18 | String haystack2 = "leetcode"; 19 | String needle2 = "leeto"; 20 | System.out.println(strStr(haystack2, needle2)); // Output: -1 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /src/Day 07/ExcelSheet.java: -------------------------------------------------------------------------------- 1 | // https://leetcode.com/problems/excel-sheet-column-title/ 2 | public class ExcelSheet { 3 | 4 | public static String convertToTitle(int columnNumber) { 5 | StringBuilder result = new StringBuilder(); 6 | 7 | while (columnNumber > 0) { 8 | // Adjusting columnNumber to 0-based index 9 | columnNumber--; 10 | 11 | char digit = (char) ('A' + columnNumber % 26); 12 | result.insert(0, digit); 13 | 14 | // Move to the next digit 15 | columnNumber /= 26; 16 | } 17 | 18 | return result.toString(); 19 | } 20 | 21 | public static void main(String[] args) { 22 | // Example usage: 23 | System.out.println(convertToTitle(1)); // Output: "A" 24 | System.out.println(convertToTitle(28)); // Output: "AB" 25 | System.out.println(convertToTitle(701)); // Output: "ZY" 26 | } 27 | } 28 | /* 29 | * This convertToTitle function takes an integer columnNumber as input and 30 | * returns its corresponding Excel column title as a string. The function uses a 31 | * StringBuilder to build the result in reverse order while continuously 32 | * dividing the columnNumber by 26 until it becomes 0. The conversion is 33 | * adjusted to a 0-based index, and the result is then reversed before 34 | * returning. 35 | * 36 | * Certainly! Let me explain the logic of the `convertToTitle` function in detail: 37 | 38 | 1. The function starts by creating a `StringBuilder` named `result` to store the Excel column title. 39 | 40 | 2. In a while loop, the function iterates as long as `columnNumber` is greater than 0. 41 | 42 | 3. Inside the loop, the variable `digit` is calculated. It represents the rightmost character of the current column title. The calculation involves adding the ASCII value of 'A' to the remainder when `columnNumber` is divided by 26. 43 | 44 | 4. The calculated `digit` is then inserted at the beginning of the `result` using `result.insert(0, digit)` to build the column title in reverse order. 45 | 46 | 5. Next, `columnNumber` is adjusted to a 0-based index by subtracting 1. This is because in Excel, the column indexing starts from 1, but in this calculation, we want it to start from 0. 47 | 48 | 6. After that, `columnNumber` is divided by 26 to move on to the next digit in the column title. 49 | 50 | 7. The loop continues until `columnNumber` becomes 0. 51 | 52 | 8. Finally, the `result` is converted to a string, and the function returns the Excel column title. 53 | 54 | In the example of `convertToTitle(28)`, the loop runs as follows: 55 | - First iteration: `columnNumber = 28`, `digit = 'B'`, `result = "B"` 56 | - Second iteration: `columnNumber = 1`, `digit = 'A'`, `result = "AB"` 57 | 58 | So, the function returns "AB" as the Excel column title for the input 28. The same logic applies to other examples provided. 59 | */ -------------------------------------------------------------------------------- /src/Day 08/ReverseInteger.java: -------------------------------------------------------------------------------- 1 | // https://leetcode.com/problems/reverse-integer/ 2 | public class ReverseInteger { 3 | public static int reverse(int x) { 4 | int result = 0; 5 | 6 | while (x != 0) { 7 | int digit = x % 10; 8 | 9 | // Check for overflow before updating result 10 | if (result > Integer.MAX_VALUE / 10 || (result == Integer.MAX_VALUE / 10 && digit > 7)) { 11 | return 0; 12 | } 13 | if (result < Integer.MIN_VALUE / 10 || (result == Integer.MIN_VALUE / 10 && digit < -8)) { 14 | return 0; 15 | } 16 | 17 | result = result * 10 + digit; 18 | x /= 10; 19 | } 20 | 21 | return result; 22 | } 23 | 24 | public static void main(String[] args) { 25 | // Example usage: 26 | int x1 = 123; 27 | int x2 = -123; 28 | int x3 = 120; 29 | 30 | System.out.println("Input: " + x1 + ", Output: " + reverse(x1)); 31 | System.out.println("Input: " + x2 + ", Output: " + reverse(x2)); 32 | System.out.println("Input: " + x3 + ", Output: " + reverse(x3)); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /src/Day 09/singleNumber.java: -------------------------------------------------------------------------------- 1 | /* 136. Single Number 2 | * In This program we will be given an array, 3 | * we will need to return the only "single number" in the array 4 | * we will use XOR operator here 5 | */ 6 | 7 | public class singleNumber { 8 | public static void main(String[] args) { 9 | int[] inputs = { 2, 2, 4, 1, 4 }; 10 | System.out.println(singleNumberXOR(inputs)); 11 | } 12 | 13 | public static int singleNumberXOR(int nums[]) { 14 | int result = 0; 15 | // XOR 16 | for (int num : nums) { 17 | result ^= num; 18 | } 19 | return result; 20 | } 21 | 22 | } 23 | -------------------------------------------------------------------------------- /src/Day 10/uglyNumber.java: -------------------------------------------------------------------------------- 1 | public class uglyNumber { 2 | public static void main(String[] args) { 3 | System.out.println(isUgly(77)); 4 | } 5 | 6 | static boolean isUgly(int n) { 7 | 8 | if (n == 0) { 9 | return false; 10 | } 11 | while (n != 1) { 12 | if (n % 2 == 0) { 13 | n = n / 2; 14 | continue; 15 | } 16 | if (n % 3 == 0) { 17 | n = n / 3; 18 | continue; 19 | } 20 | if (n % 5 == 0) { 21 | n = n / 5; 22 | continue; 23 | } 24 | return false; 25 | } 26 | return true; 27 | 28 | } 29 | } -------------------------------------------------------------------------------- /src/Day 11/wordPattern.java: -------------------------------------------------------------------------------- 1 | // https://leetcode.com/problems/word-pattern/description/ 2 | // hashmap problem 3 | 4 | import java.util.HashMap; 5 | 6 | public class wordPattern { 7 | 8 | public static boolean wordPatternCheck(String pattern, String s) { 9 | String[] word = s.split(" "); 10 | if (word.length != pattern.length()) 11 | return false; 12 | 13 | HashMap map = new HashMap<>(); 14 | for (int i = 0; i < pattern.length(); i++) { 15 | char c = pattern.charAt(i); 16 | 17 | if (map.containsKey(c)) { 18 | if (!map.get(c).equals(word[i])) { 19 | return false; 20 | } 21 | } else { 22 | if (map.containsValue(word[i])) { 23 | return false; 24 | } 25 | map.put(c, word[i]); 26 | } 27 | } 28 | 29 | return true; 30 | } 31 | 32 | public static void main(String[] args) { 33 | // Example 1: 34 | System.out.println(wordPatternCheck("abba", "dog cat cat dog")); // Output: true 35 | 36 | // Example 2: 37 | System.out.println(wordPatternCheck("abba", "dog cat cat fish")); // Output: false 38 | 39 | // Example 3: 40 | System.out.println(wordPatternCheck("aaaa", "dog cat cat dog")); // Output: false 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /src/Day 12/VaildParentheses.java: -------------------------------------------------------------------------------- 1 | import java.util.Stack; 2 | 3 | public class VaildParentheses { 4 | public static boolean isValid(String s) { 5 | Stack stack = new Stack<>(); 6 | 7 | for (char c : s.toCharArray()) { 8 | if (c == '(' || c == '{' || c == '[') { 9 | stack.push(c); 10 | } else { 11 | if (stack.isEmpty()) { 12 | return false; // Closing bracket with no corresponding open bracket 13 | } 14 | 15 | char openBracket = stack.pop(); 16 | 17 | if ((c == ')' && openBracket != '(') || 18 | (c == '}' && openBracket != '{') || 19 | (c == ']' && openBracket != '[')) { 20 | return false; // Mismatched open and close brackets 21 | } 22 | } 23 | } 24 | 25 | return stack.isEmpty(); // Check if there are any remaining open brackets 26 | } 27 | 28 | public static void main(String[] args) { 29 | // Test cases 30 | System.out.println(isValid("()")); // Output: true 31 | System.out.println(isValid("()[]{}")); // Output: true 32 | System.out.println(isValid("(]")); // Output: false 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /src/Day 13/TwoSum.java: -------------------------------------------------------------------------------- 1 | import java.util.HashMap; 2 | 3 | public class TwoSum { 4 | public static int[] twoSum(int[] nums, int target) { 5 | // Create a hash map to store the numbers and their indices 6 | HashMap numMap = new HashMap<>(); 7 | 8 | // Iterate through the array 9 | for (int i = 0; i < nums.length; i++) { 10 | int complement = target - nums[i]; 11 | 12 | // Check if the complement is already in the map 13 | if (numMap.containsKey(complement)) { 14 | // If found, return the indices of the two numbers 15 | return new int[] { numMap.get(complement), i }; 16 | } 17 | 18 | // Otherwise, add the current number and its index to the map 19 | numMap.put(nums[i], i); 20 | } 21 | 22 | // No solution found, so return an empty array 23 | return new int[] {}; 24 | } 25 | 26 | public static void main(String[] args) { 27 | // Test cases 28 | int[] nums1 = { 2, 7, 11, 15 }; 29 | int target1 = 9; 30 | int[] result1 = twoSum(nums1, target1); 31 | System.out.println("Output 1: [" + result1[0] + ", " + result1[1] + "]"); 32 | 33 | int[] nums2 = { 3, 2, 4 }; 34 | int target2 = 6; 35 | int[] result2 = twoSum(nums2, target2); 36 | System.out.println("Output 2: [" + result2[0] + ", " + result2[1] + "]"); 37 | 38 | int[] nums3 = { 3, 3 }; 39 | int target3 = 6; 40 | int[] result3 = twoSum(nums3, target3); 41 | System.out.println("Output 3: [" + result3[0] + ", " + result3[1] + "]"); 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /src/Day 14/CommonPrefix.java: -------------------------------------------------------------------------------- 1 | import java.util.Arrays; 2 | 3 | public class CommonPrefix { 4 | public String longestCommonPrefix(String[] strs) { 5 | Arrays.sort(strs); 6 | String s1 = strs[0]; 7 | String s2 = strs[strs.length - 1]; 8 | int i = 0; 9 | while (i < s1.length() && i < s2.length()) { 10 | if (s1.charAt(i) == s2.charAt(i)) { 11 | i++; 12 | } else { 13 | break; 14 | } 15 | } 16 | return s1.substring(0, i); 17 | } 18 | 19 | public static void main(String[] args) { 20 | CommonPrefix CommonPrefix = new CommonPrefix(); 21 | String[] input = { "flower", "flow", "flight" }; // Create an array of strings 22 | System.out.println(CommonPrefix.longestCommonPrefix(input)); // Call the method on the instance 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/Day 15/containsDuplicate.java: -------------------------------------------------------------------------------- 1 | import java.util.HashSet; 2 | 3 | // https://leetcode.com/problems/contains-duplicate/description/ 4 | public class containsDuplicate { 5 | public static boolean containsDuplicateCheck(int[] nums) { 6 | HashSet set = new HashSet<>(); 7 | 8 | for (int num : nums) { 9 | if (set.contains(num)) { 10 | return true; // Found a duplicate 11 | } 12 | set.add(num); 13 | } 14 | 15 | return false; // No duplicates found 16 | } 17 | 18 | public static void main(String[] args) { 19 | 20 | int[] nums1 = { 1, 2, 3, 1 }; 21 | System.out.println("Output 1: " + containsDuplicateCheck(nums1)); // Output: true 22 | 23 | int[] nums2 = { 1, 2, 3, 4 }; 24 | System.out.println("Output 2: " + containsDuplicateCheck(nums2)); // Output: false 25 | 26 | int[] nums3 = { 1, 1, 1, 3, 3, 4, 3, 2, 4, 2 }; 27 | System.out.println("Output 3: " + containsDuplicateCheck(nums3)); // Output: true 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/Day 16/WordPalidrome.java: -------------------------------------------------------------------------------- 1 | class WordPalindrome { 2 | 3 | public static boolean isPalindrome(String s) { 4 | s = s.trim(); 5 | if (s == null) 6 | return true; 7 | s = s.replaceAll("[^a-zA-Z0-9]", ""); 8 | if (s.isEmpty()) 9 | return true; 10 | s = s.toLowerCase(); 11 | int start = 0; 12 | int end = s.length() - 1; 13 | while (start < end) { 14 | if (s.charAt(start) == s.charAt(end)) { 15 | start++; 16 | end--; 17 | } else { 18 | return false; 19 | } 20 | } 21 | return true; 22 | } 23 | 24 | public static void main(String[] args) { 25 | System.out.println(isPalindrome("tacocat")); 26 | } 27 | } -------------------------------------------------------------------------------- /src/Day 17/removeDuplicates.java: -------------------------------------------------------------------------------- 1 | // https://leetcode.com/problems/remove-duplicates-from-sorted-array/description/ 2 | 3 | public class removeDuplicates { 4 | public int removeDuplicatesfunc(int[] nums) { 5 | if (nums.length == 0) { 6 | return 0; // Return 0 if the array is empty 7 | } 8 | 9 | int uniqueIndex = 1; // Pointer to the position where the next unique element should be placed 10 | 11 | for (int i = 1; i < nums.length; i++) { 12 | // If the current element is different from the previous element, 13 | // move it to the position indicated by uniqueIndex 14 | if (nums[i] != nums[i - 1]) { 15 | nums[uniqueIndex++] = nums[i]; 16 | } 17 | } 18 | 19 | return uniqueIndex; // Return the number of unique elements 20 | } 21 | 22 | public static void main(String[] args) { 23 | removeDuplicates solution = new removeDuplicates(); 24 | 25 | int[] nums1 = { 1, 1, 2 }; 26 | int k1 = solution.removeDuplicatesfunc(nums1); 27 | System.out.println("Output 1: " + k1); // Output: 2 28 | 29 | int[] nums2 = { 0, 0, 1, 1, 1, 2, 2, 3, 3, 4 }; 30 | int k2 = solution.removeDuplicatesfunc(nums2); 31 | System.out.println("Output 2: " + k2); // Output: 5 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/Day 18/addBinary.java: -------------------------------------------------------------------------------- 1 | public class addBinary { 2 | public String addBinaryfunc(String a, String b) { 3 | StringBuilder result = new StringBuilder(); 4 | 5 | int carry = 0; 6 | int i = a.length() - 1; 7 | int j = b.length() - 1; 8 | 9 | while (i >= 0 || j >= 0) { 10 | int sum = carry; 11 | if (i >= 0) { 12 | sum += a.charAt(i--) - '0'; 13 | } 14 | if (j >= 0) { 15 | sum += b.charAt(j--) - '0'; 16 | } 17 | 18 | result.insert(0, sum % 2); // Insert the least significant bit of the sum 19 | carry = sum / 2; // Update the carry for the next iteration 20 | } 21 | 22 | if (carry != 0) { 23 | result.insert(0, carry); // If there's a carry after the loop, insert it 24 | } 25 | 26 | return result.toString(); 27 | } 28 | 29 | public static void main(String[] args) { 30 | addBinary solution = new addBinary(); 31 | 32 | String a1 = "11"; 33 | String b1 = "1"; 34 | System.out.println("Output 1: " + solution.addBinaryfunc(a1, b1)); // Output: "100" 35 | 36 | String a2 = "1010"; 37 | String b2 = "1011"; 38 | System.out.println("Output 2: " + solution.addBinaryfunc(a2, b2)); // Output: "10101" 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /src/Day 19/Candies.java: -------------------------------------------------------------------------------- 1 | // https://leetcode.com/problems/kids-with-the-greatest-number-of-candies/description/?envType=study-plan-v2&envId=leetcode-75 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | public class Candies { 7 | public List kidsWithCandies(int[] candies, int extraCandies) { 8 | List result = new ArrayList<>(); 9 | 10 | // Find the maximum number of candies among all kids 11 | int maxCandies = 0; 12 | for (int candy : candies) { 13 | maxCandies = Math.max(maxCandies, candy); 14 | } 15 | 16 | // Check if each kid can have the greatest number of candies after adding extra 17 | // candies 18 | for (int candy : candies) { 19 | result.add(candy + extraCandies >= maxCandies); 20 | } 21 | 22 | return result; 23 | } 24 | 25 | public static void main(String[] args) { 26 | Candies solution = new Candies(); 27 | 28 | int[] candies1 = { 2, 3, 5, 1, 3 }; 29 | int extraCandies1 = 3; 30 | System.out.println("Output 1: " + solution.kidsWithCandies(candies1, extraCandies1)); // Output: 31 | // [true,true,true,false,true] 32 | 33 | int[] candies2 = { 4, 2, 1, 1, 2 }; 34 | int extraCandies2 = 1; 35 | System.out.println("Output 2: " + solution.kidsWithCandies(candies2, extraCandies2)); // Output: 36 | // [true,false,false,false,false] 37 | 38 | int[] candies3 = { 12, 1, 12 }; 39 | int extraCandies3 = 10; 40 | System.out.println("Output 3: " + solution.kidsWithCandies(candies3, extraCandies3)); // Output: 41 | // [true,false,true] 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /src/Day 20/CanPlaceFlowers.java: -------------------------------------------------------------------------------- 1 | //https://leetcode.com/problems/can-place-flowers/description/?envType=study-plan-v2&envId=leetcode-75 2 | public class CanPlaceFlowers { 3 | public boolean canPlaceFlowers(int[] flowerbed, int n) { 4 | int count = 0; 5 | int length = flowerbed.length; 6 | 7 | // Iterate through the flowerbed array 8 | for (int i = 0; i < length; i++) { 9 | // Check if the current plot is empty and its adjacent plots are also empty 10 | if (flowerbed[i] == 0 && (i == 0 || flowerbed[i - 1] == 0) && (i == length - 1 || flowerbed[i + 1] == 0)) { 11 | flowerbed[i] = 1; // Plant a flower 12 | count++; // Increment the count of planted flowers 13 | } 14 | } 15 | 16 | // Check if the required number of flowers can be planted 17 | return count >= n; 18 | } 19 | 20 | public static void main(String[] args) { 21 | CanPlaceFlowers FlowerCheck = new CanPlaceFlowers(); 22 | 23 | int[] flowerbed1 = { 1, 0, 0, 0, 1 }; 24 | int n1 = 1; 25 | System.out.println("Output 1: " + FlowerCheck.canPlaceFlowers(flowerbed1, n1)); // Output: true 26 | 27 | int[] flowerbed2 = { 1, 0, 0, 0, 1 }; 28 | int n2 = 2; 29 | System.out.println("Output 2: " + FlowerCheck.canPlaceFlowers(flowerbed2, n2)); // Output: false 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /src/Day 21/ReverseWords.java: -------------------------------------------------------------------------------- 1 | //https://leetcode.com/problems/reverse-words-in-a-string/description/?envType=study-plan-v2&envId=leetcode-75 2 | 3 | public class ReverseWords { 4 | public String reverseWords(String s) { 5 | // Split the input string by spaces 6 | String[] words = s.trim().split("\\s+"); 7 | StringBuilder result = new StringBuilder(); 8 | 9 | // Append words to StringBuilder in reverse order 10 | for (int i = words.length - 1; i >= 0; i--) { 11 | result.append(words[i]); 12 | if (i > 0) { 13 | result.append(" "); 14 | } 15 | } 16 | 17 | return result.toString(); 18 | } 19 | 20 | public static void main(String[] args) { 21 | ReverseWords rw = new ReverseWords(); 22 | 23 | // Test cases 24 | String s1 = "the sky is blue"; 25 | System.out.println("Input: " + s1); 26 | System.out.println("Output: " + rw.reverseWords(s1)); // Expected: "blue is sky the" 27 | 28 | String s2 = " hello world "; 29 | System.out.println("Input: " + s2); 30 | System.out.println("Output: " + rw.reverseWords(s2)); // Expected: "world hello" 31 | 32 | String s3 = "a good example"; 33 | System.out.println("Input: " + s3); 34 | System.out.println("Output: " + rw.reverseWords(s3)); // Expected: "example good a" 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /src/Day 22/CompressCharacters.java: -------------------------------------------------------------------------------- 1 | //https://leetcode.com/problems/string-compression/description/?envType=study-plan-v2&envId=leetcode-75 2 | public class CompressCharacters { 3 | public int compress(char[] chars) { 4 | int index = 0; // Index to update chars array 5 | int i = 0; // Index to traverse chars array 6 | 7 | while (i < chars.length) { 8 | char currentChar = chars[i]; 9 | int count = 0; // Count of consecutive repeating characters 10 | 11 | // Count consecutive repeating characters 12 | while (i < chars.length && chars[i] == currentChar) { 13 | i++; 14 | count++; 15 | } 16 | 17 | // Update chars array with compressed characters 18 | chars[index++] = currentChar; 19 | if (count > 1) { 20 | for (char c : Integer.toString(count).toCharArray()) { 21 | chars[index++] = c; 22 | } 23 | } 24 | } 25 | 26 | return index; // Return the new length of the array 27 | } 28 | 29 | public static void main(String[] args) { 30 | CompressCharacters cc = new CompressCharacters(); 31 | 32 | // Test cases 33 | char[] chars1 = { 'a', 'a', 'b', 'b', 'c', 'c', 'c' }; 34 | int len1 = cc.compress(chars1); 35 | System.out.print( 36 | "Output: Return " + len1 + ", and the first " + len1 + " characters of the input array should be: ["); 37 | for (int i = 0; i < len1; i++) { 38 | if (i > 0) 39 | System.out.print(", "); 40 | System.out.print("'" + chars1[i] + "'"); 41 | } 42 | System.out.println("]"); // Expected: Return 6, and the first 6 characters of the input array should be: 43 | // ['a', '2', 'b', '2', 'c', '3'] 44 | 45 | char[] chars2 = { 'a' }; 46 | int len2 = cc.compress(chars2); 47 | System.out.print( 48 | "Output: Return " + len2 + ", and the first " + len2 + " character of the input array should be: ["); 49 | for (int i = 0; i < len2; i++) { 50 | if (i > 0) 51 | System.out.print(", "); 52 | System.out.print("'" + chars2[i] + "'"); 53 | } 54 | System.out.println("]"); // Expected: Return 1, and the first character of the input array should be: 55 | // ['a'] 56 | 57 | char[] chars3 = { 'a', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b', 'b' }; 58 | int len3 = cc.compress(chars3); 59 | System.out.print( 60 | "Output: Return " + len3 + ", and the first " + len3 + " characters of the input array should be: ["); 61 | for (int i = 0; i < len3; i++) { 62 | if (i > 0) 63 | System.out.print(", "); 64 | System.out.print("'" + chars3[i] + "'"); 65 | } 66 | System.out.println("]"); // Expected: Return 4, and the first 4 characters of the input array should be: 67 | // ['a', 'b', '1', '2'] 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /src/Day 23/HighestAltitude.java: -------------------------------------------------------------------------------- 1 | public class HighestAltitude { 2 | public int largestAltitude(int[] gain) { 3 | int maxAltitude = 0; // Initialize the maximum altitude 4 | int currentAltitude = 0; // Initialize the current altitude 5 | 6 | // Traverse the gain array to compute altitudes 7 | for (int i = 0; i < gain.length; i++) { 8 | currentAltitude += gain[i]; // Update the current altitude 9 | maxAltitude = Math.max(maxAltitude, currentAltitude); // Update the maximum altitude 10 | } 11 | 12 | return maxAltitude; // Return the highest altitude 13 | } 14 | 15 | public static void main(String[] args) { 16 | HighestAltitude ha = new HighestAltitude(); 17 | 18 | // Test cases 19 | int[] gain1 = { -5, 1, 5, 0, -7 }; 20 | System.out.println("Output: " + ha.largestAltitude(gain1)); // Expected: 1 21 | 22 | int[] gain2 = { -4, -3, -2, -1, 4, 3, 2 }; 23 | System.out.println("Output: " + ha.largestAltitude(gain2)); // Expected: 0 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /src/Day 24/MergeStrings.java: -------------------------------------------------------------------------------- 1 | public class MergeStrings { 2 | public String mergeAlternately(String word1, String word2) { 3 | StringBuilder merged = new StringBuilder(); 4 | int i = 0, j = 0; 5 | 6 | // Merge strings by adding letters in alternating order 7 | while (i < word1.length() || j < word2.length()) { 8 | if (i < word1.length()) { 9 | merged.append(word1.charAt(i)); 10 | i++; 11 | } 12 | if (j < word2.length()) { 13 | merged.append(word2.charAt(j)); 14 | j++; 15 | } 16 | } 17 | 18 | return merged.toString(); 19 | } 20 | 21 | public static void main(String[] args) { 22 | MergeStrings ms = new MergeStrings(); 23 | 24 | // Test cases 25 | String word1 = "abc", word2 = "pqr"; 26 | System.out.println("Output: " + ms.mergeAlternately(word1, word2)); // Expected: "apbqcr" 27 | 28 | word1 = "ab"; 29 | word2 = "pqrs"; 30 | System.out.println("Output: " + ms.mergeAlternately(word1, word2)); // Expected: "apbqrs" 31 | 32 | word1 = "abcd"; 33 | word2 = "pq"; 34 | System.out.println("Output: " + ms.mergeAlternately(word1, word2)); // Expected: "apbqcd" 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /src/Day 25/ReverseVowels.java: -------------------------------------------------------------------------------- 1 | // https://leetcode.com/problems/reverse-vowels-of-a-string/description/?envType=study-plan-v2&envId=leetcode-75 2 | public class ReverseVowels { 3 | public String reverseVowels(String s) { 4 | char[] chars = s.toCharArray(); 5 | int i = 0, j = chars.length - 1; 6 | boolean[] isVowel = new boolean[256]; // Assuming ASCII characters 7 | isVowel['a'] = isVowel['e'] = isVowel['i'] = isVowel['o'] = isVowel['u'] = true; 8 | isVowel['A'] = isVowel['E'] = isVowel['I'] = isVowel['O'] = isVowel['U'] = true; 9 | /* 10 | * ye boolean 256 bata raha hai ki ek array banao jisme saare characters ho 11 | * ascii ke, i.e A-Z, a-z aur @#$%^&*() etc 12 | * 13 | * usme se a,e, i, o, u aur AEIOU ko true karo baaki ko false 14 | * hum log isko normal array {a,e,i,o,u} bana ke bhi check kar sakte hain but 15 | * that would be slow 16 | */ 17 | while (i < j) { 18 | while (i < j && !isVowel[chars[i]]) { 19 | i++; 20 | } 21 | while (i < j && !isVowel[chars[j]]) { 22 | j--; 23 | } 24 | if (i < j) { 25 | char temp = chars[i]; 26 | chars[i] = chars[j]; 27 | chars[j] = temp; 28 | i++; 29 | j--; 30 | } 31 | } 32 | 33 | return new String(chars); 34 | } 35 | 36 | public static void main(String[] args) { 37 | ReverseVowels rv = new ReverseVowels(); 38 | 39 | // Test cases 40 | String s1 = "hello"; 41 | System.out.println("Input: " + s1); 42 | System.out.println("Output: " + rv.reverseVowels(s1)); // Expected: "holle" 43 | 44 | String s2 = "leetcode"; 45 | System.out.println("Input: " + s2); 46 | System.out.println("Output: " + rv.reverseVowels(s2)); // Expected: "leotcede" 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /src/Day 26/DefangIPAddress.java: -------------------------------------------------------------------------------- 1 | //https://leetcode.com/problems/defanging-an-ip-address/ 2 | 3 | public class DefangIPAddress { 4 | public String defangIPaddr(String address) { 5 | return address.replace(".", "[.]"); 6 | 7 | } 8 | 9 | public static void main(String[] args) { 10 | DefangIPAddress dp = new DefangIPAddress(); 11 | 12 | // Test cases 13 | String address1 = "1.1.1.1"; 14 | System.out.println("Input: " + address1); 15 | System.out.println("Output: " + dp.defangIPaddr(address1)); // Expected: "1[.]1[.]1[.]1" 16 | 17 | String address2 = "255.100.50.0"; 18 | System.out.println("Input: " + address2); 19 | System.out.println("Output: " + dp.defangIPaddr(address2)); // Expected: "255[.]100[.]50[.]0" 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /src/Day 27/atoi.java: -------------------------------------------------------------------------------- 1 | public class atoi { 2 | public static void main(String[] args) { 3 | 4 | atoi converter = new atoi(); 5 | int result = converter.atoiConverter("btwrbn r@#%^$ fcvrbvgt123 werfvr"); 6 | System.out.println("Result: " + result); 7 | } 8 | 9 | public int atoiConverter(String s) { 10 | int res = 0; 11 | StringBuilder temp = new StringBuilder(); 12 | 13 | for (char ch : s.toCharArray()) { 14 | if (Character.isDigit(ch) || (ch == '-' && temp.length() == 0)) { 15 | temp.append(ch); 16 | } 17 | } 18 | 19 | if (temp.length() > 0) { 20 | res = Integer.parseInt(temp.toString()); 21 | } 22 | 23 | return res; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /src/Day 28/ReverseString.java: -------------------------------------------------------------------------------- 1 | public class ReverseString { 2 | public void reverseString(char[] s) { 3 | int left = 0; 4 | int right = s.length - 1; 5 | 6 | while (left < right) { 7 | // Swap characters at left and right indices 8 | char temp = s[left]; 9 | s[left] = s[right]; 10 | s[right] = temp; 11 | 12 | // Move the pointers inward 13 | left++; 14 | right--; 15 | } 16 | } 17 | 18 | public static void main(String[] args) { 19 | ReverseString solution = new ReverseString(); 20 | 21 | char[] s1 = { 'h', 'e', 'l', 'l', 'o' }; 22 | solution.reverseString(s1); 23 | System.out.println(java.util.Arrays.toString(s1)); // Output: [o, l, l, e, h] 24 | 25 | char[] s2 = { 'H', 'a', 'n', 'n', 'a', 'h' }; 26 | solution.reverseString(s2); 27 | System.out.println(java.util.Arrays.toString(s2)); // Output: [h, a, n, n, a, H] 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/Day 29/ransome.java: -------------------------------------------------------------------------------- 1 | class ransome { 2 | public static boolean canConstruct(String ransomNote, String magazine) { 3 | for (int i = 0; i < ransomNote.length(); i++) { 4 | char currentChar = ransomNote.charAt(i); 5 | boolean found = false; 6 | for (int j = 0; j < magazine.length(); j++) { 7 | if (currentChar == magazine.charAt(j)) { 8 | found = true; 9 | magazine = magazine.substring(0, j) + magazine.substring(j + 1); 10 | break; 11 | } 12 | } 13 | if (!found) { 14 | return false; // Character not found in magazine 15 | } 16 | } 17 | return true; 18 | } 19 | 20 | public static void main(String[] args) { 21 | 22 | String ransomNote1 = "a"; 23 | String magazine1 = "b"; 24 | System.out.println(canConstruct(ransomNote1, magazine1)); // Output: false 25 | 26 | String ransomNote2 = "aa"; 27 | String magazine2 = "ab"; 28 | System.out.println(canConstruct(ransomNote2, magazine2)); // Output: false 29 | 30 | String ransomNote3 = "aa"; 31 | String magazine3 = "aab"; 32 | System.out.println(canConstruct(ransomNote3, magazine3)); 33 | } 34 | } -------------------------------------------------------------------------------- /src/Day 30/GCDOfStrings.java: -------------------------------------------------------------------------------- 1 | public class GCDOfStrings { 2 | public static String gcdOfStrings(String str1, String str2) { 3 | if (!(str1 + str2).equals(str2 + str1)) { 4 | return ""; // If concatenating str1 and str2 doesn't equal str2 and str1, return an empty 5 | // string. 6 | } 7 | 8 | int length1 = str1.length(); 9 | int length2 = str2.length(); 10 | 11 | 12 | int a = length1; 13 | int b = length2; 14 | 15 | // logic 16 | while (b != 0) { 17 | int temp = b; 18 | b = a % b; 19 | a = temp; 20 | } 21 | 22 | 23 | 24 | return str1.substring(0, a); // Return the substring of str1 up to the length of gcdLength. 25 | } 26 | 27 | 28 | public static void main(String[] args) { 29 | String str1 = "ABCABC"; 30 | String str2 = "ABC"; 31 | System.out.println(gcdOfStrings(str1, str2)); // Output: "ABC" 32 | 33 | str1 = "ABABAB"; 34 | str2 = "ABAB"; 35 | System.out.println(gcdOfStrings(str1, str2)); // Output: "AB" 36 | 37 | str1 = "LEET"; 38 | str2 = "CODE"; 39 | System.out.println(gcdOfStrings(str1, str2)); // Output: "" 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /src/Day 31/moveZeros.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class Solution { 4 | public void moveZeroes(int[] nums) { 5 | if (nums == null || nums.length == 0) { 6 | return; 7 | } 8 | 9 | int nonZeroIndex = 0; 10 | 11 | // Iterate through the array 12 | for (int i = 0; i < nums.length; i++) { 13 | // If the current element is non-zero, swap it with the element at nonZeroIndex 14 | if (nums[i] != 0) { 15 | int temp = nums[i]; 16 | nums[i] = nums[nonZeroIndex]; 17 | nums[nonZeroIndex] = temp; 18 | 19 | // Move the nonZeroIndex pointer forward 20 | nonZeroIndex++; 21 | } 22 | } 23 | } 24 | 25 | public static void main(String[] args) { 26 | Solution solution = new Solution(); 27 | 28 | // Example 1 29 | int[] nums1 = { 0, 1, 0, 3, 12 }; 30 | solution.moveZeroes(nums1); 31 | System.out.println(Arrays.toString(nums1)); // Output: [1, 3, 12, 0, 0] 32 | 33 | // Example 2 34 | int[] nums2 = { 0 }; 35 | solution.moveZeroes(nums2); 36 | System.out.println(Arrays.toString(nums2)); // Output: [0] 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /src/Day 32/removeElement.java: -------------------------------------------------------------------------------- 1 | public class removeElement { 2 | public int removeElementFunc(int[] nums, int val) { 3 | int k = 0; // Initialize count of elements not equal to val 4 | for (int i = 0; i < nums.length; i++) { 5 | if (nums[i] != val) { 6 | nums[k++] = nums[i]; // Move elements not equal to val to the front 7 | } 8 | } 9 | return k; // Return the count of elements not equal to val 10 | } 11 | 12 | public static void main(String[] args) { 13 | removeElement solution = new removeElement(); 14 | 15 | // Example 1 16 | int[] nums1 = { 3, 2, 2, 3 }; 17 | int val1 = 3; 18 | System.out.println(solution.removeElementFunc(nums1, val1)); // Output: 2 19 | 20 | // Example 2 21 | int[] nums2 = { 0, 1, 2, 2, 3, 0, 4, 2 }; 22 | int val2 = 2; 23 | System.out.println(solution.removeElementFunc(nums2, val2)); // Output: 5 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /src/Day 33/HappyNumber.java: -------------------------------------------------------------------------------- 1 | // Source: https://leetcode.com/problems/happy-number/ 2 | public class HappyNumber { 3 | 4 | public boolean isHappy(int n) { 5 | if (n == 1 || n == 7) 6 | return true; 7 | else if (n <= 9) 8 | return false; 9 | int pro = 0; 10 | while (n > 0) { 11 | int rem = n % 10; 12 | n /= 10; 13 | pro += (rem * rem); 14 | } 15 | return isHappy(pro); 16 | } 17 | 18 | public static void main(String[] args) { 19 | HappyNumber solution = new HappyNumber(); 20 | 21 | // Example 1 22 | int n1 = 19; 23 | System.out.println(solution.isHappy(n1)); // Output: true 24 | 25 | // Example 2 26 | int n2 = 2; 27 | System.out.println(solution.isHappy(n2)); // Output: false 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/Day 34/ExcelSheetII.java: -------------------------------------------------------------------------------- 1 | // https://leetcode.com/problems/excel-sheet-column-number/ 2 | 3 | public class ExcelSheetII { 4 | 5 | public static int titleToNumber(String columnTitle) { 6 | int result = 0; 7 | 8 | for (int i = 0; i < columnTitle.length(); i++) { 9 | int digit = columnTitle.charAt(i) - 'A' + 1; 10 | result = result * 26 + digit; 11 | } 12 | 13 | return result; 14 | } 15 | 16 | public static void main(String[] args) { 17 | // Example usage: 18 | System.out.println(titleToNumber("A")); // Output: 1 19 | System.out.println(titleToNumber("AB")); // Output: 28 20 | System.out.println(titleToNumber("ZY")); // Output: 701 21 | } 22 | } -------------------------------------------------------------------------------- /src/Day 35/ContainsDuplicate.java: -------------------------------------------------------------------------------- 1 | // https://leetcode.com/problems/contains-duplicate/ 2 | 3 | import java.util.HashSet; 4 | import java.util.Set; 5 | 6 | public class ContainsDuplicate { 7 | 8 | public boolean containsDuplicate(int[] nums) { 9 | Set set = new HashSet<>(); 10 | for (int num : nums) { 11 | if (!set.add(num)) 12 | return true; 13 | } 14 | return false; 15 | } 16 | 17 | public static void main(String[] args) { 18 | ContainsDuplicate solution = new ContainsDuplicate(); 19 | 20 | // Example 1 21 | int[] nums1 = { 1, 2, 3, 1 }; 22 | System.out.println(solution.containsDuplicate(nums1)); // Output: true 23 | 24 | // Example 2 25 | int[] nums2 = { 1, 2, 3, 4 }; 26 | System.out.println(solution.containsDuplicate(nums2)); // Output: false 27 | 28 | // Example 3 29 | int[] nums3 = { 1, 1, 1, 3, 3, 4, 3, 2, 4, 2 }; 30 | System.out.println(solution.containsDuplicate(nums3)); // Output: true 31 | } 32 | } -------------------------------------------------------------------------------- /src/Day 36/IsomorphicStrings.java: -------------------------------------------------------------------------------- 1 | // https://leetcode.com/problems/isomorphic-strings/ 2 | 3 | public class IsomorphicStrings { 4 | 5 | public static boolean isIsomorphic(String s, String t) { 6 | int[] sMap = new int[256]; 7 | int[] tMap = new int[256]; 8 | 9 | for (int i = 0; i < s.length(); i++) { 10 | char sChar = s.charAt(i); 11 | char tChar = t.charAt(i); 12 | 13 | if (sMap[sChar] != tMap[tChar]) { 14 | return false; 15 | } 16 | 17 | sMap[sChar] = i + 1; 18 | tMap[tChar] = i + 1; 19 | } 20 | 21 | return true; 22 | } 23 | 24 | public static void main(String[] args) { 25 | // Example usage: 26 | System.out.println(isIsomorphic("egg", "add")); // Output: true 27 | System.out.println(isIsomorphic("foo", "bar")); // Output: false 28 | System.out.println(isIsomorphic("paper", "title")); // Output: true 29 | } 30 | } -------------------------------------------------------------------------------- /src/Day 37/concentationArray.java: -------------------------------------------------------------------------------- 1 | import java.util.Arrays; 2 | 3 | public class concentationArray { 4 | // very easy 5 | public static int[] getConcatenation(int[] nums) { 6 | int n = nums.length; 7 | int[] ans = new int[2 * n]; 8 | for (int i = 0; i < n; i++) { 9 | ans[i] = nums[i]; 10 | ans[i + n] = nums[i]; 11 | } 12 | return ans; 13 | } 14 | 15 | public static void main(String[] args) { 16 | 17 | // Example 1 18 | int[] nums1 = { 1, 2, 1 }; 19 | System.out.println(Arrays.toString(getConcatenation(nums1))); // Output: [1, 2, 1, 1, 2, 1] 20 | 21 | // Example 2 22 | int[] nums2 = { 1, 3, 2, 1 }; 23 | System.out.println(Arrays.toString(getConcatenation(nums2))); // Output: [1, 3, 2, 1, 1, 3, 2, 1] 24 | } 25 | } -------------------------------------------------------------------------------- /src/Day 38/plusOne.java: -------------------------------------------------------------------------------- 1 | import java.util.Arrays; 2 | 3 | public class plusOne { 4 | public static int[] plusOneFunc(int[] digits) { 5 | int n = digits.length; 6 | for (int i = n - 1; i >= 0; i--) { 7 | if (digits[i] < 9) { 8 | digits[i]++; 9 | return digits; 10 | } 11 | digits[i] = 0; 12 | } 13 | // if all digits are 9 i.e [9,9,9,9,9,9] 14 | int[] newNumber = new int[n + 1]; 15 | newNumber[0] = 1; 16 | return newNumber; 17 | } 18 | 19 | public static void main(String[] args) { 20 | // Example 1 21 | int[] nums1 = { 1, 2, 3 }; 22 | System.out.println(Arrays.toString(plusOneFunc(nums1))); // Output: [1, 2, 4] 23 | 24 | // Example 2 25 | int[] nums2 = { 4, 3, 2, 1 }; 26 | System.out.println(Arrays.toString(plusOneFunc(nums2))); // Output: [4, 3, 2, 2] 27 | 28 | // Example 3 29 | int[] nums3 = { 0 }; 30 | System.out.println(Arrays.toString(plusOneFunc(nums3))); // Output: [1] 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/Day 39/mergeArray.java: -------------------------------------------------------------------------------- 1 | import java.util.Arrays; 2 | 3 | public class mergeArray { 4 | // given two sorted arrays, merge them into a single sorted array 5 | 6 | public static int[] merge(int[] nums1, int m, int[] nums2, int n) { 7 | int j = 0; 8 | for (int i = m; i < nums1.length; i++) { 9 | nums1[i] = nums2[j++]; 10 | } 11 | Arrays.sort(nums1); 12 | 13 | return nums1; 14 | } 15 | 16 | public static void main(String[] args) { 17 | // Example 1 18 | int[] nums1 = { 1, 2, 3, 0, 0, 0 }; 19 | int m = 3; 20 | int[] nums2 = { 2, 5, 6 }; 21 | int n = 3; 22 | System.out.println(Arrays.toString(merge(nums1, m, nums2, n))); // Output: [1, 2, 2, 3, 5, 6] 23 | 24 | // Example 2 25 | int[] nums3 = { 1 }; 26 | int m1 = 1; 27 | int[] nums4 = {}; 28 | int n1 = 0; 29 | System.out.println(Arrays.toString(merge(nums3, m1, nums4, n1))); // Output: [1] 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /src/Day 40/missingNumber.java: -------------------------------------------------------------------------------- 1 | public class missingNumber { 2 | public static int missingNumberCheck(int[] nums) { 3 | int n = nums.length; 4 | int sum = n * (n + 1) / 2; 5 | for (int i = 0; i < n; i++) { 6 | sum -= nums[i]; 7 | } 8 | return sum; 9 | } 10 | 11 | public static void main(String[] args) { 12 | // Example 1 13 | int[] nums1 = { 3, 0, 1 }; 14 | System.out.println(missingNumberCheck(nums1)); // Output: 2 15 | 16 | // Example 2 17 | int[] nums2 = { 0, 1 }; 18 | System.out.println(missingNumberCheck(nums2)); // Output: 2 19 | 20 | // Example 3 21 | int[] nums3 = { 9, 6, 4, 2, 3, 5, 7, 0, 1 }; 22 | System.out.println(missingNumberCheck(nums3)); // Output: 8 23 | 24 | // Example 4 25 | int[] nums4 = { 0 }; 26 | System.out.println(missingNumberCheck(nums4)); // Output: 1 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/Day 41/maximumProduct.java: -------------------------------------------------------------------------------- 1 | import java.util.Arrays; 2 | 3 | public class maximumProduct { 4 | public static int maxProd(int[] nums) { 5 | Arrays.sort(nums); 6 | int n = nums.length; 7 | return Math.max(nums[0] * nums[1] * nums[n - 1], nums[n - 1] * nums[n - 2] * nums[n - 3]); // both negative and 8 | // positive numbers 9 | } 10 | 11 | public static void main(String[] args) { 12 | // Example 1 13 | int[] nums1 = { 1, 2, 3 }; 14 | System.out.println(maxProd(nums1)); // Output: 6 15 | 16 | // Example 2 17 | int[] nums2 = { 1, 2, 3, 4 }; 18 | System.out.println(maxProd(nums2)); // Output: 24 19 | 20 | // Example 3 21 | int[] nums3 = { -1, -2, -3 }; 22 | System.out.println(maxProd(nums3)); // Output: -6 23 | } 24 | } -------------------------------------------------------------------------------- /src/Day 42/findGCD.java: -------------------------------------------------------------------------------- 1 | public class findGCD { 2 | public static int GCD(int[] nums) { 3 | int n = nums.length; 4 | int max = nums[0]; 5 | int min = nums[0]; 6 | for (int i = 0; i < n; i++) { 7 | max = Math.max(max, nums[i]); 8 | min = Math.min(min, nums[i]); 9 | } 10 | return gcd(max, min); 11 | } 12 | 13 | public static int gcd(int a, int b) { 14 | if (b == 0) { 15 | return a; 16 | } 17 | return gcd(b, a % b); 18 | } 19 | 20 | public static void main(String[] args) { 21 | // Example 1 22 | int[] nums1 = { 2, 5, 6, 9, 10 }; 23 | System.out.println(GCD(nums1)); // Output: 1 24 | 25 | // Example 2 26 | int[] nums2 = { 7, 5, 6, 8, 3 }; 27 | System.out.println(GCD(nums2)); // Output: 1 28 | 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /src/Day 43/multiplyStrings.java: -------------------------------------------------------------------------------- 1 | public class multiplyStrings { 2 | public static String multiply(String num1, String num2) { 3 | int n = num1.length(); 4 | int m = num2.length(); 5 | int[] result = new int[n + m]; 6 | for (int i = n - 1; i >= 0; i--) { 7 | for (int j = m - 1; j >= 0; j--) { 8 | int product = (num1.charAt(i) - '0') * (num2.charAt(j) - '0'); 9 | int p1 = i + j; 10 | int p2 = i + j + 1; 11 | int sum = product + result[p2]; 12 | result[p1] += sum / 10; 13 | result[p2] = sum % 10; 14 | } 15 | } 16 | StringBuilder sb = new StringBuilder(); 17 | for (int p : result) { 18 | if (!(sb.length() == 0 && p == 0)) { 19 | sb.append(p); 20 | } 21 | } 22 | return sb.length() == 0 ? "0" : sb.toString(); 23 | } 24 | 25 | public static void main(String[] args) { 26 | // Example 1 27 | String num1 = "2"; 28 | String num2 = "3"; 29 | System.out.println(multiply(num1, num2)); // Output: 6 30 | 31 | // Example 2 32 | String num3 = "123"; 33 | String num4 = "456"; 34 | System.out.println(multiply(num3, num4)); // Output: 56088 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /src/Day 44/countAndSay.java: -------------------------------------------------------------------------------- 1 | public class countAndSay { 2 | public String countAndSayfunc(int n) { 3 | String s = "1"; 4 | for (int i = 2; i <= n; i++) { 5 | s = countAndAdd(s); 6 | } 7 | return s; 8 | 9 | } 10 | 11 | public String countAndAdd(String s) { 12 | StringBuilder str = new StringBuilder(); 13 | char c = s.charAt(0); 14 | int count = 1; 15 | 16 | for (int i = 1; i < s.length(); i++) { 17 | if (s.charAt(i) == c) 18 | count++; 19 | 20 | else { 21 | str.append(count); 22 | str.append(c); 23 | c = s.charAt(i); 24 | count = 1; 25 | } 26 | } 27 | str.append(count); 28 | str.append(c); 29 | 30 | return str.toString(); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/Day 45/majorityElement.java: -------------------------------------------------------------------------------- 1 | public class majorityElement { 2 | 3 | public static int majorityElementFunc(int[] nums) { 4 | int count = 0; 5 | Integer candidate = null; 6 | for (int num : nums) { 7 | if (count == 0) { 8 | candidate = num; 9 | } 10 | count += (num == candidate) ? 1 : -1; 11 | } 12 | return candidate; 13 | } 14 | 15 | public static void main(String[] args) { 16 | // Example 1 17 | int[] nums1 = { 3, 2, 3 }; 18 | System.out.println(majorityElementFunc(nums1)); // Output: 3 19 | 20 | // Example 2 21 | int[] nums2 = { 2, 2, 1, 1, 1, 2, 2 }; 22 | System.out.println(majorityElementFunc(nums2)); // Output: 2 23 | } 24 | } -------------------------------------------------------------------------------- /src/Day 46/rotateMatrix.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public void rotate(int[][] matrix) { 3 | int n = matrix.length; 4 | 5 | // Step 1: Transpose the matrix 6 | for (int i = 0; i < n; i++) { 7 | for (int j = i; j < n; j++) { 8 | int temp = matrix[i][j]; 9 | matrix[i][j] = matrix[j][i]; 10 | matrix[j][i] = temp; 11 | } 12 | } 13 | 14 | // Step 2: Reverse each row 15 | for (int i = 0; i < n; i++) { 16 | for (int j = 0; j < n / 2; j++) { 17 | int temp = matrix[i][j]; 18 | matrix[i][j] = matrix[i][n - 1 - j]; 19 | matrix[i][n - 1 - j] = temp; 20 | } 21 | } 22 | } 23 | 24 | public static void main(String[] args) { 25 | Solution solution = new Solution(); 26 | 27 | // Example 1 28 | int[][] matrix1 = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } }; 29 | solution.rotate(matrix1); 30 | for (int[] row : matrix1) { 31 | for (int num : row) { 32 | System.out.print(num + " "); 33 | } 34 | System.out.println(); 35 | } 36 | 37 | // Example 2 38 | int[][] matrix2 = { { 5, 1, 9, 11 }, { 2, 4, 8, 10 }, { 13, 3, 6, 7 }, { 15, 14, 12, 16 } }; 39 | solution.rotate(matrix2); 40 | for (int[] row : matrix2) { 41 | for (int num : row) { 42 | System.out.print(num + " "); 43 | } 44 | System.out.println(); 45 | } 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /src/Day 46/rotateMatrix.py: -------------------------------------------------------------------------------- 1 | def rotate(matrix): 2 | n = len(matrix) 3 | 4 | # Step 1: Transpose the matrix 5 | for i in range(n): 6 | for j in range(i, n): 7 | matrix[i][j], matrix[j][i] = matrix[j][i], matrix[i][j] 8 | 9 | # Step 2: Reverse each row 10 | for row in matrix: 11 | row.reverse() 12 | 13 | # Example 1 14 | matrix1 = [[1,2,3],[4,5,6],[7,8,9]] 15 | rotate(matrix1) 16 | print(matrix1) # Output: [[7, 4, 1], [8, 5, 2], [9, 6, 3]] 17 | 18 | # Example 2 19 | matrix2 = [[5,1,9,11],[2,4,8,10],[13,3,6,7],[15,14,12,16]] 20 | rotate(matrix2) 21 | print(matrix2) # Output: [[15, 13, 2, 5], [14, 3, 4, 1], [12, 6, 8, 9], [16, 7, 10, 11]] 22 | -------------------------------------------------------------------------------- /src/Day 47/FirstMissingPositive.java: -------------------------------------------------------------------------------- 1 | import java.util.Arrays; 2 | 3 | class Solution { 4 | public int firstMissingPositive(int[] nums) { 5 | Arrays.sort(nums); // Sorting the array 6 | 7 | int n = nums.length; 8 | 9 | // Handling edge cases 10 | if (n == 0 || nums[n - 1] <= 0) { 11 | return 1; 12 | } 13 | 14 | int smallestMissing = 1; 15 | 16 | // Iterate through the sorted array 17 | for (int i = 0; i < n; i++) { 18 | // If the current element is negative or zero, continue to the next element 19 | if (nums[i] <= 0) { 20 | continue; 21 | } 22 | 23 | // If the current element is larger than the smallest missing positive, return 24 | // the smallest missing positive 25 | if (nums[i] > smallestMissing) { 26 | return smallestMissing; 27 | } 28 | 29 | // If the current element is equal to the smallest missing positive, increment 30 | // the smallest missing positive 31 | if (nums[i] == smallestMissing) { 32 | smallestMissing++; 33 | } 34 | } 35 | 36 | return smallestMissing; 37 | } 38 | 39 | public static void main(String[] args) { 40 | Solution solution = new Solution(); 41 | 42 | // Test cases 43 | int[] nums1 = { 1, 2, 0 }; 44 | System.out.println(solution.firstMissingPositive(nums1)); // Output: 3 45 | 46 | int[] nums2 = { 3, 4, -1, 1 }; 47 | System.out.println(solution.firstMissingPositive(nums2)); // Output: 2 48 | 49 | int[] nums3 = { 7, 8, 9, 11, 12 }; 50 | System.out.println(solution.firstMissingPositive(nums3)); // Output: 1 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /src/Day 48/letterCombination.java: -------------------------------------------------------------------------------- 1 | // https://leetcode.com/problems/letter-combinations-of-a-phone-number/ 2 | 3 | import java.util.ArrayList; 4 | import java.util.HashMap; 5 | import java.util.List; 6 | import java.util.Map; 7 | 8 | class Solution { 9 | 10 | public List letterCombinations(String digits) { 11 | List results = new ArrayList<>(); 12 | if (digits == null || digits.isEmpty()) 13 | return results; 14 | 15 | Map map = new HashMap<>(); 16 | map.put('2', "abc"); 17 | map.put('3', "def"); 18 | map.put('4', "ghi"); 19 | map.put('5', "jkl"); 20 | map.put('6', "mno"); 21 | map.put('7', "pqrs"); 22 | map.put('8', "tuv"); 23 | map.put('9', "wxyz"); 24 | 25 | char[] arr = new char[digits.length()]; 26 | buildCombinations(digits, 0, map, arr, results); 27 | return results; 28 | 29 | } 30 | 31 | private void buildCombinations(String digits, int i, Map map, char[] arr, List results) { 32 | if (i == digits.length()) { 33 | results.add(new String(arr)); 34 | return; 35 | } 36 | Character c = digits.charAt(i); 37 | String s = map.get(c); 38 | for (int j = 0; j < s.length(); j++) { 39 | arr[i] = s.charAt(j); 40 | buildCombinations(digits, i + 1, map, arr, results); 41 | } 42 | } 43 | } -------------------------------------------------------------------------------- /src/Day 49/insertPosition.java: -------------------------------------------------------------------------------- 1 | // https://leetcode.com/problems/search-insert-position/ 2 | 3 | import java.util.Arrays; 4 | 5 | public class insertPosition { 6 | public static void main(String[] args) { 7 | int[] nums = { 1, 3, 5, 6 }; 8 | int target = 5; 9 | System.out.println(searchInsert(nums, target)); 10 | } 11 | 12 | public static int searchInsert(int[] nums, int target) { 13 | int index = Arrays.binarySearch(nums, target); 14 | return index < 0 ? -index - 1 : index; 15 | } 16 | 17 | } 18 | -------------------------------------------------------------------------------- /src/Day 50/jumpGame.java: -------------------------------------------------------------------------------- 1 | // https://leetcode.com/problems/jump-game/ 2 | public class jumpGame { 3 | 4 | public static void main(String[] args) { 5 | int[] nums = { 2, 3, 1, 1, 4 }; 6 | System.out.println(canJump(nums)); 7 | } 8 | 9 | public static boolean canJump(int[] nums) { 10 | int max = 0; 11 | for (int i = 0; i < nums.length; i++) { 12 | if (i > max) 13 | return false; 14 | max = Math.max(max, i + nums[i]); 15 | } 16 | return true; 17 | } 18 | } -------------------------------------------------------------------------------- /src/Day 51/colors.java: -------------------------------------------------------------------------------- 1 | // sort colors 2 | 3 | public class colors { 4 | public void sortColors(int[] nums) { 5 | int low = 0, mid = 0, high = nums.length - 1; 6 | while (mid <= high) { 7 | if (nums[mid] == 0) { 8 | swap(nums, low, mid); 9 | low++; 10 | mid++; 11 | } else if (nums[mid] == 1) { 12 | mid++; 13 | } else { 14 | swap(nums, mid, high); 15 | high--; 16 | } 17 | } 18 | } 19 | 20 | private void swap(int[] nums, int i, int j) { 21 | int temp = nums[i]; 22 | nums[i] = nums[j]; 23 | nums[j] = temp; 24 | } 25 | 26 | } -------------------------------------------------------------------------------- /src/Day 52/search2D.java: -------------------------------------------------------------------------------- 1 | public class search2D { 2 | public static void main(String[] args) { 3 | int[][] matrix = { 4 | { 1, 3, 5, 7 }, 5 | { 10, 11, 16, 20 }, 6 | { 23, 30, 34, 60 } 7 | }; 8 | int target = 3; 9 | System.out.println(searchMatrix(matrix, target)); 10 | } 11 | 12 | public static boolean searchMatrix(int[][] matrix, int target) { 13 | // Check for edge cases 14 | if (matrix == null || matrix.length == 0 || matrix[0].length == 0) { 15 | return false; 16 | } 17 | 18 | // Loop through each row 19 | for (int i = 0; i < matrix.length; i++) { 20 | // Loop through each column in the current row 21 | for (int j = 0; j < matrix[i].length; j++) { 22 | // If the current element equals the target, return true 23 | if (matrix[i][j] == target) { 24 | return true; 25 | } 26 | } 27 | } 28 | 29 | // If the target is not found in any element, return false 30 | return false; 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/Day 53/bestStockII.java: -------------------------------------------------------------------------------- 1 | // https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii 2 | 3 | public class bestStockII { 4 | public static void main(String[] args) { 5 | int[] prices = { 7, 1, 5, 3, 6, 4 }; 6 | System.out.println(maxProfit(prices)); 7 | } 8 | 9 | public static int maxProfit(int[] prices) { 10 | int maxProfit = 0; 11 | for (int i = 1; i < prices.length; i++) 12 | if (prices[i] > prices[i - 1]) 13 | maxProfit += prices[i] - prices[i - 1]; 14 | return maxProfit; 15 | } 16 | } -------------------------------------------------------------------------------- /src/Day 54/rabbits.java: -------------------------------------------------------------------------------- 1 | // https://leetcode.com/problems/rabbits-in-forest/ 2 | 3 | class rabbits { 4 | public int numRabbits(int[] answers) { 5 | int[] count = new int[1000]; 6 | int res = 0; 7 | for (int i : answers) { 8 | if (count[i] % (i + 1) == 0) { 9 | res += i + 1; 10 | } 11 | count[i]++; 12 | } 13 | return res; 14 | } 15 | } 16 | 17 | // explaination 18 | // In a forest, each rabbit has some color. Some subset of rabbits (possibly all 19 | // of them) tell you how many other rabbits have the same color as them. Those 20 | // answers are placed in an array. 21 | 22 | // we make a count array of size 1000 and then we iterate through the answers 23 | // array and check if the count of the current answer is divisible by the answer 24 | // + 1 then we add the answer + 1 to the result and then increment the count of 25 | // the current answer. At the end we return the result. 26 | // Time complexity: O(n) -------------------------------------------------------------------------------- /src/Day 55/CardsInDeck.java: -------------------------------------------------------------------------------- 1 | // https://leetcode.com/problems/x-of-a-kind-in-a-deck-of-cards/description/ 2 | 3 | class CardsInDeck { 4 | public boolean hasGroupsSizeX(int[] deck) { 5 | int[] count = new int[10000]; 6 | for (int c : deck) { 7 | count[c]++; 8 | } 9 | int g = -1; 10 | for (int i = 0; i < 10000; i++) { 11 | if (count[i] > 0) { 12 | if (g == -1) { 13 | g = count[i]; 14 | } else { 15 | g = gcd(g, count[i]); 16 | } 17 | } 18 | } 19 | return g >= 2; 20 | } 21 | 22 | public int gcd(int x, int y) { 23 | return x == 0 ? y : gcd(y % x, x); 24 | } 25 | } -------------------------------------------------------------------------------- /src/Day 56/GoodPairs.java: -------------------------------------------------------------------------------- 1 | // https://leetcode.com/problems/number-of-good-pairs/description/ 2 | 3 | class GoodPairs { 4 | public int numIdenticalPairs(int[] nums) { 5 | int[] count = new int[101]; 6 | int res = 0; 7 | for (int i : nums) { 8 | res += count[i]++; 9 | } 10 | return res; 11 | } 12 | } -------------------------------------------------------------------------------- /src/Day 57/XORTriplets.java: -------------------------------------------------------------------------------- 1 | // https://leetcode.com/problems/count-triplets-that-can-form-two-arrays-of-equal-xor/description/ 2 | 3 | public class XORTriplets { 4 | public int countTriplets(int[] arr) { 5 | int n = arr.length; 6 | int[] prefix = new int[n + 1]; 7 | for (int i = 0; i < n; i++) { 8 | prefix[i + 1] = prefix[i] ^ arr[i]; 9 | } 10 | int count = 0; 11 | for (int i = 0; i < n; i++) { 12 | for (int k = i + 1; k < n; k++) { 13 | if (prefix[i] == prefix[k + 1]) { 14 | count += k - i; 15 | } 16 | } 17 | } 18 | return count; 19 | } 20 | } -------------------------------------------------------------------------------- /src/Day 58/Prime.java: -------------------------------------------------------------------------------- 1 | class Prime{ 2 | public static void main(String[] args) { 3 | int n = 10; 4 | int count = 0; 5 | for(int i = 2; i <= n; i++){ 6 | if(n % i == 0){ 7 | count++; 8 | } 9 | } 10 | if(count == 1){ 11 | System.out.println("Prime"); 12 | }else{ 13 | System.out.println("Not Prime"); 14 | } 15 | } 16 | } 17 | 18 | // requested problem -------------------------------------------------------------------------------- /src/Day 59/NicePairs.java: -------------------------------------------------------------------------------- 1 | // https://leetcode.com/problems/count-nice-pairs-in-an-array/description/ 2 | 3 | class NicePairs{ 4 | public int countNicePairs(int[] nums) { 5 | final int MOD = 1000000007; 6 | Map map = new HashMap<>(); 7 | long nicePairs = 0; 8 | 9 | for (int num : nums) { 10 | int revNum = reverse(num); 11 | int diff = num - revNum; 12 | nicePairs += map.getOrDefault(diff, 0); 13 | map.put(diff, map.getOrDefault(diff, 0) + 1); 14 | } 15 | 16 | return (int) (nicePairs % MOD); 17 | } 18 | 19 | private int reverse(int num) { 20 | int rev = 0; 21 | while (num > 0) { 22 | rev = rev * 10 + num % 10; 23 | num /= 10; 24 | } 25 | return rev; 26 | } 27 | } -------------------------------------------------------------------------------- /src/Day 60/BeautyNumber.java: -------------------------------------------------------------------------------- 1 | // https://leetcode.com/problems/find-the-k-beauty-of-a-number/ 2 | public class BeautyNumber { 3 | public int kBeauty(int num, int k) { 4 | // Convert num to string 5 | String numStr = Integer.toString(num); 6 | 7 | // Counter to keep track of k-beauty substrings 8 | int count = 0; 9 | 10 | // Iterate through all substrings of length k 11 | for (int i = 0; i <= numStr.length() - k; i++) { 12 | String substr = numStr.substring(i, i + k); 13 | 14 | // Convert substring back to integer 15 | int subNum = Integer.parseInt(substr); 16 | 17 | // Check if subNum is a divisor of num and not 0 18 | if (subNum != 0 && num % subNum == 0) { 19 | count++; 20 | } 21 | } 22 | 23 | return count; 24 | } 25 | 26 | public static void main(String[] args) { 27 | Solution solution = new Solution(); 28 | System.out.println(solution.kBeauty(240, 2)); // Output: 2 29 | System.out.println(solution.kBeauty(430043, 2)); // Output: 2 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /src/Day 61/DaysSpentTogether.java: -------------------------------------------------------------------------------- 1 | //https://leetcode.com/problems/count-days-spent-together/submissions/1184218999/ 2 | 3 | public class DaysSpentTogether { 4 | public int daysTogether(String arriveAlice, String leaveAlice, String arriveBob, String leaveBob) { 5 | // Parse dates 6 | int startAlice = toDayOfYear(arriveAlice); 7 | int endAlice = toDayOfYear(leaveAlice); 8 | int startBob = toDayOfYear(arriveBob); 9 | int endBob = toDayOfYear(leaveBob); 10 | 11 | // Counter for days together 12 | int daysTogether = 0; 13 | 14 | // Iterate through each day of the year 15 | for (int day = 1; day <= 365; day++) { 16 | // Check if the day falls within both Alice and Bob's stay 17 | if (day >= startAlice && day <= endAlice && day >= startBob && day <= endBob) { 18 | daysTogether++; 19 | } 20 | } 21 | 22 | return daysTogether; 23 | } 24 | 25 | // Helper function to convert date string to day of year 26 | private int toDayOfYear(String date) { 27 | String[] parts = date.split("-"); 28 | int month = Integer.parseInt(parts[0]); 29 | int day = Integer.parseInt(parts[1]); 30 | 31 | int[] daysPerMonth = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; 32 | 33 | int dayOfYear = 0; 34 | for (int i = 0; i < month - 1; i++) { 35 | dayOfYear += daysPerMonth[i]; 36 | } 37 | dayOfYear += day; 38 | 39 | return dayOfYear; 40 | } 41 | 42 | public static void main(String[] args) { 43 | Solution solution = new Solution(); 44 | System.out.println(solution.daysTogether("08-15", "08-18", "08-16", "08-19")); // Output: 3 45 | System.out.println(solution.daysTogether("10-01", "10-31", "11-01", "12-31")); // Output: 0 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /src/Day 62/Fraction.java: -------------------------------------------------------------------------------- 1 | // https://leetcode.com/problems/simplified-fractions/description/ 2 | 3 | import java.util.*; 4 | 5 | public class Fraction { 6 | public List simplifiedFractions(int n) { 7 | List result = new ArrayList<>(); 8 | 9 | for (int denominator = 2; denominator <= n; denominator++) { 10 | for (int numerator = 1; numerator < denominator; numerator++) { 11 | if (gcd(numerator, denominator) == 1) { 12 | result.add(numerator + "/" + denominator); 13 | } 14 | } 15 | } 16 | 17 | return result; 18 | } 19 | 20 | // Function to calculate the greatest common divisor using Euclid's algorithm 21 | private int gcd(int a, int b) { 22 | while (b != 0) { 23 | int temp = b; 24 | b = a % b; 25 | a = temp; 26 | } 27 | return a; 28 | } 29 | 30 | public static void main(String[] args) { 31 | Solution solution = new Solution(); 32 | System.out.println(solution.simplifiedFractions(2)); // Output: ["1/2"] 33 | System.out.println(solution.simplifiedFractions(3)); // Output: ["1/2","1/3","2/3"] 34 | System.out.println(solution.simplifiedFractions(4)); // Output: ["1/2","1/3","1/4","2/3","3/4"] 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /src/Day 63/NumberOfDays.java: -------------------------------------------------------------------------------- 1 | //https://leetcode.com/problems/day-of-the-year/description/ 2 | 3 | public class NumberOfDays { 4 | public int dayOfYear(String date) { 5 | int year = Integer.parseInt(date.substring(0, 4)); 6 | int month = Integer.parseInt(date.substring(5, 7)); 7 | int day = Integer.parseInt(date.substring(8)); 8 | 9 | int[] daysInMonth = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; 10 | 11 | // Account for leap years 12 | if (isLeapYear(year)) 13 | daysInMonth[2] = 29; 14 | 15 | int dayOfYear = day; 16 | for (int i = 1; i < month; i++) { 17 | dayOfYear += daysInMonth[i]; 18 | } 19 | 20 | return dayOfYear; 21 | } 22 | 23 | // Function to check if a year is a leap year 24 | private boolean isLeapYear(int year) { 25 | return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0); 26 | } 27 | 28 | public static void main(String[] args) { 29 | Solution solution = new Solution(); 30 | System.out.println(solution.dayOfYear("2019-01-09")); // Output: 9 31 | System.out.println(solution.dayOfYear("2019-02-10")); // Output: 41 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/Extras/foldermaker.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | setlocal enabledelayedexpansion 3 | 4 | REM THIS CODE IS USED TO MAKE FOLDERS IN BULK 5 | REM DO NOT RUN UNLESS YOU KNOW WHAT YOU ARE DOING, FOR ADMIN USE ONLY. 6 | 7 | 8 | REM Set the number of days you want folders for INCRESE AS REQUIRED 9 | set /A "num_days=70" 10 | 11 | REM Loop to create folders 12 | for /L %%i in (1,1,%num_days%) do ( 13 | set "day=%%i" 14 | set "day=0!day!" 15 | set "day=!day:~-2!" 16 | mkdir "Day !day!" 17 | ) 18 | 19 | echo Folders created successfully. 20 | -------------------------------------------------------------------------------- /src/PROBLEMS.md: -------------------------------------------------------------------------------- 1 | ### These are the problems that already have been solved; 2 | 3 | ``` 4 | > Day 01: Roman Number to Integer // add short description of the problem if needed 5 | > Day 02: Check Palindrome Number 6 | > Day 03: Find Length of the last word 7 | > Day 04: Best Time to Buy a Stock 8 | > Day 05: Integer to Roman Numeral 9 | > Day 06: Needle in a Haystack String Problem 10 | > Day 07: Excel Sheet Number to Letter 11 | > Day 08: Reverse Number II 12 | > Day 09: Single Number Check 13 | > Day 10: Ugly Number 14 | > Day 11: Word Pattern 15 | > Day 12: Valid Parenthesis 16 | > Day 13: Two Sum 17 | > Day 14: Common Prefix 18 | > Day 15: Contains Duplicate 19 | > Day 16: 20 | > Day 17: 21 | > Day 18: 22 | > Day 19: 23 | > Day 20: 24 | > Day 21: 25 | 26 | // ADD MORE DAYS AND PROBLEMS AS NEEDED 27 | ``` --------------------------------------------------------------------------------