Created
May 21, 2022 08:57
-
-
Save chnirt/fed7dea7eb4d9d79330b4f09fc3fe730 to your computer and use it in GitHub Desktop.
Find the second most frequent letter with multiple conditions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// There is a string s, which consists of lowercase letters from twenty-six alphabet letters "a" through "z". You are asked to find the second most frequent letter in this string. For example, if s = “xddxdca”, then the second most frequent letter in this string is“x”. | |
// Even if there is more than one letter that are most frequent, none of them should count toward picking the second most frequent letter. For example, if s = “jpcpjppcyycyy”, then the most frequent letters in this string are “p” and “y”, as there are four of each in the string. In this case, the second most frequent letter in this string will be “c”, as there are three "c"s in this string. | |
// If there is more than one second most frequent letter in the given string, then arrange them in alphabetical order. For example, if s = “brbabrrara”, then the second most frequent letters here are “b” and “a”. Arrange them in alphabetical order, and you get “ab”. | |
// In the case where there is no second most frequent letter in the given string s, you must return “-“. For example, if s = “xxx”, where the string consists of only one letters, or, if s = “gwgwgw”, where the string consists of two letters of the same quantity, you must return ”-“. | |
// Suppose a parameter s is given, where s is a string. Please write a function solution that returns a string that contains the second most frequent letter in the given string s, as described above. | |
// Constraints | |
// The length of the string s is between 1 and 1,000. | |
// The string s consists of lowercase English letters only. | |
// Examples | |
// s answer | |
// "xddxdca" "x" | |
// "jpcpjppcyycyy" "c" | |
// "brbabrrara" "ab" | |
// "xxx" "-" | |
// "gwgwgw" "-" | |
// Demonstrated in the prompt above. | |
// ----- | |
import java.util.*; | |
import java.util.stream.*; | |
import java.util.Arrays; | |
import java.util.Scanner; | |
public class MyClass { | |
public static void main(String args[]) { | |
int x=10; | |
int y=25; | |
int z=x+y; | |
// System.out.println("Sum of x+y = " + z); | |
for (String s : Arrays.asList("ababababd", "ababdababc", "dabbcacd", "acbdagh")) { | |
System.out.println(s + " -> " + get2ndMostFrequent(s)); | |
} | |
} | |
public static String get2ndMostFrequent(String str) { | |
if (null == str || str.length() < 2) { | |
return null; | |
} | |
String newStr = new Scanner(str).nextLine(); | |
char charArray[] = newStr.toCharArray(); | |
Arrays.sort(charArray); | |
System.out.println(new String(charArray)); | |
String formatStr = new String(charArray); | |
return Arrays.stream(formatStr.split("")) // Stream<String> chars | |
.collect(Collectors.groupingBy( | |
s -> s, LinkedHashMap::new, Collectors.summingInt(s -> 1) | |
)) | |
.entrySet() | |
.stream() | |
.collect(Collectors.toMap( | |
Map.Entry::getValue, | |
Map.Entry::getKey, | |
(s1, s2) -> s1 + s2, | |
() -> new TreeMap<Integer, String>(Comparator.reverseOrder()) | |
)) | |
.values() | |
.stream() | |
.skip(1) | |
.findFirst() | |
.orElse("-"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment