How to use Two-Pointer Technique In Javascript

Using the two-pointer technique to reverse consonants involves placing pointers at the beginning and end of the string, swapping consonants when both pointers are at consonants, and moving pointers inward. This continues until the pointers cross, ensuring non-consonants remain unaffected.

Example: In this example The function reverseConsonants reverses consonants in a string using a two-pointer technique, leaving other characters unchanged. It correctly reverses consonants while preserving the string’s structure.

JavaScript
function reverseConsonants(str) {
  const isConsonant = (char) => /[bcdfghjklmnpqrstvwxyz]/i.test(char);
  
  let arr = str.split('');
  let left = 0;
  let right = arr.length - 1;
  
  while (left < right) {
    if (!isConsonant(arr[left])) {
      left++;
    } else if (!isConsonant(arr[right])) {
      right--;
    } else {
      [arr[left], arr[right]] = [arr[right], arr[left]];
      left++;
      right--;
    }
  }
  
  return arr.join('');
}

console.log(reverseConsonants("Hello, World!")); // "Hollo, Werld!"

Output
delro, WollH!

JavaScript Program to Reverse Consonants in a Given String Without Affecting the Other Elements

In this article, we have given a string and the task is to write a javascript program to reverse only the consonants of a string without affecting the other elements’ positions and printing the output in the console.

Examples:

Input: hello
Output: lelho
Explanation: h,l,l are consonants in the given string.
After modifying the string their occurrences are l,l,h and
vowels positions are not changed.

These are the following approaches by using these we can solve the given question:

Similar Reads

Using loop:

In this approach, we reverse the consonants in a given string. It initializes pointers at the start and end of the string, identifies consonants using a set, and swaps them until the pointers meet. If a character is not a consonant, the corresponding pointer is incremented or decremented accordingly. The code converts the array of characters back to a string and returns the modified string....

Using reverse() method:

In this approach, the function generates a consonant list from the input string using array operations. It reverses this list, initializes an empty string, and iterates through the input, replacing consonants with reversed ones. It utilizes the ‘match()‘ method for character checks, array methods like ‘filter()‘ and ‘reverse()‘, and string concatenation to build the modified string, which is then returned....

Using Two-Pointer Technique

Using the two-pointer technique to reverse consonants involves placing pointers at the beginning and end of the string, swapping consonants when both pointers are at consonants, and moving pointers inward. This continues until the pointers cross, ensuring non-consonants remain unaffected....

Using Regular Expressions and Array Manipulation:

This approach utilizes regular expressions to match consonants and then employs array manipulation techniques to reverse only the consonants while keeping the vowels and other characters in their original positions....

Contact Us