JAVA PROGRAMMING Two words (spelt in uppercase) are similar if and only if all letters in the 1st word are in the 2nd word and all letters in the 2nd word are in the 1st word; including the same number of duplications. For example, ABBA and BABA are similar; however, BABA and BAAA are not similar. To check two words which are similar, we can use two integer arrays, each has 26 elements (as we have 26 letters), you can assume that 1st array element stores the number of letter ‘A’, 2nd array element stores the number of letter ‘B’, and so on. What you need to do now is to check whether the two integer arrays are equal to decide whether the two words are similar. We propose following UML for this question: The class TwoWords has two String instance variables. In this class, other than the constructors, accessor and mutator methods; we have two important methods: the countInfo method receives a String parameter (a word, for example) counts and stores the letters’ info in an integer array and returns this array; the areSimilar method is to check whether the two words are similar. - Main method drives the following interaction:
JAVA PROGRAMMING
Two words (spelt in uppercase) are similar if and only if all letters in the 1st word are in the 2nd word and all letters in the 2nd word are in the 1st word; including the same number of duplications. For example, ABBA and BABA are similar; however, BABA and BAAA are not similar.
To check two words which are similar, we can use two integer arrays, each has 26 elements (as we have 26 letters), you can assume that 1st array element stores the number of letter ‘A’, 2nd array element stores the number of letter ‘B’, and so on. What you need to do now is to check whether the two integer arrays are equal to decide whether the two words are similar.
We propose following UML for this question:
The class TwoWords has two String instance variables. In this class, other than the constructors, accessor and mutator methods; we have two important methods: the countInfo method receives a String parameter (a word, for example) counts and stores the letters’ info in an integer array and returns this array; the areSimilar method is to check whether the two words are similar.
- Main method drives the following interaction:
Trending now
This is a popular solution!
Step by step
Solved in 2 steps with 1 images