🚀Day 94/100 #100DaysOfLeetCode 🧩Problem: Reverse Words in a String III✅ 💻Language: Java 🔍Approach: 🔹Split the given string by spaces to separate each word. 🔹For every word, reverse it using a StringBuilder. 🔹Append each reversed word back to the result string, adding spaces between them. 🔹Finally, return the complete reversed word string. 🧠Key Takeaways: 🔹Practiced efficient string manipulation using StringBuilder. 🔹Learned how splitting and reversing operations can be optimized for clarity. 🔹Strengthened understanding of text processing in Java. ⚡Performance: ⏱️Runtime: 4 ms (Beats 86.86%) 💾Memory: 45.49 MB (Beats 47.42%) #100DaysOfLeetCode #Java #CodingJourney #LeetCode #CodingChallenge #ProblemSolving
Reversing Words in a String III with Java
More Relevant Posts
-
🚀Day 95/100 #100DaysOfLeetCode 🔍Problem: Length of Last Word✅ 💻Language: Java 🧠Approach: 1️⃣ First, trim the string to remove trailing and leading spaces. 2️⃣ Initialize a counter to 0. 3️⃣ Traverse the string from the end. 4️⃣ Count characters until the first space is encountered. 5️⃣ Return the count as the length of the last word. 💡Key Takeaways: 🔹Trimming the input ensures no trailing spaces interfere. 🔹Backward traversal avoids splitting the entire string. 🔹Simple yet efficient one-pass solution. ⚡Performance: ⏱️Runtime: 0 ms (Beats 100.00%) 💾Memory: 41.58 MB (Beats 91.52%) #100DaysOfLeetCode #Java #CodingJourney #ProblemSolving #LeetCode #CodingChallenge
To view or add a comment, sign in
-
-
Week 8 || Day 2💡 Reversing words in Java — step by step! Today I practiced reversing each word in a sentence using two different approaches: 🔹 Approach 1 — With .reverse() method: Split the sentence using split(" ") to separate words. Used StringBuffer for each word and applied .reverse() directly. Joined the reversed words back with spaces. 🔹 Approach 2 — Without using .reverse(): Again split the string into words. For each word, used a for loop running from the last character to the first. Appended each character manually into a new StringBuffer. Combined the reversed words carefully, avoiding extra spaces.⚡ #Java #StringBuffer #ProgrammingLogic #JavaFullStack
To view or add a comment, sign in
-
Extract a character from a string To get a character from a string in Java, you can use the charAt() method of the String class. This method takes an index as an argument and returns the character at that position (0-based index). Here's a simple Java program to demonstrate this: Code : public class GetCharacterFromString { public static void main(String[] args) { String str = "Hello, World!"; int index = 7; // Index of the character to retrieve (0-based) // Get the character at the specified index char ch = str.charAt(index); // Print the character System.out.println("Character at index " + index + " is: " + ch); } } Can anyone guess the output ?. #JavaProgramming #JavaCode #Coding #ProgrammingTips #LearnJava #JavaDevelopment #CodeSnippet #SoftwareDevelopment #TechTips #100DaysOfCode
To view or add a comment, sign in
-
✨ Ever wondered what each part of `public static void main(String[] args)` means in Java? ✨ Check out this colorful visual breakdown! 🎨👇 🟡 **public** – Access specifier: Makes the method accessible from anywhere. 🟠 **static** – Keyword: Allows JVM to call main() without creating an object. 🟢 **void** – Return type: Specifies that main() doesn’t return any value. 🔵 **main** – Identifier: Predefined name recognized by the JVM as the entry point. 🟣 **String[] args** – Parameter: Accepts command-line arguments for the Java program. 📝 The code inside `{}` is the function body where your program execution starts! The `main()` method is the heart of every Java application. Understanding it is your first step to writing powerful Java programs! 🚀 #Java #Programming #Learning #CodeNewbie #MainMethod #JavaBasics Anand Kumar Buddarapu sir Saketh Kallepu sir
To view or add a comment, sign in
-
-
Problem Statement: Need to find highest length word in a sentence. Code: String s1 = "I am learning stream API in java"; String[] str = s1.split(" "); String res = Stream.of(s1.split(" " ) .max(Comparator.comparing(String::length)).get(); System.out.println(res);
To view or add a comment, sign in
-
#Day-66) LeetCode 474. Ones and Zeroes – Java DP Solution Just tackled a classic knapsack-style problem using 2D Dynamic Programming in Java. 🧩 Problem: Given a list of binary strings and limits on the number of 0s (m) and 1s (n), find the largest subset such that the total number of 0s and 1s stays within bounds. 🧠 Java Approach: Count 0s and 1s for each string Use a dp[m+1][n+1] table to track the max subset size Iterate backwards to preserve previous states and avoid reuse 💡 Why It Works: This mirrors the 0/1 knapsack pattern — each string is like an item with a cost (0s and 1s) and a value (1). Reverse iteration ensures we don’t double-count. #Java #DynamicProgramming #LeetCode #CodingChallenge #TechPrep #ProblemSolving #PranshuCodes #LinkedInLearning 😊
To view or add a comment, sign in
-
-
🧠 Daily LeetCode Grind — Java Edition Today’s challenge: ✅ Integer to Roman (#12 - Medium) 📌 Goal: Convert an integer to its equivalent Roman numeral representation using standard Roman numeral rules and subtractive notation. 📌 Approach: 🔹 Use two arrays — one for values and one for their corresponding Roman symbols. 🔹 Iterate from the largest value to the smallest, appending the corresponding Roman symbol while subtracting its value from the number. 🔹 Continue until the entire number is converted. 🔹 Handles subtractive cases like IV (4), IX (9), XL (40), XC (90), CD (400), and CM (900). 🧩 Test Case: Input: num = 3749 Output: "MMMDCCXLIX" 💡 Key Takeaways: 🔹 Strengthened understanding of greedy algorithms. 🔹 Learned efficient symbol mapping and iteration logic. 🔹 Improved clarity on how Roman numeral rules apply in algorithmic form. 💻 Language: Java 🧠 Complexity: O(1) — fixed number of Roman symbols and operations. #LeetCode #Java #CodingPractice #ProblemSolving #GreedyAlgorithm #DSA #DeveloperLife #CybernautEdTech #AcceptedSolution
To view or add a comment, sign in
-
-
Day 91 of #100DaysOfCode Solved Base 7 in Java 🔢 Approach The challenge was to convert a given integer (num) to its base 7 string representation. Conversion Method The core of the solution lies in the standard algorithm for base conversion: repeated division and remainder collection. Handle Zero and Negatives: If the input num is 0, the result is immediately "0". I determined if the number is negative and stored this in a boolean flag, then proceeded with the absolute value of the number (num = Math.abs(num)) for the conversion logic. Conversion Loop: I used a while loop that continues as long as num > 0. In each iteration: The remainder when num is divided by 7 (num % 7) gives the next digit in base 7. This digit is appended to a StringBuilder. num is then updated by integer division by 7 (num /= 7). Final Result: Since the remainders are collected in reverse order, I called sb.reverse(). If the original number was negative, I prepended a hyphen (-) to the reversed string. Finally, I returned the result as a string. This simple and efficient implementation had a very fast runtime, beating 77.39% of submissions. #Java #100DaysOfCode #LeetCode #CodingChallenge #Algorithms #BaseConversion #ProblemSolving
To view or add a comment, sign in
-
-
Ever wonder about the difference between final, finally, and finalize? Or why Strings are immutable? 🤔 I found a fantastic breakdown of key Java concepts with clear answers. Knowledge is power—start sharpening your skills today! Follow⏩ and fly✈️ #JavaProgramming #TechSkills #CodingInterview #JavaLearning #ProgrammingTips #JDKvsJRE
To view or add a comment, sign in
-
(Largest Perimeter Triangle — Java 🔺) Hey everyone 👋 Today I explored how to find the largest perimeter triangle from a given array of side lengths using Java. While solving this, I understood how a simple mathematical condition can help in optimizing the entire logic 🔥 Here’s the breakdown 👇 🧠 Concept: To form a valid triangle from 3 sides, the sum of any two sides must be greater than the third one. → a + b > c → a + c > b → b + c > a 💡 Approach 1: Sort the array in descending order and check triplets from the start. If any 3 consecutive sides satisfy the triangle condition, their sum gives the largest perimeter. ⚡ Approach 2 (Optimized): Instead of reversing the array, just sort it in ascending order and iterate from the end — gives the same result but with cleaner logic and less work. 🧩 Time Complexity: O(N log N) 💾 Space Complexity: O(1) This problem helped me understand how sorting order and traversal direction can make an algorithm more elegant and efficient 🚀 📁 Code is available on my GitHub repo: https://lnkd.in/ekjD9s22 #Java #DSA #ProblemSolving #Arrays #CodingJourney #Developers #LearningByDoing #FullStackDeveloper #MCA #GitHub
To view or add a comment, sign in
Explore content categories
- Career
- Productivity
- Finance
- Soft Skills & Emotional Intelligence
- Project Management
- Education
- Technology
- Leadership
- Ecommerce
- User Experience
- Recruitment & HR
- Customer Experience
- Real Estate
- Marketing
- Sales
- Retail & Merchandising
- Science
- Supply Chain Management
- Future Of Work
- Consulting
- Writing
- Economics
- Artificial Intelligence
- Employee Experience
- Workplace Trends
- Fundraising
- Networking
- Corporate Social Responsibility
- Negotiation
- Communication
- Engineering
- Hospitality & Tourism
- Business Strategy
- Change Management
- Organizational Culture
- Design
- Innovation
- Event Planning
- Training & Development