String matching in an array

class Solution {
    public List<String> stringMatching(String[] words) {
        if (words == null || words.length == 0) {
            return new ArrayList<>();
        }
        HashSet<String> set = new HashSet<>();
        for (String i : words) {
            if (!set.contains(i)) {
                for (String j : words) {
                    if (j.contains(i) && !j.equals(i)) {
                        set.add(i);
                        break;
                    }
                }
            }
        }
        return new ArrayList<>(set);
    }
}

or

StringBuilder builder = new StringBuilder();
        for (String word : words) {
            builder.append(word).append(",");
        }
        String wholeWords = builder.toString();
        List<String> resultList = new ArrayList<>();
        for (String word : words) {
            int index1 = wholeWords.indexOf(word);
            int index2 = wholeWords.lastIndexOf(word);
            if (index1 >= 0 && index2 >= 0 && index1 != index2) {
                resultList.add(word);
            }
        }
        return resultList;
        ```

Objectives of this Assignment 1. Declare arrays of different types dynamically. 2. Iterate through arrays, processing all of the elements. 3. Write methods with arrays as parameters and return values In this assignment you will create your own class and write the methods in it. The class you write is called Main and it has four methods that build arrays and four methods that process arrays. All methods should be public and static. Create a project called P7_3 and a class named Main in a file calledMain.java, then follow the instructions below exactly: 1. Write a method named createStrings that takes a String as an input parameter, and returns an array of string with 4 elements. The first element is the original string passed in, the second element is the same string converted to uppercase, the third element is the same string converted to lowercase, and the fourth element is the same string with the first character removed. 2. Write a method called charFrequency that takes an array of strings and a single character as parameters. The method should iterate the array, counting instances of the character passed in. For example, if the array is ["Hello", "There"] and we are asked to count the character 'e', the return value will be three. If the character is not found in any of the elements in the array, the return value should be zero. 3.Add a main method with the usual signature that instantiates the Main class and tests its methods as follow: public static void main(String[] args) { Scanner in = new Scanner(System.in); String str = in.nextLine(); char ch = in.next().charAt(0); // Create arrays String[] stringArray = createStrings(str); // Test processing System.out.println(charFrequency(stringArray, ch)); in.close(); } Input Specification: There are two lines in the input. The first line is the string to be converted. The second line contains one character to be counted. Output Specification: For each case, output the frequency of the specified character in the created array. Sample Input: Hello There e Sample Output: 在这里给出相应的输出。例如: 9
最新发布
03-10
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值