-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added 29-10-2024 Solution and 30-10-2024 Question
Added 29-10-2024 Solution and 30-10-2024 Question
- Loading branch information
1 parent
c4e8067
commit 5329dca
Showing
3 changed files
with
92 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
### Explanation: | ||
|
||
**"Why is the unique element eternally single?** | ||
Because everyone else found their perfect match, leaving it to go solo forever!" 😂 | ||
|
||
Given the array `nums = [3, 3, 7, 7, 10, 11, 11]`: | ||
|
||
1. **Initialization:** `unique_ele = 0` | ||
|
||
2. **XOR Process:** | ||
|
||
- `0 ^ 3 = 3` → (`unique_ele = 3`) | ||
- `3 ^ 3 = 0` → (`unique_ele = 0`, 3s cancel out) | ||
- `0 ^ 7 = 7` → (`unique_ele = 7`) | ||
- `7 ^ 7 = 0` → (`unique_ele = 0`, 7s cancel out) | ||
- `0 ^ 10 = 10` → (`unique_ele = 10`) | ||
- `10 ^ 11 = 1` → (`unique_ele = 1`) | ||
- `1 ^ 11 = 10` → (`unique_ele = 10`, 11s cancel out) | ||
|
||
3. **Result:** | ||
- After the XOR operation on all elements, `unique_ele` contains `10`, which is the unique element in the array. | ||
|
||
### **Summary:** | ||
|
||
By using the XOR operation on each element in the array, we efficiently cancel out pairs of identical elements, leaving only the single, unique element. This approach allows the solution to run in linear time with constant space. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# Given sorted array where every element appears exactly twice except one unique element | ||
nums = [3, 3, 7, 7, 10, 11, 11] | ||
|
||
|
||
def unique_element(arr): | ||
# Initialize unique_ele to 0 since XOR with 0 retains the other number. | ||
# XOR of two identical numbers results in 0, while XOR of a number with 0 retains the number. | ||
# By XORing all elements, we eliminate paired numbers and are left with the unique number. | ||
unique_ele = 0 | ||
|
||
# Traverse through each element in the array | ||
for i in range(len(arr)): | ||
# XOR the current element with unique_ele. | ||
# This effectively "cancels out" pairs of identical numbers | ||
# and accumulates the unique number in unique_ele. | ||
unique_ele = unique_ele ^ arr[i] | ||
|
||
# After the loop completes, unique_ele will hold the single element | ||
return unique_ele | ||
|
||
|
||
# Print the result, which is the unique element in the array | ||
print("Unique Number is:", unique_element(nums)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
### **Question: Capital Usage Validator** | ||
|
||
**Difficulty Level:** 🟢 Beginner | ||
**Domain:** String Processing (Validation) | ||
|
||
### **Objective:** | ||
|
||
Given a word, determine if its capitalization is used correctly. We define correct capitalization as follows: | ||
|
||
1. All letters in the word are uppercase (e.g., `"USA"`). | ||
2. All letters in the word are lowercase (e.g., `"leetcode"`). | ||
3. Only the first letter in the word is uppercase (e.g., `"Google"`). | ||
|
||
If any of these conditions are met, return `True`. Otherwise, return `False`. | ||
|
||
### **Conceptual Background:** | ||
|
||
To validate correct capitalization, we simply need to check three conditions for the input word: | ||
|
||
1. **All Uppercase:** Every letter in the word is capital. | ||
2. **All Lowercase:** Every letter in the word is lowercase. | ||
3. **Title Case:** Only the first letter is uppercase, and all other letters are lowercase. | ||
|
||
Python provides straightforward string methods (`isupper`, `islower`, and `istitle`) to evaluate each of these cases, allowing for an efficient solution. | ||
|
||
### **Example Execution:** | ||
|
||
- **Input:** `"USA"` | ||
- **Processing:** | ||
- `isupper()` returns `True` because all letters are uppercase. | ||
- Since one condition is satisfied, the function returns `True`. | ||
- **Output:** `True` | ||
|
||
- **Input:** `"Google"` | ||
- **Processing:** | ||
- `istitle()` returns `True` since only the first letter is uppercase. | ||
- The function returns `True`. | ||
- **Output:** `True` | ||
|
||
- **Input:** `"flaG"` | ||
- **Processing:** | ||
- None of the conditions (`isupper`, `islower`, `istitle`) are satisfied. | ||
- The function returns `False`. | ||
- **Output:** `False` |