Skip to content

Commit

Permalink
Valid Palindrome solition rust added
Browse files Browse the repository at this point in the history
  • Loading branch information
sektor7k committed Jun 7, 2023
1 parent dc7525f commit 8e05099
Show file tree
Hide file tree
Showing 11 changed files with 164 additions and 14 deletions.
File renamed without changes.
File renamed without changes.
34 changes: 34 additions & 0 deletions Rust/0125-valid-palindrome/1-answer.md
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;
}
}
```


43 changes: 43 additions & 0 deletions Rust/0125-valid-palindrome/question.md
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) |
2 changes: 1 addition & 1 deletion Rust/0155-min-stack/question.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,4 @@ minStack.getMin(); // return -2
| ID | METHOD |
| :-- | :-------------------------: |
| 1 | [Double Stack](1-answer.md) |
| 2 | [Double Stack](2-answer.md) |
| 2 | [Double Stack 2](2-answer.md) |
21 changes: 21 additions & 0 deletions Rust/0167-two-sum-2/1-answer.md
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
50 changes: 50 additions & 0 deletions Rust/0167-two-sum-2/question.md
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.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@ Given an integer array `nums`, return `true` if any value appears **at least twi
## Solutions
<!-- Solutions of problem and their links. -->

| ID | METHOD | LINK |
| :-- | :-----: | :--- |
| 1 | example | |
| ID | METHOD |
| :-- | :--------------------: |
| 1 | [example](1-answer.md) |
File renamed without changes.
22 changes: 12 additions & 10 deletions Rust/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,18 @@

---

| ID | Leetcode ID | πŸ‘€ Question Name | πŸ“¦ Category | πŸ”₯ Difficulty |
| --- | :---------- | :---------------------------------------------------------------------------: | ---------------- | -------------- |
| 1 | 0001 | [Two Sum](0001-two-sum/q-two-sum.md) | Arrays & Hashing | Easy |
| 2 | 0217 | [Contains Duplicate](0217-contains-duplicate/q-contains-duplicate.md) | Arrays & Hashing | Easy |
| 3 | 0242 | [Valid Anagram](0242-valid-anagram/q-valid-anagram.md) | Arrays & Hashing | Easy |
| 4 | 0049 | [Group Anagram](0049-group-anagram/q-group-anagram.md) | Arrays & Hashing | Medium |
| 5 | 0347 | [Top K Frequent Elements](0347-top-k-frequent-elements/question.md) | Arrays & Hashing | Medium |
| 6 | 0238 | [Product Of Array Except Self](0238-product-of-array-except-self/question.md) | Arrays & Hashing | Medium |
| 7 | 0020 | [Valid Parentheses](0020-valid-parentheses/question.md) | Stack | Easy |
| 8 | 0155 | [Min Stack](0155-min-stack/question.md) | Stack | Medium |
| ID | Leetcode ID | πŸ‘€ Question Name | πŸ“¦ Category | πŸ”₯ Difficulty | 🏒 Company |
| --- | :---------- | :---------------------------------------------------------------------------: | ---------------- | -------------- | ----------- |
| 1 | 0001 | [Two Sum](0001-two-sum/question.md) | Arrays & Hashing | Easy | Microsoft |
| 2 | 0217 | [Contains Duplicate](0217-contains-duplicate/question.md) | Arrays & Hashing | Easy | Uber |
| 3 | 0242 | [Valid Anagram](0242-valid-anagram/question.md) | Arrays & Hashing | Easy | Facebook |
| 4 | 0049 | [Group Anagram](0049-group-anagram/question.md) | Arrays & Hashing | Medium | Amazon |
| 5 | 0347 | [Top K Frequent Elements](0347-top-k-frequent-elements/question.md) | Arrays & Hashing | Medium | Amazon |
| 6 | 0238 | [Product Of Array Except Self](0238-product-of-array-except-self/question.md) | Arrays & Hashing | Medium | Amazon |
| 7 | 0020 | [Valid Parentheses](0020-valid-parentheses/question.md) | Stack | Easy | Facebook |
| 8 | 0125 | [Valid Palindrome](0125-valid-palindrome/question.md) | Two Pointers | Easy | Spotify |
| 9 | 0155 | [Min Stack](0155-min-stack/question.md) | Stack | Medium | Amazon |
| 10 | 0167 | [Two Sum II Input Array Is Sorted](0167-two-sum-2/question.md) | Two Pointers | Medium | Amazon |

<div align="center">
<h1>Awesome Leetcode Algorithms Solututions With Rust</h1>
Expand Down

0 comments on commit 8e05099

Please sign in to comment.