String Prefix Checker in TypeScript
This challenge asks you to implement a function that determines if a given array of strings all start with a specified prefix. This is a common task in data processing and validation, useful for filtering lists of items based on a shared characteristic or ensuring data consistency. Your function should be robust and handle various edge cases gracefully.
Problem Description
You are tasked with creating a TypeScript function called startsWithPrefix that takes two arguments: an array of strings (strings) and a string (prefix). The function should return true if every string in the strings array begins with the provided prefix. If even one string does not start with the prefix, the function should return false.
Key Requirements:
- The function must be written in TypeScript.
- The function must handle empty arrays gracefully.
- The function must handle empty prefixes gracefully.
- The function should be case-sensitive.
- The function should return a boolean value.
Expected Behavior:
The function should iterate through the strings array and, for each string, check if it starts with the prefix using the startsWith() method. If any string fails this check, the function should immediately return false. If all strings pass the check, the function should return true.
Edge Cases to Consider:
- Empty
stringsarray: Should returntrue(vacuously true - all elements satisfy the condition because there are no elements). - Empty
prefix: Should returntrue(every string starts with an empty string). stringsarray containing non-string elements: While the problem specifies an array of strings, consider how your function should behave if it encounters a non-string element. For this challenge, you can assume the input will always be an array of strings.prefixlonger than any string instrings: Should returnfalsein this case.
Examples
Example 1:
Input: ["apple", "apricot", "avocado"], "ap"
Output: true
Explanation: All strings in the array start with "ap".
Example 2:
Input: ["apple", "banana", "apricot"], "ap"
Output: false
Explanation: "banana" does not start with "ap".
Example 3:
Input: ["apple", "apricot", "avocado"], ""
Output: true
Explanation: Every string starts with an empty string.
Example 4:
Input: [], "ap"
Output: true
Explanation: The array is empty, so all (zero) elements satisfy the condition.
Example 5:
Input: ["apple", "apricot", "avocado"], "applet"
Output: false
Explanation: "apple" is the only string that *could* start with "applet", but it doesn't.
Constraints
- The
stringsarray will contain only strings. - The
prefixwill be a string. - The length of the
stringsarray can be up to 1000. - The length of each string in the
stringsarray can be up to 100. - The length of the
prefixcan be up to 50. - Performance: The function should complete in a reasonable time (e.g., less than 1 second) for the given constraints.
Notes
Consider using the built-in startsWith() method for string comparison. Think about how to efficiently short-circuit the loop once a string is found that doesn't match the prefix. Focus on writing clean, readable, and well-documented code.