-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Valid Palindrome solition rust added
- Loading branch information
Showing
11 changed files
with
164 additions
and
14 deletions.
There are no files selected for viewing
File renamed without changes.
File renamed without changes.
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,34 @@ | ||
# [β Lowercase Alphanumeric to solve the π§π»βπ» Valid Palindrome Problem on π¦ Python language]() | ||
|
||
## 𧩠Complexity | ||
|
||
The `Solution` struct contains a public function `is_palindrome` that takes a `String` parameter `s` and returns a boolean value. The goal of the function is to determine if the given string `s` is a palindrome. | ||
|
||
The solution first converts the string `s` to lowercase using the `to_lowercase` method, storing the result in the variable `a`. This step is necessary to perform a case-insensitive comparison. | ||
|
||
Next, the characters of `a` are collected into a vector `b` using the `chars` method. Each character is filtered using the `retain` method, which removes whitespace and punctuation characters from `b`. This is achieved by keeping only the characters for which the closure condition `!x.is_whitespace() && !x.is_ascii_punctuation()` returns true. | ||
|
||
To check if the string is a palindrome, a clone of `b` is created in the variable `c`. The reverse method is then used to `reverse` the order of characters in `c`. | ||
|
||
Finally, an if statement compares the vectors `b` and `c`. If they are equal, it means that the original string `s` is a palindrome, so the function returns `true`. Otherwise, it returns `false`. | ||
|
||
## π Code | ||
|
||
``` RUST | ||
impl Solution { | ||
pub fn is_palindrome(s: String) -> bool { | ||
let a = s.to_lowercase(); | ||
let mut b = a.chars().collect::<Vec<char>>(); | ||
b.retain(|&x| !x.is_whitespace() && !x. is_ascii_punctuation()); | ||
|
||
let mut c = b.clone(); | ||
c.reverse(); | ||
if b == c{ | ||
return true; | ||
} | ||
return false; | ||
} | ||
} | ||
``` | ||
|
||
|
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,43 @@ | ||
# [Valid Palindrome](https://leetcode.com/problems/valid-palindrome/) | ||
|
||
## π¨ Problem | ||
<!-- Explanation of problem. --> | ||
A phrase is a **palindrome** if, after converting all uppercase letters into lowercase letters and removing all non-alphanumeric characters, it reads the same forward and backward. Alphanumeric characters include letters and numbers. | ||
|
||
Given a string `s`, return `true` _if it is a **palindrome**, or_ `false` _otherwise_. | ||
|
||
**Example 1:** | ||
<!-- An example of problem. --> | ||
|
||
>**Input:** s = "A man, a plan, a canal: Panama" </br> <!-- Input example. --> | ||
**Output:** true </br> <!-- Output example. --> | ||
**Explanation:** "amanaplanacanalpanama" is a palindrome. <!-- Basic explanation of example. --> | ||
**Example 2:** | ||
<!-- An example of problem. --> | ||
|
||
>**Input:** s = "race a car" </br> <!-- Input example. --> | ||
**Output:** false </br> <!-- Output example. --> | ||
**Explanation:** "raceacar" is not a palindrome. | ||
|
||
**Example 3:** | ||
<!-- An example of problem. --> | ||
|
||
>**Input:** s = " " </br> <!-- Input example. --> | ||
**Output:** true </br> <!-- Output example. --> | ||
**Explanation:** s is an empty string "" after removing non-alphanumeric characters. Since an empty string reads the same forward and backward, it is a palindrome. | ||
|
||
**Constraints:** | ||
<!-- Constraints of problem. --> | ||
- $1 <= s.length <= 2 * 10^5$ | ||
- `s` consists only of printable ASCII characters. | ||
|
||
**Follow-up:** | ||
<!-- Do more! --> | ||
|
||
## π Solutions | ||
<!-- Solutions of problem and their links. --> | ||
|
||
| ID | METHOD | | ||
| :-- | :-----------------------------------: | | ||
| 1 | [Lowercase Alphanumeric](1-answer.md) | |
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
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,21 @@ | ||
# [Header](link) | ||
|
||
## π§π»βπ» Approach | ||
<!-- Describe your approach to solving the problem. --> | ||
Explain the approach | ||
|
||
## π Code | ||
|
||
``` Programming language | ||
printf("Hello World"); | ||
``` | ||
|
||
## 𧩠Complexity | ||
|
||
- Time complexity: | ||
<!-- Add your time complexity here, e.g. $O(n)$ --> | ||
Time Complexity | ||
|
||
- Space complexity: | ||
<!-- Add your space complexity here, e.g. $O(n)$ --> | ||
Space Complexity |
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,50 @@ | ||
# [Two Sum II - Input Array Is Sorted](https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/) | ||
|
||
## π¨ Problem | ||
<!-- Explanation of problem. --> | ||
Given a **1-indexed** array of integers `numbers` that is already **_sorted in non-decreasing order_**, find two numbers such that they add up to a specific `target` number. Let these two numbers be $numbers[index_1]$ and $numbers[index_2]$ where $1 <= index_1 < index_2 <Β numbers.length$. | ||
|
||
Return _the indices of the two numbers,_ $index_1$ _and_ $index_2$_, **added by one** as an integer array_ $[index_1, index_2]$ _of length 2._ | ||
|
||
The tests are generated such that there is **exactly one solution**. You **may not** use the same element twice. | ||
|
||
Your solution must use only constant extra space. | ||
|
||
**Example 1:** | ||
<!-- An example of problem. --> | ||
|
||
>**Input:** numbers = \[2,7,11,15\], target = 9</br> <!-- Input example. --> | ||
**Output:** \[1,2\]</br> <!-- Output example. --> | ||
**Explanation:** The sum of 2 and 7 is 9. Therefore, $index_1$ = 1, $index_2$ = 2. We return \[1, 2\]. <!-- Basic explanation of example. --> | ||
**Example 2:** | ||
<!-- An example of problem. --> | ||
|
||
>**Input:** numbers = \[2,3,4\], target = 6 </br> <!-- Input example. --> | ||
**Output:** \[1,3\] </br> <!-- Output example. --> | ||
**Explanation:** The sum of 2 and 4 is 6. Therefore $index_1$ = 1, $index_2$ = 3. We return \[1, 3\]. <!-- Basic explanation of example. --> | ||
**Example 3:** | ||
<!-- An example of problem. --> | ||
|
||
>**Input:** numbers = \[\-1,0\], target = -1 </br> <!-- Input example. --> | ||
**Output:** \[1,2\] </br> <!-- Output example. --> | ||
**Explanation:** The sum of -1 and 0 is -1. Therefore $index_1$ = 1, $index_2$ = 2. We return \[1, 2\]. <!-- Basic explanation of example. --> | ||
**Constraints:** | ||
<!-- Constraints of problem. --> | ||
- $2 <= numbers.length <= 3 * 10^4$ | ||
- $-1000 <= numbers[i] <= 1000$ | ||
- `numbers` is sorted in **non-decreasing order**. | ||
- $-1000 <= target <= 1000$ | ||
- The tests are generated such that there is **exactly one solution**. | ||
|
||
**Follow-up:** | ||
<!-- Do more! --> | ||
|
||
## π Solutions | ||
<!-- Solutions of problem and their links. --> | ||
|
||
| ID | METHOD | | ||
| :-- | :--------------------: | | ||
| 1 | [example](1-answer.md) | |
File renamed without changes.
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
File renamed without changes.
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