Q3

 avatar
unknown
java
3 years ago
1.8 kB
9
Indexable
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author thaiq
 */
public class MyString implements IString {

    public static boolean checkPrace(String a) {
        int i = 0;
        int j = a.length() - 1;
        while (i < j) {
            if (a.charAt(i) != a.charAt(j)) {
                return false;
            }
            j--;
            i++;
        }
        return true;
    }

    @Override
    public int f1(String str) {
        int count = 0;
        for (char c : str.toCharArray()) {
            if (Character.isDigit(c)) {
                int a = Integer.valueOf(c);
                if (a % 2 == 1) {
                    count++;
                }
            }
        }
        return count;
    }

    @Override
    public String f2(String a) {
        String[] t = a.split(" ");
        Set<String> al1 = new HashSet<>();
        ArrayList<String> al = new ArrayList<>();
        for (int i = 0; i < t.length; i++) {

            if (checkPrace(t[i])) {
                for (int j = i + 1; j < t.length; j++) {
                    if (t[i].equals(t[j])) {
                        al1.add(t[j]);
                    }
                }
            }
            al.add(t[i]);
        }
        for (String f : al1) {

            int h = al.indexOf(f);
            t[h] = "XX";
        }
        String string = t[0];
        for (int i = 1; i < t.length - 1; i++) {
            string += " " + t[i];
        }
        return string + " " + t[t.length - 1];

    }

}
Editor is loading...