字符串精简功能实现遇阻:HashMap方案部分Hackerrank测试用例失败求助
Hey there! Let's break down why your code might be failing some Hackerrank test cases. Based on what you've described, here are the most likely issues and fixes:
1. Unordered Character Output (Biggest Culprit)
The standard HashMap in Java doesn't preserve the order of characters as they appear in the original string. When you iterate over the keys of a HashMap, you'll get characters in a random (hash-based) order, not the order they first showed up in your input.
For example, if your input was bac (each character appears once, so all should be kept), a HashMap might output abc instead of bac—which would fail test cases that check for correct output order.
Fix: Use LinkedHashMap Instead
LinkedHashMap maintains the insertion order of elements, so the characters will stay in the same order they first appeared in the input string. Just replace your HashMap declaration with LinkedHashMap:
LinkedHashMap<Character, Integer> charCount = new LinkedHashMap<>();
2. Incorrect Result Construction (Duplicate Characters)
If you're just adding each qualifying character (with odd counts) once to the result, that's correct for your example—but make sure you're not accidentally adding multiple instances. Alternatively, if you want to guarantee order without switching map types, iterate over the original string and add valid characters only once:
static String super_reduced_string(String s) { HashMap<Character, Integer> charCount = new HashMap<>(); // First pass: count occurrences for (char c : s.toCharArray()) { charCount.put(c, charCount.getOrDefault(c, 0) + 1); } StringBuilder result = new StringBuilder(); // Second pass: build result in original order, adding each valid char once for (char c : s.toCharArray()) { int count = charCount.get(c); if (count % 2 == 1 && result.indexOf(String.valueOf(c)) == -1) { result.append(c); } } return result.toString(); }
This way, you guarantee the output matches the order of first occurrence in the input, which will pass order-sensitive test cases.
3. Edge Case Handling
Make sure your code handles edge cases like:
- Empty input string (should return empty)
- All characters have even counts (should return empty string)
- Single character input (should return that character)
For example, if input is aabb, your code should return an empty string—double-check that your logic correctly skips all even-count characters.
Final Corrected Code Example
Here's a polished version using LinkedHashMap for order preservation:
import java.util.LinkedHashMap; import java.util.Map; public class Solution { static String super_reduced_string(String s) { Map<Character, Integer> charCount = new LinkedHashMap<>(); // Count each character's occurrences for (char c : s.toCharArray()) { charCount.put(c, charCount.getOrDefault(c, 0) + 1); } StringBuilder sb = new StringBuilder(); // Build result in insertion order for (Map.Entry<Character, Integer> entry : charCount.entrySet()) { if (entry.getValue() % 2 == 1) { sb.append(entry.getKey()); } } return sb.toString(); } }
This should fix the order-related test case failures, as LinkedHashMap ensures we process characters in the order they first appeared in the input.
内容的提问来源于stack exchange,提问作者Karthik

