Contents

Tail String (with.Java)

   Nov 25, 2023     1 min read

This is a post about the “tail string” problem.

We’re going to learn about it by solving coding test problems, reflecting on the problems we’ve solved, and exploring other ways to solve them.

Let’s start with the problem.

The Problem

Given a list of strings, the string that combines all the strings in order is called the tail string.

When creating the tail string, you want to exclude strings that contain a specific string.

For example, if you have a list of strings [“abc”, “def”, “ghi”] and you create a tail string that excludes strings containing the string “ef”, you get “abcghi”.

Given a list of strings str_list and a string ex that you want to exclude, complete the solution function so that it returns a tail string created by excluding strings containing ex from str_list.

Example input and output

str_listexresult
[“abc”, “def”, “ghi”]“ef”“abcghi”
[“abc”, “bbc”, “cbc”]“c”””

My solution to the ### problem

class Solution {
    public String solution(String[] str_list, String ex) {
        String answer = "";
        for(String temp: str_list){
            answer += temp.contains(ex) ? "" : temp;
        }
    } return answer;
}

solution description

String answer = “”;: Initialize an empty string answer to store the result.

for(String temp : str_list) : Iterate over the array of strings str_list, examining each string temp.

answer += temp.contains(ex) ? “” : temp;: if the current string temp contains ex, add an empty string; otherwise, add the string temp to answer.

return answer;: Return answer, the result of concatenating the strings in str_list that do not contain ex.