Day 6: Anagram Detector

100 Days of Code (48 Part Series)

1 Day 1: Two Sum
2 Day 2: Check Prime
44 more parts…
3 Day 3: Hex Colour
4 Day 4: Repeating Numbers
5 Day 5: Nearest Prime Numbers
6 Day 6: Anagram Detector
7 Day 7: Poker ID
8 Day 8: Bubble Sort
9 Day 9: Password Generator
10 Day 10: Array Search
11 Day 11: Rock, Paper, Scissors
12 Day 12: List Shuffle
13 Day 13: Validate ISBN
14 Day 14: HTML Markup Generator
15 Day 15: Pascal’s Triangle
16 Day 16: Pascal’s Pyramid
17 Day 17: d20 Dice Roller
18 Day 18: Base64
19 Day 19: Vigenère cipher
20 Day 20: Seven-segment display
21 Day 21: Insertion Sort
22 Day 22: Binary-Decimal
23 Day 23: Ducci sequence
24 Day 24: Shell Sort
25 Day 25: Calculator
26 Day 26: Merge Sort
27 Day 27: Bucket Sort
28 Day 28: Counting Sort
29 Day 29: Heap Sort
30 Day 30: Quick Sort
31 Day 31: Radix Sort
32 Day 32: Shaker Sort
33 Day 33: Stooge Sort
34 Day 34: Consuming a RESTful API with React
35 Day 35: Adding Machine
36 Day 36: React Form Validation
37 Day 37: Time Format Converter
38 Day 38: React Stopwatch
39 Day 39: Blackjack Checker
40 Day 40: Password Guessing Game
41 Day 41: String Case
42 Day 42: Tip Calculator
43 Day 43: Twitter Auto-reply
44 Day 44: React Currency Converter
45 Day 45: Phonebook
46 Day 46: Sudoku Generator
47 Day 47: Binary Search Tree
48 Day 48: AVL Tree

Detect if given word or phrase form an anagram.

import java.util.Arrays;

public class DaySix
{
    public static void main(String[] args)
    {
        String input = "Dormitory ? Dirty room\n" +
                "Conversation ? Voices rant on\n" +
                "The eyes ? They see\n" +
                "Inch ? Chins\n" +
                "Fourth of July ? Joyful Fourth\n" +
                "Elbow ? Below\n" +
                "The Morse Code ? Here come the dots\n" +
                "Astronomer ? Moon starer\n" +
                "Vacation Time ? I'm Not as Active\n" +
                "Listen ? Silent\n" +
                "Eleven plus two ? Twelve plus one";

        String[] line = input.split("\n");
        String[][] wordSet = new String[line.length][2];

        for(int i = 0; i < line.length; i++)
            for (int j = 0; j < 2; j++)
                wordSet[i][j] = line[i].split("\\?")[j].trim();


        for(String[] cur: wordSet)
            System.out.println( "\"" + cur[0] + "\"" + (isAnagram(cur[0], cur[1]) ? " is an anagram of " : " is NOT an anagram of ") +  "\"" + cur[1] + "\"");


    }

    private static boolean isAnagram(String str1, String str2)
    {
        char[] charArr1;
        char[] charArr2;

        charArr1 = str1.toLowerCase()
                .replaceAll("'", "")
                .replaceAll("\\s", "")
                .toCharArray();
        charArr2 = str2.toLowerCase()
                .replaceAll("'", "")
                .replaceAll("\\s", "")
                .toCharArray();

        Arrays.sort(charArr1);
        Arrays.sort(charArr2);

        return Arrays.equals(charArr1, charArr2);
    }
}

Enter fullscreen mode Exit fullscreen mode

100 Days of Code (48 Part Series)

1 Day 1: Two Sum
2 Day 2: Check Prime
44 more parts…
3 Day 3: Hex Colour
4 Day 4: Repeating Numbers
5 Day 5: Nearest Prime Numbers
6 Day 6: Anagram Detector
7 Day 7: Poker ID
8 Day 8: Bubble Sort
9 Day 9: Password Generator
10 Day 10: Array Search
11 Day 11: Rock, Paper, Scissors
12 Day 12: List Shuffle
13 Day 13: Validate ISBN
14 Day 14: HTML Markup Generator
15 Day 15: Pascal’s Triangle
16 Day 16: Pascal’s Pyramid
17 Day 17: d20 Dice Roller
18 Day 18: Base64
19 Day 19: Vigenère cipher
20 Day 20: Seven-segment display
21 Day 21: Insertion Sort
22 Day 22: Binary-Decimal
23 Day 23: Ducci sequence
24 Day 24: Shell Sort
25 Day 25: Calculator
26 Day 26: Merge Sort
27 Day 27: Bucket Sort
28 Day 28: Counting Sort
29 Day 29: Heap Sort
30 Day 30: Quick Sort
31 Day 31: Radix Sort
32 Day 32: Shaker Sort
33 Day 33: Stooge Sort
34 Day 34: Consuming a RESTful API with React
35 Day 35: Adding Machine
36 Day 36: React Form Validation
37 Day 37: Time Format Converter
38 Day 38: React Stopwatch
39 Day 39: Blackjack Checker
40 Day 40: Password Guessing Game
41 Day 41: String Case
42 Day 42: Tip Calculator
43 Day 43: Twitter Auto-reply
44 Day 44: React Currency Converter
45 Day 45: Phonebook
46 Day 46: Sudoku Generator
47 Day 47: Binary Search Tree
48 Day 48: AVL Tree

原文链接:Day 6: Anagram Detector

© 版权声明
THE END
喜欢就支持一下吧
点赞7 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容