(UVA) Deli Deli - Solution

Link to the problem: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=745&page=show_problem&problem=2174


import java.io.*;
import java.util.*;

class Main {
    public boolean receiveES(String s) {
        if (s.charAt(s.length()-1) == 'o'
                || s.charAt(s.length()-1) == 's'
                || s.charAt(s.length()-1) == 'x'
                || (s.charAt(s.length()-2) == 'c' && s.charAt(s.length()-1) == 'h')
                || (s.charAt(s.length()-2) == 's' && s.charAt(s.length()-1) == 'h')) {
            return true;
        }
        return false;
    }
   
    public void process() throws NumberFormatException, IOException {
        Scanner sc = new Scanner(System.in);
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));

        int numIrregularWords = sc.nextInt();
        int numQueries = sc.nextInt();
       
        Map<String, String> irregular = new HashMap<>();
        for (int i = 0; i < numIrregularWords; i++) {
            String s1 = sc.next();
            String s2 = sc.next();
            irregular.put(s1, s2);
        }       
                                 
        String notConsonants = "aeiouy";
        for (int i = 0; i < numQueries; i++) {
            String s = sc.next();
            if (irregular.containsKey(s)) {
                bw.write(irregular.get(s)+"\n");
            } else if (s.charAt(s.length()-1) == 'y' && notConsonants.indexOf(s.charAt(s.length()-2)) == -1) {
                bw.write(s.substring(0, s.length()-1)+"ies\n");
            } else if (receiveES(s)) {
                bw.write(s+"es\n");
            } else {
                bw.write(s+"s\n");
            }
        }                            

        bw.flush();
        bw.close();
               
        return;
    }
   
    public static void main(String[] args) throws NumberFormatException, IOException {
        Main m = new Main();
        m.process();
       
        System.exit(0);
    }
}

Comments

Popular posts from this blog

(Coderbyte) Dash Insert II - Solução

(Coderbyte) Run Length - Solução

(Coderbyte) Counting Minutes I - Solução