Skip to content

Added Rust solutions for Chapter 1: Two Pointers #86

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jun 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions rust/Two Pointers/is_palindrome_valid.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
fn is_palindrome_valid(s: &str) -> bool {
let chars: Vec<_> = s.chars().collect();
let mut left = 0;
let mut right = s.len() - 1;
while left < right {
// Skip non-alphanumeric characters from the left.
while left < right && !chars[left].is_alphanumeric(){
left += 1
}
// Skip non-alphanumeric characters from the right.
while left < right && !chars[right].is_alphanumeric(){
right -= 1
}
// If the characters at the left and right pointers don't
// match, the string is not a palindrome.
if chars[left] != chars[right] {
return false;
}
left += 1;
right -= 1;
}
true
}
28 changes: 28 additions & 0 deletions rust/Two Pointers/largest_container.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
use std::{
cmp::{max, min}
};

fn largest_container(heights: Vec<i32>) -> i32 {
let mut max_water = 0;
let mut left = 0;
let mut right = heights.len() - 1;

while left < right {
// Calculate the water contained between the current pair of lines.
let water = min(heights[left], heights[right])
* (right as i32 - left as i32);
max_water = max(max_water, water);
// Move the pointers inward, always moving the pointer at the
// shorter line. If both lines have the same height, move both
// pointers inward.
if heights[left] < heights[right] {
left += 1;
} else if heights[left] > heights[right] {
right -= 1;
} else {
left += 1;
right -= 1;
}
}
max_water
}
20 changes: 20 additions & 0 deletions rust/Two Pointers/pair_sum_sorted.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
fn pair_sum_sorted(nums: &[i32], target: i32) -> Vec<usize> {
let mut left = 0;
let mut right = nums.len() - 1;
while left < right {
let sum = nums[left] + nums[right];
// If the sum is smaller, increment the left pointer, aiming
// to increase the sum toward the target value.
if sum < target {
left += 1;
} else if sum > target {
// If the sum is larger, decrement the right pointer, aiming
// to decrease the sum toward the target value.
right -= 1;
} else {
// If the target pair is found, return its indexes.
return vec![left, right];
}
}
vec![]
}
54 changes: 54 additions & 0 deletions rust/Two Pointers/triplet_sum.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
fn triplet_sum(mut nums: Vec<i32>) -> Vec<Vec<i32>> {
let mut triplets: Vec<Vec<i32>> = vec![];
nums.sort();
let len = nums.len();

for i in 0..len {
// Optimization: triplets consisting of only positive numbers
// will never sum to 0.
if nums[i] > 0 {
break;
}

// To avoid duplicate triplets, skip 'a' if it's the same as
// the previous number.
if i > 0 && nums[i] == nums[i - 1] {
continue;
}

// Find all pairs that sum to a target of '-a' (-nums[i]).
let pairs = pair_sum_sorted_all_pairs(&nums, i + 1, -nums[i]);
for pair in pairs {
triplets.push(vec![nums[i], pair[0], pair[1]]);
}
}

triplets
}

fn pair_sum_sorted_all_pairs(nums: &Vec<i32>, start: usize, target: i32) -> Vec<Vec<i32>> {
let mut pairs = Vec::new();
let mut left = start;
let mut right = nums.len() - 1;

while left < right {
let sum = nums[left] + nums[right];

if sum == target {
pairs.push(vec![nums[left], nums[right]]);
left += 1;

// To avoid duplicate '[b, c]' pairs, skip 'b' if it's the
// same as the previous number.
while left < right && nums[left] == nums[left - 1] {
left += 1;
}
} else if sum < target {
left += 1;
} else {
right -= 1;
}
}

pairs
}