Skip to content

Acronym

kyra-ptn edited this page Aug 25, 2024 · 4 revisions

TIP102 Unit 1 Session 2 Standard (Click for link to problem statements)

Problem Highlights

  • 💡 Difficulty: Easy
  • Time to complete: 10 mins
  • 🛠️ Topics: String Processing, Iteration

U-nderstand

Understand what the interviewer is asking for by using test cases and questions about the problem.

  • Established a set (2-3) of test cases to verify their own solution later.
  • Established a set (1-2) of edge cases to verify their solution handles complexities.
  • Have fully understood the problem and have no clarifying questions.
  • Have you verified any Time/Space Constraints for this problem?
  • The function is_acronym() should take a list of strings words and a string s, and return True if s is an acronym of words, otherwise return False.
HAPPY CASE Input: words = ["christopher", "robin", "milne"], s = "crm" Expected Output: True Input: words = ["pooh", "bear"], s = "pb" Expected Output: True EDGE CASE Input: words = ["bear", "pooh"], s = "pb" Expected Output: False Input: words = [], s = " Expected Output: True 

P-lan

Plan the solution with appropriate visualizations and pseudocode.

General Idea: Define a function that checks if the given string s can be formed by concatenating the first characters of each string in the list words.

1. Define the function `is_acronym(words, s)`. 2. Check if the length of `s` is different from the length of `words`. If yes, return `False`. 3. Initialize an empty string `acronym` to store the constructed acronym. 4. Use a for loop to iterate through each word in `words`. 5. For each word, add its first character to `acronym`. 6. After the loop, compare `acronym` with `s` and return the result.

⚠️ Common Mistakes

  • Not correctly handling the case where the list of words is empty.
  • Incorrectly constructing the acronym by missing or adding extra characters.

I-mplement

Implement the code to solve the algorithm.

def is_acronym(words, s): # Check if lengths are different if len(s) != len(words): return False acronym = " # Iterate through each word in words and build the acronym for word in words: acronym += word[0] # Compare the constructed acronym with s return acronym == s
Clone this wiki locally