| about | help | code help+videos | done | prefs |
Given a String str, find the largest distance (in terms of index positions in the input) between any letter of the alphabet and the letter after or before it in the alphabet. For the input "cab" the output is 2, the distance between "c" and "b". That is longer than the distance between "a" and "b" (1). For the input "bug" the output is 0 because there are no letters that are "one away" from other letters in the input. Preconditions: All inputs will just be a collection of lowercase letters, and str.length() > 0. Postconditions: outputs are always positive or zero. HINTS You will probably want to use str.charAt(i) where i is an index of str to extract a single char from the String. True, char is not one of the AP exam data types, but it's usefully able to be treated as a number. For example:char a = 'a'; char b = 'b'; int diff = b - a; // diff holds 1 because 'b' is 1 more than 'a'You will probably need a "nested loop" structure in your code, to compare every character to every other character of the input. apcsaStringsMaxDistance("cab") → 2 apcsaStringsMaxDistance("bug") → 0 apcsaStringsMaxDistance("belated") → 5 ...Save, Compile, Run (ctrl-enter) |
Progress graphs:
Your progress graph for this problem
Random user progress graph for this problem
Random Epic Progress Graph
Difficulty: 300
Copyright Nick Parlante 2017 - privacy