Coding questions · xAI
Implement two string utility functions
This question evaluates proficiency in string manipulation and text processing, including character counting, tokenization by spaces, case-sensitive comparisons, and preserving original word order.
Implement the following Python functions:
-
number_of_character(string, char)- Return the number of times character
charappears instring. - Matching should be case-sensitive.
- Example:
number_of_character("Today is Tuesday", "t")should return2.
- Return the number of times character
-
unique_string(msg1, msg2)- Split each input message into words using spaces.
- Return the words that appear in
msg1but not inmsg2, followed by the words that appear inmsg2but not inmsg1. - Preserve original order.
- Use case-sensitive comparison.
- Example:
msg1 = "This is the first message"msg2 = "Hello this is the second message"- Output:
["This", "first", "Hello", "this", "second"]
Part 1: Count Occurrences of a Character
Implement a function that returns how many times a given character appears in a string. Matching is case-sensitive, so uppercase and lowercase letters are treated as different characters. For example, counting 'T' in 'Today is Tuesday' returns 2.
Constraints
- 0 <= len(string) <= 100000
- len(char) == 1
- Matching is case-sensitive.
- string may contain letters, digits, spaces, or punctuation.
Examples
Example 1
Input: ('banana', 'a')
Output: 3
Explanation: The character 'a' appears three times in 'banana'.
Example 2
Input: ('Today is Tuesday', 'T')
Output: 2
Explanation: There are two uppercase 'T' characters.
Hints
- Scan the string one character at a time and compare each character to char.
- Be careful not to convert the string to lowercase or uppercase; matching must be case-sensitive.
Part 2: Find Words Unique to Each Message
Implement a function that compares two messages and returns the words that appear in exactly one of the messages. First return the distinct words that appear in msg1 but not in msg2, preserving their first-appearance order in msg1. Then append the distinct words that appear in msg2 but not in msg1, preserving their first-appearance order in msg2. Word comparison is case-sensitive. Multiple repeated copies of the same unique word in one message should appear only once in the output.
Constraints
- 0 <= len(msg1), len(msg2) <= 100000
- Messages contain words separated by spaces.
- Word comparison is case-sensitive.
- If a word appears in both messages exactly, it should not be included in the result.
- If the same unique word appears multiple times in one message, include it only once at its first occurrence.
Examples
Example 1
Input: ('This is the first message', 'Hello this is the second message')
Output: ['This', 'first', 'Hello', 'this', 'second']
Explanation: 'This' and 'first' appear only in msg1. 'Hello', 'this', and 'second' appear only in msg2. Case matters, so 'This' and 'this' are different.
Example 2
Input: ('', '')
Output: []
Explanation: Both messages are empty, so there are no unique words.
Hints
- Use sets to quickly check whether a word from one message appears in the other message.
- Build the answer in two passes: first over msg1, then over msg2. Track words already added to avoid duplicates.