'finding the longest sequence of identical digits in an integer (Java)
I need to write a recursive method that takes an int as input and returns the longest sequence of identical numbers in it as an int (NOT as a string). counting sequences isn't the hard part but when given a number with a few sequences in it, I can't figure out how to return the correct value without counting all the sequnces but just the longest one. For now I wrote a code that only counts the length of a sequence:
public static int equalDigits (int num)
{
return equalDigits(num, num % 10);
}
private static int equalDigits (int num, int last)
{
if (num == 0)
return 0;
if (num % 10 == last)
{
last = num % 10;
return 1 + equalDigits(num/10, last);
}
last = num % 10;
return equalDigits(num/10, last);
}
And I really struggle on getting the rest done.
Solution 1:[1]
As @markspace said, it is impossible to recognise the longest sequence by the above codes. The codes would need to record possible new sequences that started in the middle, which means it might be necessary to have a storage for both target and pastIndex variables.
The below codes should solve the problem:
import java.util.Scanner;
public class Sequence {
public int countDigit(int input, int target, int height, int lastIndex, int count) {
if (input != 0) {
int currIndex = input % 10;
if (lastIndex != currIndex) {
// Checks if it is an index from a new starting sequence,
// remove previous count, then count++.
count = 0;
count++;
} else {
// same sequence, then count++.
count++;
}
// Below two lines for display purpose only.
System.out.println("Count of target [" + target + "]: " + height);
System.out.println("Count of currIndex [" + currIndex + "]: " + count);
if (count > height) {
// Checks if a new height in count is reached,
// modify count.
height = count;
if (target != currIndex) {
// Checks if the new height is by a different index,
// modify target.
target = currIndex;
}
}
lastIndex = currIndex;
return countDigit(input / 10, target, height, lastIndex, count);
} else {
// if no input is left,
return target;
}
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int input = scanner.nextInt();
Sequence sequence = new Sequence();
System.out.println("Result: " + sequence.countDigit(input, 0, 0, 0, 0));
}
}
And with input 112999456, the output is:
Count of target [0]: 0
Count of currIndex [6]: 1
Count of target [6]: 1
Count of currIndex [5]: 1
Count of target [6]: 1
Count of currIndex [4]: 1
Count of target [6]: 1
Count of currIndex [9]: 1
Count of target [6]: 1
Count of currIndex [9]: 2
Count of target [9]: 2
Count of currIndex [9]: 3
Count of target [9]: 3
Count of currIndex [2]: 1
Count of target [9]: 3
Count of currIndex [1]: 1
Count of target [9]: 3
Count of currIndex [1]: 2
Result: 9
Hope this answer helps you well.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | hokwanhung |
