Day 29 of my #100DaysOfCode challenge 🚀 Today I worked on a Python program to find the maximum occurring character in a string. The goal was to count the frequency of each character and determine which one appears the most. What the program does: • Takes a string as input • Counts the frequency of each character using a dictionary • Tracks the character with the highest occurrence • Handles edge cases like an empty string • Returns the most frequent character How the logic works: 1)A dictionary char_counts is used to store character frequencies 2)The program iterates through each character in the string 3)For every character, its count is updated using .get(char, 0) + 1 4)After counting, another loop checks which character has the highest frequency 5)The character with the maximum count is stored and returned 6)If the string is empty, the function returns None Example: Input: "hello world" Output: Maximum occurring character → l Input: "program" Output: Maximum occurring character → r Why this approach works well: – Uses dictionary for fast lookups – Time Complexity: O(n) – Efficient way to track frequencies Key learnings from Day 29: – Using dictionaries for frequency counting – Iterating through strings efficiently – Handling edge cases in programs – Strengthening string manipulation logic #100DaysOfCode #Day29 #Python #PythonProgramming #StringManipulation #Algorithms #ProblemSolving #CodingPractice #DataStructures #InterviewPrep #LearnByDoing #ComputerScience #BTech #CSE #AIandML #VITBhopal #TechJourney
Python Program to Find Most Frequent Character in a String
More Relevant Posts
-
Day 31 of my #100DaysOfCode challenge 🚀 Today I worked on a Python program to remove all vowels from a given string. The goal was to practice string traversal and filtering characters based on conditions. What the program does: • Takes a string as input • Checks each character in the string • Removes vowels (a, e, i, o, u) in both lowercase and uppercase • Builds a new string without vowels • Returns the final filtered string How the logic works: 1)A string vowels is defined containing all vowel characters 2)An empty string result_string is created to store the result 3)The program loops through each character in the input string 4)If the character is not a vowel, it is added to result_string 5)The process continues until all characters are checked 6)Finally, the filtered string is returned Example: Input: "Hello World" Output:"Hll Wrld" Another example: Input: "AEIOUaeiou" Output: "" (All characters are vowels) Why this approach works well: – Simple and easy to understand logic – Efficient single-pass traversal – Time Complexity: O(n) Key learnings from Day 31: – Iterating through strings – Filtering characters based on conditions – Working with string concatenation – Strengthening string manipulation fundamentals #100DaysOfCode #Day31 #Python #PythonProgramming #StringManipulation #ProblemSolving #CodingPractice #Algorithms #LearnByDoing #ComputerScience #ProgrammingJourney #DeveloperGrowth #BTech #CSE #AIandML #VITBhopal #TechJourney
To view or add a comment, sign in
-
-
Day 30 of my #100DaysOfCode challenge 🚀 Today I worked on a Python program to check whether two strings are rotations of each other. The idea is that if one string is a rotation of another, it will appear as a substring of the original string concatenated with itself. What the program does: • Takes two strings as input • Checks if both strings have the same length • Concatenates the first string with itself • Verifies if the second string exists inside the concatenated string • Returns True if they are rotations, otherwise False How the logic works: 1)First, the program checks if both strings have equal length 2)If lengths differ, they cannot be rotations 3)The first string is concatenated with itself: temp = string1 + string1 4)If string2 appears as a substring inside temp, then it is a valid rotation 5)The function returns the result (True or False) Example: String 1:"abcde" String 2:"cdeab" Output: True (because "cdeab" is a rotation of "abcde") Another example: String 1:"abc" String 2:"acb" Output:False Why this approach works well: – Uses string concatenation trick – Avoids complex rotation checks – Time Complexity: O(n) Key learnings from Day 30: – Understanding string rotation logic – Using substring checks effectively – Solving problems with simple mathematical insights – Strengthening string manipulation skills #100DaysOfCode #Day30 #Python #PythonProgramming #StringAlgorithms #ProblemSolving #CodingPractice #DataStructures #Algorithms #InterviewPrep #LearnByDoing #ComputerScience #BTech #CSE #AIandML #VITBhopal #TechJourney
To view or add a comment, sign in
-
-
Day 49 of my #100DaysOfCode challenge 🚀 Today I worked on a Python program to find the Equilibrium Index of an array. An equilibrium index is an index where the sum of elements on the left is equal to the sum of elements on the right. What the program does: • Takes an array as input • Finds an index where left sum = right sum • Returns the index if found • Returns -1 if no such index exists How the logic works: • Calculate the total sum of the array • Initialize left_sum = 0 • Traverse the array using enumerate() • For each element: – Right sum = total_sum - left_sum - current element – If left_sum == right_sum, return the index • Add the current element to left_sum • If no equilibrium index is found, return -1 Example: Input: [-7, 1, 5, 2, -4, 3, 0] Output: 3(Left sum = Right sum = 1) Another example: Input: [1, 2, 3] Output: -1 (No equilibrium index) Another example: Input: [1, 0, -1] Output: 1 Why this approach is efficient: – Uses prefix sum concept – Avoids nested loops – Time Complexity: O(n) Key learnings from Day 49: – Understanding prefix sums – Optimizing from brute force to O(n) – Working with running totals – Strengthening array problem-solving #100DaysOfCode #Day49 #Python #PythonProgramming #Arrays #PrefixSum #Algorithms #DataStructures #ProblemSolving #CodingPractice #InterviewPrep #LearnByDoing #ProgrammingJourney #DeveloperGrowth #BTech #CSE #AIandML #VITBhopal #TechJourney
To view or add a comment, sign in
-
-
Day 42 of my #100DaysOfCode challenge 🚀 Today I worked on a Python program to find the K-th largest element in an array. This is a common problem in coding interviews and helps build understanding of sorting and indexing. What the program does: • Takes an array and a value k as input • Sorts the array in descending order • Returns the k-th largest element • Handles duplicate values correctly How the logic works: • The array is sorted in descending order using sorted() • Since Python uses 0-based indexing,the k-th largest element is at index k-1 • The element at index k-1 is returned as the result Example: Input: Array: [3, 2, 1, 5, 6, 4] k = 2 Output: 5 (2nd largest element) Another example: Input: Array: [3, 2, 3, 1, 2, 4, 5, 5, 6] k = 4 Output: 4 Why this approach works: – Simple and easy to implement – Uses built-in sorting for reliability – Time Complexity: O(n log n) Key learnings from Day 42: – Understanding sorting in descending order – Working with indexing (k-1) – Solving common interview problems – Handling duplicates in arrays #100DaysOfCode #Day42 #Python #PythonProgramming #Arrays #Sorting #Algorithms #DataStructures #ProblemSolving #CodingPractice #InterviewPrep #LearnByDoing #ProgrammingJourney #DeveloperGrowth #BTech #CSE #AIandML #VITBhopal #TechJourney
To view or add a comment, sign in
-
-
🚀 Day 52 of My Python & DSA Journey Today I revisited Complement of Base 10 Integer on LeetCode — but this time with a more optimized bit manipulation approach! 🔍 Problem Overview: Find the complement of a number by flipping all bits in its binary form. 🧠 Optimized Approach: Instead of converting to binary string, I used bitwise operations: • Create a mask with all bits set to 1 (same length as the number) • Use XOR (^) to flip the bits directly • Special case: when n = 0, return 1 👉 Formula used: (mask - 1) ^ n ⚡ Why this is better: • Avoids string conversion • Works directly on bits (more efficient) • Cleaner and more optimal solution 📊 Complexity: • Time Complexity: O(log n) • Space Complexity: O(1) 💡 Key Learnings: • Deepened understanding of bitwise operators • Learned how to build bit masks dynamically • Understood difference between basic vs optimized solutions Under the Guidance of : Rudra Sravan kumar and Manoj Kumar Reddy Parlapalli #Day52 #Python #LeetCode #BitManipulation #DataStructures #Algorithms #CodingJourney #100DaysOfCode 🚀
To view or add a comment, sign in
-
-
Day 43 of my #100DaysOfCode challenge 🚀 Today I worked on a Python program to find the Longest Common Prefix (LCP) among a list of strings. This is a popular problem that helps strengthen string manipulation and comparison logic. What the program does: • Takes a list of strings as input • Finds the common starting characters shared by all strings • Returns the longest common prefix • Handles edge cases like empty lists and single strings How the logic works: • If the list is empty, return an empty string • Sort the list of strings • Compare only the first and last strings (they will have the maximum difference) • Iterate character by character • Add matching characters to the prefix • Stop when characters don’t match • Return the final prefix Example: Input: ["flower", "flow", "flight"] Output: "fl" Another example: Input: ["dog", "racecar", "car"] Output: "" (No common prefix) Another example: Input: ["apple", "apricot", "april"] Output: "ap" Why this approach works well: – Sorting reduces comparisons to just two strings – Efficient and easy to implement – Time Complexity: O(n log n + m) Key learnings from Day 43: – String comparison techniques – Using sorting to simplify problems – Handling edge cases effectively – Writing optimized and clean logic #100DaysOfCode #Day43 #Python #PythonProgramming #Strings #Algorithms #ProblemSolving #CodingPractice #DataStructures #InterviewPrep #LearnByDoing #DeveloperGrowth #ProgrammingJourney #ComputerScience #BTech #CSE #AIandML #VITBhopal #TechJourney
To view or add a comment, sign in
-
-
Day 6 of my Python learning journey: Focusing on writing code that's not just functional, but also dependable. Highlights from today: - Warmed up by practicing iterators and generators. - Solved the "First Non-Repeating Character" problem using a clean two-pass frequency approach with O(n) complexity. - Merged two sorted arrays efficiently through the two-pointer technique (O(n + m))—avoiding any redundant sorting. - Developed a FastAPI endpoint for the above problem, complete with schema validation. Observations: Initially, I attempted a one-pass solution for the non-repeating character problem, but it ended up feeling convoluted. Switching to a two-pass strategy made the solution much more straightforward and maintainable. Current focus areas: - Maintaining input order integrity. - Performing input validation early on. - Keeping logic as simple as possible. - Thoroughly testing edge cases beforehand. Major takeaway: Define inputs/outputs upfront → validate data early → then prioritize optimization. Starting to grasp how even minor design decisions can significantly impact code quality. GitHub link: https://lnkd.in/gGPw8_js #Python #FastAPI #ProblemSolving #SoftwareEngineering #CleanCode #LearningInPublic #OOP #Testing
To view or add a comment, sign in
-
-
Day 47 of my #100DaysOfCode challenge 🚀 Today I worked on a Python program to find a peak element in an array. A peak element is an element that is greater than or equal to its neighbors. What the program does: • Takes an array as input • Finds any peak element in the array • Handles edge cases (first and last elements) • Returns the peak value How the logic works: • If the array is empty → return None • If it has only one element → return that element • Check the first element: – If it is greater than or equal to the next element → it's a peak • Check the last element: – If it is greater than or equal to the previous element → it's a peak • Traverse the array from index 1 to n-2: – If an element is greater than or equal to both neighbors → return it • If no peak is found (rare case), return None Example: Input: [1, 3, 20, 4, 1, 0] Output: 20 Another example: Input: [1, 2, 3, 4, 5] Output: 5 Another example: Input: [5, 4, 3, 2, 1] Output: 5 Why this approach works: – Checks boundary conditions properly – Works for increasing and decreasing arrays – Time Complexity: O(n) Key learnings from Day 47: – Handling edge cases in arrays – Comparing neighboring elements – Writing clean traversal logic – Strengthening problem-solving skills #100DaysOfCode #Day47 #Python #PythonProgramming #Arrays #Algorithms #ProblemSolving #CodingPractice #DataStructures #InterviewPrep #LearnByDoing #DeveloperGrowth #ProgrammingJourney #ComputerScience #BTech #CSE #AIandML #VITBhopal #TechJourney
To view or add a comment, sign in
-
-
🚀 #100DaysOfCode – Day 52 📌 Problem: Sort Colors (LeetCode 75) 💡 Problem Idea: You are given an array containing only 0s, 1s, and 2s, representing three colors. The task is to sort the array in-place so that all 0s come first, then 1s, and then 2s. ⚡ Key Insight: Instead of using a sorting algorithm, we can solve this in one pass using three pointers. We maintain three regions: Left pointer → position for next 0 Right pointer → position for next 2 Current pointer (i) → traverses the array 🎯 Approach (Dutch National Flag Algorithm): If element is 0 → swap with left, move both left and i If element is 1 → just move i If element is 2 → swap with right, decrease right 📈 Complexity: Time Complexity: O(n) Space Complexity: O(1) (in-place sorting) ✅ Efficient because the array is processed only once. 💭 Learning: This problem is a great example of how pointer techniques can optimize sorting problems without using built-in sort functions. #DSA #LeetCode #Python #CodingJourney #ProblemSolving #Algorithms #100DaysOfCode #LinkedInLearning
To view or add a comment, sign in
-
-
🚀 Day 51 of My Python Journey Today I solved Apply Discount to Prices on LeetCode. 🔍 Problem Overview: The problem gives a sentence containing words and prices (values starting with $). The task is to apply a given discount percentage to all valid prices and return the updated sentence. The updated prices must be formatted with exactly two decimal places. 🧠 Approach: • Split the sentence into individual words. • Check whether a word represents a valid price (starts with $ and the remaining characters are digits). • Convert the price to an integer and apply the discount formula. • Format the discounted price to two decimal places and rebuild the sentence. ⚡ Key Learnings: • Practiced string parsing and validation • Improved understanding of formatting numbers in Python • Strengthened problem-solving using conditional logic 📊 Complexity: • Time Complexity: O(n) • Space Complexity: O(n) Under the Guidance of : Rudra Sravan kumar and Manoj Kumar Reddy Parlapalli #Day51 #Python #LeetCode #DataStructures #Algorithms #CodingJourney #ProblemSolving #100DaysOfCode
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