Count Case-Insensitive Unique Strings
Given an array of strings, words, your task is to count the number of unique strings present in the array, considering case-insensitivity. This means that if 'apple' and 'Apple' are both in the array, they should be treated as the same string and only contribute one to the total unique count. The final count should reflect the total number of distinct strings after normalizing for case.
[ "[\"apple\", \"banana\", \"Apple\", \"orange\", \"banana\"]" ]
Explanation. The unique case-insensitive strings are 'apple' (from 'apple', 'Apple'), 'banana' (from 'banana', 'banana'), and 'orange'. There are 3 distinct strings.
[ "[\"Hello\", \"world\", \"HELLO\", \"World\", \"test\"]" ]
Explanation. The unique case-insensitive strings are 'hello' (from 'Hello', 'HELLO'), 'world' (from 'world', 'World'), and 'test'. There are 3 distinct strings.
[ "[\"one\", \"two\", \"three\"]" ]
Explanation. All strings are distinct even considering case-insensitivity. There are 3 distinct strings.
[ "[]" ]
Explanation. An empty input array contains no strings, so the count of unique strings is 0.
[ "[\"A\", \"a\", \"A\", \"b\", \"B\"]" ]
Explanation. The unique case-insensitive strings are 'a' (from 'A', 'a', 'A') and 'b' (from 'b', 'B'). There are 2 distinct strings.
[ "[\"foo\", \"Foo\", \"BAR\", \"foo\", \"baz\"]" ]
Explanation. The unique case-insensitive strings are 'foo' (from 'foo', 'Foo', 'foo'), 'bar' (from 'BAR'), and 'baz'. There are 3 distinct strings.
[ "[\"\", \" \", \" \", \"\", \"abc\"]" ]
Explanation. The unique case-insensitive strings are '' (empty string), ' ' (string with a single space), and 'abc'. Spaces are significant. There are 3 distinct strings.
[ "[\"JavaScript\", \"javascript\", \"JAVASCRIPT\"]" ]
Explanation. All three strings are case-insensitive variations of 'javascript'. Only one unique string.
Follow-up: Modify your solution to return an array of the unique strings themselves (maintaining their first encountered case) instead of just the count. For example, for input `["apple", "banana", "Apple"]`, the output could be `["apple", "banana"]`.
The input array `words` will contain between 0 and 1000 strings. Each string `word` will have a length between 0 and 50 characters (inclusive). Each string `word` will consist of English letters (uppercase and lowercase) and spaces. Spaces are significant, e.g., 'hello world' is distinct from 'helloworld'.
- Views
- 3