First Unique Character Across Strings
Given an array of strings, words, find the first character that appears exactly once across all the strings combined. The character should be determined by its first appearance in the combined sequence of characters formed by concatenating the strings in the given order. If no such character exists, return an empty string.
For example, if words = ["hello", "world", "h"]:
- The combined sequence of characters is "helloworldh".
- Counting character frequencies:
- 'h': 2 times
- 'e': 1 time
- 'l': 3 times
- 'o': 2 times
- 'w': 1 time
- 'r': 1 time
- 'd': 1 time
- Characters appearing exactly once are 'e', 'w', 'r', 'd'.
- The first character among these, based on their appearance in "helloworldh", is 'e'.
[ "[\"apple\", \"banana\"]" ]
Explanation. The combined string is "applebanana". Character counts are: a:3, p:2, l:1, e:1, b:1, n:2. The unique characters are 'l', 'e', 'b'. The first among these in "applebanana" is 'l'.
[ "[\"aa\", \"bb\"]" ]
Explanation. The combined string is "aabb". Character counts are: a:2, b:2. No character appears exactly once.
[ "[\"abca\", \"defg\"]" ]
Explanation. The combined string is "abcdefg". Character counts are: a:2, b:1, c:1, d:1, e:1, f:1, g:1. 'a' appears twice. 'b' is the first character in the combined string that appears exactly once.
[ "[\"leetcode\"]" ]
Explanation. The combined string is "leetcode". Character counts are: l:1, e:3, t:1, c:1, o:1, d:1. 'l' is the first unique character.
[ "[\"abacaba\", \"defgh\", \"ij\"]" ]
Explanation. The combined string is "abacabadefghij". Character counts are: a:4, b:2, c:1, d:1, e:1, f:1, g:1, h:1, i:1, j:1. 'c' is the first character in the combined string that appears exactly once.
[ "[\"a\", \"b\", \"a\", \"c\"]" ]
Explanation. The combined string is "abac". Character counts are: a:2, b:1, c:1. 'b' is the first character that appears exactly once.
Follow-up: How would you modify your approach if you needed to return *all* unique characters, sorted by their first appearance in the combined sequence, instead of just the first one?
- The `words` array will contain between 1 and 100 strings. - Each string in `words` will consist of lowercase English letters only. - The length of each string will be between 1 and 100 characters. - The total number of characters across all strings will not exceed 1000.
- Views
- 3