-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathProblem_7.java
More file actions
56 lines (45 loc) · 1.92 KB
/
Problem_7.java
File metadata and controls
56 lines (45 loc) · 1.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package strings;
// * Problem Title: Count and Say
public class Problem_7 {
public static String countAndSay(int n) {
if (n == 1) {
return "1"; // Base case: first term is "1"
} else if (n == 2) {
return "11"; // Base case: second term is "11"
}
// Initialize result string with the base case
String result = "11";
// Generate terms from 3 to n based on previous terms
for (int termNumber = 3; termNumber <= n; termNumber++) {
StringBuilder nextTerm = new StringBuilder();
// Process previous term character by character
char previousChar = result.charAt(0); // Initialize with first character
int currentCharCount = 1; // Count of consecutive occurrences of previous character
for (int charIndex = 1; charIndex < result.length(); charIndex++) {
char currentChar = result.charAt(charIndex);
// If character changes, append count and character to next term
if (currentChar != previousChar) {
nextTerm.append(currentCharCount);
nextTerm.append(previousChar);
previousChar = currentChar;
currentCharCount = 1;
} else {
// If character is same, increment count
currentCharCount++;
}
}
// Append the last character and its count (if applicable)
nextTerm.append(currentCharCount);
nextTerm.append(previousChar);
// Update result string with the generated next term
result = nextTerm.toString();
}
return result;
}
public static void main(String[] args) {
// Example usage:
int n = 4;
String sequenceTerm = countAndSay(n);
System.out.println("Term " + n + ": " + sequenceTerm);
}
}