Check for anagrams

degree of difficulty: 2

A anagrom is a word or a phrase that can be created by rearranging the letters of another given word or phrase. We ignore white spaces and letter case. The all letters of "Desperation" can be rearranged to the phrase "A Rope Ends It".

Implement a Java program that checks to given Strings whether one is an anagram of the other.

Hint: Objects s of type String can be converted to lower case with s.toLowerCase(). s.toCharArray() returns the content of the String as an char-array. Note that char values can be used in Java everywhere where an int value is allowed.

Solution

Find the sum of digits for a sequence of digits

degree of difficulty: 3

Implement a Java function that calculates the sum of digits for a given char array consisting of the digits '0' to '9'. The function should return the digit sum as a long value.

Hint: you can do arithmetic with char-values like int-values in Java: the character's unicode is treated as an int-value. The digits '0' to '9' have a consecutive binary encoding without gaps. The expression '1' == '0' + 1, for instance, is true.

Solution

Find the smallest distance between two neighbouring numbers in an array

degree of difficulty: 2

Implement a Java function that finds two neighbouring numbers in an array with the smallest distance to each other. The function should return the index of the first number.

In the sequence 4 8 6 1 2 9 4 the minimum distance is 1 (between 1 and 2). The function should return the index 3 (of number 1).

Hint: Use Math.abs() to calculate the distance of two numbers.

Solution