diff --git a/28-10-2024/Explanation.md b/28-10-2024/Explanation.md new file mode 100644 index 0000000..452f498 --- /dev/null +++ b/28-10-2024/Explanation.md @@ -0,0 +1,15 @@ +### Explanation + +**Why did the keyboard get kicked out of the typing club?** +Because it kept _shifting_ between rows! 😂 + +In this code: + +1. **Input Collection:** We first take the number of words and store each word in `word_list`. +2. **Function Definition:** `word_row_type` checks if each word can be typed on a single row by: + - Converting the word to uppercase. + - Using a list of keyboard rows to check if all letters of the word are contained within any one row. +3. **Filtering Typable Words:** Using list comprehension, we select words that meet the typing criteria. +4. **Output:** The resulting list of row-typable words is printed. + +This solution uses list comprehension and built-in functions for an efficient and beginner-friendly approach to the problem. diff --git a/28-10-2024/Solution.py b/28-10-2024/Solution.py new file mode 100644 index 0000000..f0c4e25 --- /dev/null +++ b/28-10-2024/Solution.py @@ -0,0 +1,29 @@ +# Taking Input +N = int(input("Enter Number of Words: ")) + +# List to Store Words +word_list = [] + +# Loop to collect words +for i in range(N): + word = input(f"Enter Word {i+1}: ") + word_list.append(word) + + +def word_row_type(w): + # Convert the word to uppercase to match keyboard row format + w = w.upper() + + # Rows on American keyboard + rows = ["QWERTPOIUY", "ASDFLKJGH", "ZXCVMNB"] + + # Check if all characters of the word can be typed using only one row + if any(all(char in row for char in w) for row in rows): + return 1 # Return 1 if the word can be typed on a single row + + +# List of words that can be typed from a single row +word_typable = [w for w in word_list if (word_row_type(w) == 1)] + +print("Words that can be typed from One Row Of American Keyboard are:") +print(word_typable) diff --git a/29-10-2024/Question.md b/29-10-2024/Question.md new file mode 100644 index 0000000..aedf373 --- /dev/null +++ b/29-10-2024/Question.md @@ -0,0 +1,40 @@ +### **Question: Unique Element** + +**Difficulty Level:** 🟡 Intermediate +**Domain:** Algorithms and Data Structures (Binary Search) + +### **Objective:** + +Given a sorted array where every element appears exactly twice except for one unique element that appears only once, identify this unique element. The challenge requires achieving this in **O(log n)** time and **O(1)** space. + +### **Conceptual Background:** + +The array is sorted and every element except one appears in pairs, allowing the unique element to be located efficiently with a binary search approach. Since binary search works well with sorted data and can achieve logarithmic time complexity, it's a suitable technique here. + +### **Steps to Approach:** + +1. **Binary Search Setup:** + Initialize two pointers, `left` and `right`, representing the bounds of the search range. + +2. **Binary Search to Locate Unique Element:** + Perform binary search: + + - Calculate the midpoint `mid`. + - Determine if the midpoint index is even or odd. + - If `nums[mid]` equals `nums[mid + 1]` or `nums[mid - 1]`, the unique element is located further to the right or left respectively. + +3. **Narrowing Down the Search:** + Adjust `left` and `right` pointers based on comparisons to ensure you converge towards the unique element efficiently. This process will conclude with the unique element at the `left` pointer once the range is narrowed down. + +4. **Output the Unique Element:** + Once found, the element at the `left` pointer is the unique element in the array. + +### **Example Execution:** + +- **Input:** `nums = [3,3,7,7,10,11,11]` +- **Processing:** + - Binary search will adjust `left` and `right` pointers based on the pairing position. + - As the binary search narrows down, it isolates the unique element. +- **Output:** `10` + +This approach, leveraging binary search, ensures the solution runs in optimal time and space complexity.