0% found this document useful (0 votes)
28 views2 pages

Document

The document contains Java code for a class named ValidPairFinder, which includes a method to find a valid pair of characters in a string based on specific frequency criteria. The method uses a frequency map to track occurrences of each character and checks adjacent characters for validity. The main method tests this functionality with three example strings.

Uploaded by

zygard143
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views2 pages

Document

The document contains Java code for a class named ValidPairFinder, which includes a method to find a valid pair of characters in a string based on specific frequency criteria. The method uses a frequency map to track occurrences of each character and checks adjacent characters for validity. The main method tests this functionality with three example strings.

Uploaded by

zygard143
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Import java.util.

*;

Public class ValidPairFinder {

Public static String findValidPair(String s) {

Map<Character, Integer> freqMap = new HashMap<>();

For (char c : s.toCharArray()) {

freqMap.put(c, freqMap.getOrDefault(c, 0) + 1);

For (int I = 0; I < s.length() – 1; i++) {

Char first = s.charAt(i);

Char second = s.charAt(I + 1);

If (first != second && freqMap.get(first) ==


Character.getNumericValue(first)

&& freqMap.get(second) == Character.getNumericValue(second))


{

Return “” + first + second;

Return “”;

Public static void main(String[] args) {

String s1 = “2523533”;

String s2 = “221”;

String s3 = “22”;

System.out.println(findValidPair(s1));
System.out.println(findValidPair(s2));

System.out.println(findValidPair(s3));

You might also like