Two Pointers
Two Pointers
Java
class Solution {
public int removeDuplicates(int[] nums) {
int i = 0;
for (int j = 1; j < nums.length; j++) {
if (nums[i] != nums[j]) {
i++;
nums[i] = nums[j];
}
}
return i + 1;
}
}
class Solution {
public int removeElement(int[] nums, int val) {
int i = 0;
for (int j = 0; j < nums.length; j++) {
if (nums[j] != val) {
nums[i] = nums[j];
i++;
}
}
return i;
}
}
class Solution {
public:
void moveZeroes(vector<int>& nums) {
int i = 0;
for (int j = 0; j < nums.size(); j++) {
if (nums[j] != 0) {
swap(nums[i], nums[j]);
i++;
}
}
}
};
Java
class Solution {
public void moveZeroes(int[] nums) {
int i = 0;
for (int j = 0; j < nums.length; j++) {
if (nums[j] != 0) {
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
i++;
}
}
}
}
class Solution {
public:
void reverseString(vector<char>& s) {
int left = 0, right = s.size() - 1;
while (left < right) {
swap(s[left], s[right]);
left++;
right--;
}
}
};
Java
class Solution {
public void reverseString(char[] s) {
int left = 0, right = s.length - 1;
while (left < right) {
char temp = s[left];
s[left] = s[right];
s[right] = temp;
left++;
right--;
}
}
}
Java
class Solution {
public int strStr(String haystack, String needle) {
int m = haystack.length(), n = needle.length();
for (int i = 0; i <= m - n; i++) {
if (haystack.substring(i, i + n).equals(needle)) {
return i;
}
}
return -1;
}
}
class Solution {
public String reverseOnlyLetters(String s) {
char[] chars = s.toCharArray();
int left = 0, right = chars.length - 1;
while (left < right) {
if (!Character.isLetter(chars[left])) {
left++;
} else if (!Character.isLetter(chars[right])) {
right--;
} else {
char temp = chars[left];
chars[left] = chars[right];
chars[right] = temp;
left++;
right--;
}
}
return new String(chars);
}
}
class Solution {
public int[] sortArrayByParityII(int[] nums) {
int i = 0, j = 1; // i for even indices, j for odd indices
int n = nums.length;
while (i < n && j < n) {
if (nums[i] % 2 == 0) {
i += 2;
} else if (nums[j] % 2 == 1) {
j += 2;
} else {
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
i += 2;
j += 2;
}
}
return nums;
}
}
Java
class Solution {
public String reverseStr(String s, int k) {
char[] chars = s.toCharArray();
for (int i = 0; i < chars.length; i += 2 * k) {
int left = i, right = Math.min(i + k - 1, chars.length - 1);
while (left < right) {
char temp = chars[left];
chars[left] = chars[right];
chars[right] = temp;
left++;
right--;
}
}
return new String(chars);
}
}
class Solution {
public:
string reverseWords(string s) {
int start = 0;
for (int end = 0; end <= s.size(); end++) {
if (end == s.size() || s[end] == ' ') {
reverse(s.begin() + start, s.begin() + end);
start = end + 1;
}
}
return s;
}
};
class Solution {
public String reverseWords(String s) {
char[] chars = s.toCharArray();
int start = 0;
for (int end = 0; end <= chars.length; end++) {
if (end == chars.length || chars[end] == ' ') {
reverse(chars, start, end - 1);
start = end + 1;
}
}
return new String(chars);
}
private void reverse(char[] chars, int left, int right) {
while (left < right) {
char temp = chars[left];
chars[left] = chars[right];
chars[right] = temp;
left++;
right--;
}
}
}
class Solution {
public:
int countBinarySubstrings(string s) {
int prev = 0, curr = 1, count = 0;
for (int i = 1; i < s.size(); i++) {
if (s[i] == s[i - 1]) {
curr++;
} else {
count += min(prev, curr);
prev = curr;
curr = 1;
}
}
return count + min(prev, curr);
}
};
class Solution {
public int countBinarySubstrings(String s) {
int prev = 0, curr = 1, count = 0;
for (int i = 1; i < s.length(); i++) {
if (s.charAt(i) == s.charAt(i - 1)) {
curr++;
} else {
count += Math.min(prev, curr);
prev = curr;
curr = 1;
}
}
return count + Math.min(prev, curr);
}
}
class Solution {
public void duplicateZeros(int[] arr) {
int n = arr.length, countZeros = 0;
for (int num : arr) {
if (num == 0) countZeros++;
}
int i = n - 1, j = n + countZeros - 1;
while (i >= 0) {
if (j < n) arr[j] = arr[i];
if (arr[i] == 0) {
j--;
if (j < n) arr[j] = 0;
}
i--;
j--;
}
}
}
class Solution {
public:
int removePalindromeSub(string s) {
int left = 0, right = s.size() - 1;
while (left < right) {
if (s[left] != s[right]) return 2;
left++;
right--;
}
return 1;
}
};
class Solution {
public int removePalindromeSub(String s) {
int left = 0, right = s.length() - 1;
while (left < right) {
if (s.charAt(left) != s.charAt(right)) return 2;
left++;
right--;
}
return 1;
}
}
class Solution {
public:
vector<int> findKDistantIndices(vector<int>& nums, int key, int k) {
vector<int> result;
for (int i = 0; i < nums.size(); i++) {
for (int j = 0; j < nums.size(); j++) {
if (nums[j] == key && abs(i - j) <= k) {
result.push_back(i);
break;
}
}
}
return result;
}
};
class Solution {
public List<Integer> findKDistantIndices(int[] nums, int key, int k) {
List<Integer> result = new ArrayList<>();
for (int i = 0; i < nums.length; i++) {
for (int j = 0; j < nums.length; j++) {
if (nums[j] == key && Math.abs(i - j) <= k) {
result.add(i);
break;
}
}
}
return result;
}
}
2460. Apply Operations to an Array
class Solution {
public:
vector<int> applyOperations(vector<int>& nums) {
int n = nums.size();
for (int i = 0; i < n - 1; i++) {
if (nums[i] == nums[i + 1]) {
nums[i] *= 2;
nums[i + 1] = 0;
}
}
int idx = 0;
for (int i = 0; i < n; i++) {
if (nums[i] != 0) {
swap(nums[idx++], nums[i]);
}
}
return nums;
}
};
class Solution {
public int[] applyOperations(int[] nums) {
int n = nums.length;
for (int i = 0; i < n - 1; i++) {
if (nums[i] == nums[i + 1]) {
nums[i] *= 2;
nums[i + 1] = 0;
}
}
int idx = 0;
for (int i = 0; i < n; i++) {
if (nums[i] != 0) {
int temp = nums[idx];
nums[idx++] = nums[i];
nums[i] = temp;
}
}
return nums;
}
}
};
class Solution {
public boolean validWordAbbreviation(String word, String abbr) {
int i = 0, j = 0;
while (i < word.length() && j < abbr.length()) {
if (Character.isDigit(abbr.charAt(j))) {
if (abbr.charAt(j) == '0') return false; // Leading zeros are invalid
int num = 0;
while (j < abbr.length() && Character.isDigit(abbr.charAt(j))) {
num = num * 10 + (abbr.charAt(j++) - '0');
}
i += num;
} else {
if (i >= word.length() || word.charAt(i++) != abbr.charAt(j++)) return false;
}
}
return i == word.length() && j == abbr.length();
}
}
class Solution {
public boolean isLongPressedName(String name, String typed) {
int i = 0, j = 0;
while (j < typed.length()) {
if (i < name.length() && name.charAt(i) == typed.charAt(j)) {
i++;
j++;
} else if (j > 0 && typed.charAt(j) == typed.charAt(j - 1)) {
j++;
} else {
return false;
}
}
return i == name.length();
}
}
class Solution {
public String firstPalindrome(String[] words) {
for (String word : words) {
if (isPalindrome(word)) {
return word;
}
}
return "";
}
private boolean isPalindrome(String s) {
int left = 0, right = s.length() - 1;
while (left < right) {
if (s.charAt(left) != s.charAt(right)) {
return false;
}
left++;
right--;
}
return true;
}
}
class Solution {
public long findTheArrayConcVal(int[] nums) {
long concatenationValue = 0;
int left = 0, right = nums.length - 1;
while (left <= right) {
if (left == right) {
concatenationValue += nums[left];
} else {
String concat = nums[left] + "" + nums[right];
concatenationValue += Long.parseLong(concat);
}
left++;
right--;
}
return concatenationValue;
}
}
class Solution {
public:
vector<int> findIndices(vector<int>& nums, int indexDifference, int valueDifference) {
int n = nums.size();
}
};
class Solution {
public int[] findIndices(int[] nums, int indexDifference, int valueDifference) {
int n = nums.length;