|
| 1 | +\addcontentsline{toc}{subsection}{Anagram} |
| 2 | +\LARGE \circled{0} \textbf{Example Problem - Anagram} \normalsize |
| 3 | + |
| 4 | +{\itshape A Rag Man.} |
| 5 | + |
| 6 | +This problem is intended to get you familiar with how submission works. |
| 7 | + |
| 8 | +A pair of strings are called anagrams of each other if they share the same letters, but in a different order. |
| 9 | +Your task: determine whether given pairs of strings are anagrams. |
| 10 | + |
| 11 | +\vspace{8pt} |
| 12 | +\hrule |
| 13 | + |
| 14 | +\textbf{Input} |
| 15 | + |
| 16 | +The first line of the input contains an integer $t$, denoting the number of test cases. |
| 17 | + |
| 18 | +The next $t$ lines of the input consist of two space-separated strings $w_1$ and $w_2$. |
| 19 | + |
| 20 | +\textbf{Constraints} |
| 21 | + |
| 22 | +\begin{itemize} |
| 23 | + \item $1 \leq t \leq 10$ |
| 24 | + \item $1 \leq |w_1| = |w_2| \leq 13$ |
| 25 | + \item $w_1$ and $w_2$ will be uppercase. |
| 26 | +\end{itemize} |
| 27 | + |
| 28 | +\textbf{Output} |
| 29 | + |
| 30 | +$t$ lines each consisting of either "Yes" if the strings are anagrams, or "No" if they're not. |
| 31 | + |
| 32 | +\vspace{8pt} |
| 33 | +\hrule |
| 34 | + |
| 35 | +\textbf{Example} |
| 36 | + |
| 37 | +\begin{table}[h] |
| 38 | + \centering |
| 39 | + \begin{tabular}{|p{0.4\linewidth}|p{0.4\linewidth}|} |
| 40 | + \hline |
| 41 | + Input & Output \\ |
| 42 | + \hline |
| 43 | + 3 \newline BABA ABBA \newline ORANGE CHERRY \newline MOUNTAINEERS ENUMERATIONS & |
| 44 | + \text{} \newline Yes \newline No \newline Yes \\ |
| 45 | + \hline |
| 46 | + \end{tabular} |
| 47 | +\end{table} |
| 48 | + |
| 49 | +Note that we have one output line for each of the test cases. |
| 50 | + |
| 51 | +Feel free to give the problem a try yourself! |
| 52 | +However, the main goal of this is for you to understand the submission system, so I've given some example solutions on the next page, and ways to submit them. |
| 53 | +These are given for Python and Java, to show the differences. |
| 54 | + |
| 55 | +Once you feel comfortable, let's move onto the first real problem! |
| 56 | + |
| 57 | +\newpage |
| 58 | + |
| 59 | +\textbf{Solutions and Submission} |
| 60 | + |
| 61 | +\lstset{language=python} |
| 62 | +\begin{lstlisting} |
| 63 | +#!/usr/bin/env python |
| 64 | + |
| 65 | +t = int(input()) # Get number of test cases |
| 66 | + |
| 67 | +for _ in range(t): |
| 68 | + # Get the two words |
| 69 | + w1, w2 = input().split() |
| 70 | + |
| 71 | + # Convert to sorted character arrays |
| 72 | + s1 = sorted(w1) |
| 73 | + s2 = sorted(w2) |
| 74 | + |
| 75 | + # Compare the arrays |
| 76 | + if s1 == s2: |
| 77 | + print("Yes") |
| 78 | + else: |
| 79 | + print("No") |
| 80 | + |
| 81 | +\end{lstlisting} |
| 82 | + |
| 83 | +\small |
| 84 | +\texttt{./ProgcompCli 0 sol.py} on Mac or Linux, with a shebang. \\ |
| 85 | +\texttt{.\textbackslash ProgcompCli.exe 0 python.exe -{}-executable-args sol.py} on Windows (remove \texttt{.exe} otherwise). |
| 86 | + |
| 87 | +\vspace{8pt} |
| 88 | +\hrule |
| 89 | + |
| 90 | +\lstset{language=java} |
| 91 | +\begin{lstlisting} |
| 92 | +import java.io.*; |
| 93 | +import java.util.*; |
| 94 | + |
| 95 | +public class Solution { |
| 96 | + public static void main(String[] args) { |
| 97 | + // Allows us to read from stdin |
| 98 | + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 99 | + |
| 100 | + // Get number of test cases |
| 101 | + int t = 0; |
| 102 | + try { |
| 103 | + t = Integer.parseInt(br.readLine()); |
| 104 | + } catch (IOException ex) {} |
| 105 | + |
| 106 | + String word1, word2; |
| 107 | + for (int i = 0; i < t; i++) { // Repeat for number of test cases |
| 108 | + // Get words |
| 109 | + try { |
| 110 | + String line = br.readLine(); |
| 111 | + String[] split = line.split(" "); |
| 112 | + word1 = split[0]; |
| 113 | + word2 = split[1]; |
| 114 | + } catch (IOException ex) { continue; } |
| 115 | + |
| 116 | + // Sort arrays |
| 117 | + char[] arr1 = word1.toCharArray(); |
| 118 | + Arrays.sort(arr1); |
| 119 | + char[] arr2 = word2.toCharArray(); |
| 120 | + Arrays.sort(arr2); |
| 121 | + |
| 122 | + // Compare arrays |
| 123 | + if (Arrays.equals(arr1, arr2)) { |
| 124 | + System.out.println("Yes"); |
| 125 | + } else { |
| 126 | + System.out.println("No"); |
| 127 | + } |
| 128 | + } |
| 129 | + } |
| 130 | +} |
| 131 | +\end{lstlisting} |
| 132 | + |
| 133 | +\texttt{.\textbackslash ProgcompCli.exe 0 java -{}-executable-args Solution} on Windows (remove \texttt{.exe} otherwise). |
| 134 | +\\ Make sure to compile it first with \texttt{javac}! |
| 135 | + |
| 136 | +\normalsize |
0 commit comments