3438. Find Valid Pair of Adjacent Digits in String LeetCode Solution

In this guide, you will get 3438. Find Valid Pair of Adjacent Digits in String LeetCode Solution with the best time and space complexity. The solution to Find Valid Pair of Adjacent Digits in String problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Find Valid Pair of Adjacent Digits in String solution in C++
  4. Find Valid Pair of Adjacent Digits in String solution in Java
  5. Find Valid Pair of Adjacent Digits in String solution in Python
  6. Additional Resources
3438. Find Valid Pair of Adjacent Digits in String LeetCode Solution image

Problem Statement of Find Valid Pair of Adjacent Digits in String

You are given a string s consisting only of digits. A valid pair is defined as two adjacent digits in s such that:

The first digit is not equal to the second.
Each digit in the pair appears in s exactly as many times as its numeric value.

Return the first valid pair found in the string s when traversing from left to right. If no valid pair exists, return an empty string.

Example 1:

Input: s = “2523533”
Output: “23”
Explanation:
Digit ‘2’ appears 2 times and digit ‘3’ appears 3 times. Each digit in the pair “23” appears in s exactly as many times as its numeric value. Hence, the output is “23”.

Example 2:

Input: s = “221”
Output: “21”
Explanation:
Digit ‘2’ appears 2 times and digit ‘1’ appears 1 time. Hence, the output is “21”.

See also  667. Beautiful Arrangement II LeetCode Solution

Example 3:

Input: s = “22”
Output: “”
Explanation:
There are no valid adjacent pairs.

Constraints:

2 <= s.length <= 100
s only consists of digits from '1' to '9'.

Complexity Analysis

  • Time Complexity: O(n)
  • Space Complexity: O(10) = O(1)

3438. Find Valid Pair of Adjacent Digits in String LeetCode Solution in C++

class Solution {
 public:
  string findValidPair(string s) {
    vector<int> count(10);

    for (const char c : s)
      ++count[c - '0'];

    for (int i = 0; i < s.length() - 1; ++i) {
      const int a = s[i] - '0';
      const int b = s[i + 1] - '0';
      if (a != b && count[a] == a && count[b] == b)
        return s.substr(i, 2);
    }

    return "";
  }
};
/* code provided by PROGIEZ */

3438. Find Valid Pair of Adjacent Digits in String LeetCode Solution in Java

class Solution {
  public String findValidPair(String s) {
    int[] count = new int[10];

    for (final char c : s.toCharArray())
      ++count[c - '0'];

    for (int i = 0; i < s.length() - 1; ++i) {
      final int a = s.charAt(i) - '0';
      final int b = s.charAt(i + 1) - '0';
      if (a != b && count[a] == a && count[b] == b)
        return s.substring(i, i + 2);
    }

    return "";
  }
}
// code provided by PROGIEZ

3438. Find Valid Pair of Adjacent Digits in String LeetCode Solution in Python

class Solution:
  def findValidPair(self, s: str) -> str:
    count = collections.Counter(s)
    return next((a + b
                for a, b in itertools.pairwise(s)
                if a != b and count[a] == int(a) and count[b] == int(b)), '')
# code by PROGIEZ

Additional Resources

Happy Coding! Keep following PROGIEZ for more updates and solutions.