This question already has answers here:
Continue at first loop , inside the second loop
(7 answers)
Closed 1 year ago.
For this code kata I need to continue 2 for loops at the same time. How can I do that?
public class StringMerger {
public static boolean isMerge(String s, String part1, String part2) {
StringBuilder MergedWord = new StringBuilder("");
String Whole = part1+part2;
for(int j = 0; j < Whole.length(); j++){
for(int I = 0; I < part1.length(); I++){
if((Character.compare(s.charAt(j), part1.charAt(I)) == 0) && (j == I)){
MergedWord.append(s.charAt(I)+"");
continue;
}
else
break;
}
for(int i = 0; i < part2.length(); i++){
if((Character.compare(s.charAt(j), part2.charAt(i)) == 0) && (j == i)){
MergedWord.append(part2.charAt(i) + "");
continue;
}
else
break;
}
}
return s.equals(MergedWord.toString())? true : false;
}
}
I noticed when one of the for loops continue it only continues the internal loop, but labels would continue the upper loop and would be inefficient. Could I continue 2 for loops at the same time a.k.a continue the inner and upper loop in this nested for loop?
The task is to match each character in the target string, so you only need one loop, which iterates over the characters in the string. You do need two other loop variables to track how much of the two source strings have been used up.
My solution below is looping over three things at once: the target string and the two 'part' strings. The rate it moves over the 'part' strings isn't constant, but it does progress over them monotonically.
It wasn't clear to me whether the source strings could include extra characters not used in the target string. As the example didn't show any, I assumed not.
public class MergedStringChecker {
public static void main(String[] args) {
String target = "codewars";
String part1 = "cdw";
String part2 = "oears";
for (int i = 0, p1 = 0, p2 = 0; i < target.length(); ++i) {
if (p1 < part1.length() && target.charAt(i) == part1.charAt(p1)) {
++p1;
} else if (p2 < part2.length() && target.charAt(i) == part2.charAt(p2)) {
++p2;
} else {
throw new RuntimeException("No matching characters at index " + i);
}
}
}
}
The above solution is not Unicode safe if the string contains > 16 bit code points.
Try this.
public static boolean isMerge(String s, String part1, String part2) {
int length = s.length();
int length1 = part1.length();
int length2 = part2.length();
int i1 = 0, i2 = 0;
for (int i = 0; i < length; ++i)
if (i1 < length1 && s.charAt(i) == part1.charAt(i1))
++i1;
else if (i2 < length2 && s.charAt(i) == part2.charAt(i2))
++i2;
else
return false;
return i1 == length1 && i2 == length2;
}
public static void main(String[] args) throws IOException {
System.out.println(isMerge("codewars", "cdw", "oears"));
System.out.println(isMerge("codewars", "codewars", ""));
System.out.println(isMerge("codewars", "cdw", "oearsEXTRA"));
}
output:
true
true
false
you have an error on the first part loop you must append from the part not the s char
you don't need to chek the id of the part with the inital word (remove this check (j == I) and this one (j == i)
Use counter to avoid looping many times
Try this code :
public static boolean isMerge(String s, String part1, String part2) {
StringBuilder MergedWord = new StringBuilder("");
String Whole = part1+part2;
int counter1=0;
int counter2=0;
for(int w = 0; w < Whole.length(); w++){
for(int c1 = counter1; c1 < part1.length(); c1++){
if((s.charAt(w) == part1.charAt(c1)) ){
MergedWord.append(part1.charAt(c1)+"");
counter1++;
continue;
}
else
break;
}
for(int c2 = counter2; c2 < part2.length(); c2++){
if((s.charAt(w) == part2.charAt(c2)) ){
MergedWord.append(part2.charAt(c2) + "");
counter2++;
continue;
}
else
break;
}
}
return s.equals(MergedWord.toString())? true : false;
}
Related
I am writing a program to find the number of 'a' in a given string that is repeated. For example, the call findAmountA("aba", 7) means that it finds the number of 'a' in the string "aba" repeated for 7 characters. So "abaabaa" is the final string, so that call would return 5.
Without actually making the string 7 characters (so calls for 1,000,000 characters would not take so long), how would I use mathematics to accomplish this task? I cannot get further than this, as I have been trying to troubleshoot this for a while.
Keep in mind I am a beginner Java programmer (Student) and do not want to use any advanced/fancy syntax that I would not learn in high school. Thank you!
public class AInString {
public static void main(String[] args) {
boolean a = findAmountA("aba", 10) == 7;
boolean b = findAmountA("a", 100) == 100;
boolean c = findAmountA("abca", 10) == 5;
boolean d = findAmountA("", 10) == 0;
boolean e = findAmountA("abcaa", 1000000) == 600000;
boolean f = findAmountA("abc", 0) == 0;
boolean g = findAmountA("bcd", 10) == 0;
System.out.println(a && b && c && d && e && f && g);
}
public static int findAmountA(String word, int n) {
String s = word;
if(s.length() == 0 || aInWord(word) == 0) {
return 0;
}else {
int a = (aInWord(s));
return a;
}
}
public static int aInWord(String word) {
String s = word;
int aInWord = 0;
for(int i = 0; i < word.length(); i++) {
if(s.charAt(i) == 'a') {
aInWord++;
}
}
return aInWord;
}
}
Let's say your short string w has N copies of 'a' in it. Then the result string will consist of K copies of w followed by a possibility empty “tail” string.
The value of K can be determined by integer-dividing the number of 'a's in the target string by N. Then the number t of 'a's in the “tail” would be equal to the remainder of the division. Now you can print K copies of w followed by the shortest prefix of 'w' containing t 'a's.
Divide the target length by the input length: for the example:
7 / 3 = 2 remainder 1
2 the number of "full copies" of the entire input string you will use. So, find the number of "a"s in the entire string, multiply by 2.
You will take the first 1 character of the input to make up the remainder of the 7 characters. Count the number of "a"s in that substring.
Simply add these two numbers together.
int total = count(input, "a") * targetLength / input.length()
+ count(input.substring(0, targetLength % input.length()), "a");
where count(input, c) is some method to count the number of occurrences of c in input.
Now that you've counted the occurrences of a char a in a string word, you can count the occurrences of the char in the string extended n characters with:
return n / word.length() * aInWord(word) + aInWord(word.substring(0, n % word.length()));
n / word.length() gives the number of full repeats of the string that fit into n. Multiplying this by the count of aInWord(word) gives the count of a in repeats of word that fit cleanly into n.
The rest is a matter of finding the number of repeats in the substring of word that doesn't fit cleanly into n using the % modulus operator to find the size of the partial substring (if any). Adding the two counts together produces the total number of occurrences in the extended string.
Here is a clean version which avoids duplicate variables, extra conditionals and generalizes methods to maximize reusability:
class Main {
public static void main(String[] args) {
assert findAmount("aba", 10, "a") == 7;
assert findAmount("a", 100, "a") == 100;
assert findAmount("abca", 10, "a") == 5;
assert findAmount("", 10, "a") == 0;
assert findAmount("abcaa", 1000000, "a") == 600000;
assert findAmount("abc", 0, "a") == 0;
assert findAmount("bcd", 10, "a") == 0;
System.out.println("tests passed");
}
public static int findAmount(String word, int n, String target) {
if (word.length() == 0) {
return 0;
}
return n / word.length() * count(target, word) +
count(target, word.substring(0, n % word.length()));
}
public static int count(String target, String s) {
return s.length() - s.replace(target, "").length();
}
}
Try it!
I made some changes in your code, take a look:
public static void main(String[] args) {
int a = findAmountA("aba", 10); // 7
int b = findAmountA("a", 100); // 100;
int c = findAmountA("abca", 10); //5;
int d = findAmountA("", 10); //0;
int f = findAmountA("abc", 0); //0;
int g = findAmountA("bcd", 10); //0;
System.out.println(a + " " + b + " " + c + " " + d + " " + f + " " + g);
}
public static int findAmountA(String word, int n) {
if (word.length() < n) {
for (int i=0; i<word.length(); i++) {
while (word.length() < n) {
word = word + word.charAt(i);
break;
}
}
} else if (word.length() > n) {
for (int i=0; i<word.length(); i++) {
word = word.substring(0, n);
}
} else {
return aInWord(word);
}
return aInWord(word);
}
public static int aInWord(String word) {
String s = word;
int aInWord = 0;
for(int i = 0; i < word.length(); i++) {
if(s.charAt(i) == 'a') {
aInWord++;
}
}
Thank you all for your help, using substrings I found an answer:
public class AInString {
public static void main(String[] args) {
boolean a = findAmountA("aba", 10) == 7;
boolean b = findAmountA("a", 100) == 100;
boolean c = findAmountA("abca", 10) == 5;
boolean d = findAmountA("", 10) == 0;
boolean e = findAmountA("abcaa", 1000000) == 600000;
boolean f = findAmountA("abc", 0) == 0;
boolean g = findAmountA("bcd", 10) == 0;
System.out.println(a && b && c && d && e && f && g);
}
public static int findAmountA(String word, int n) {
String s = word;
if(s.length() == 0 || aInWord(s) == 0) {
return 0;
}else {
int a = aInWord(s)*(n/s.length());
int b = n % s.length();
return a + aInWord(s.substring(0, b));
}
}
public static int aInWord(String word) {
String s = word;
int aInWord = 0;
for(int i = 0; i < word.length(); i++) {
if(s.charAt(i) == 'a') {
aInWord++;
}
}
return aInWord;
}
}
My Question would be how can replace every 3rd ';' from a String a put a ',' at this position ?
for eg.:
String s = "RED;34;34;BLUE;44;44;GREEN;8;8;BLUE;53;53"
so that the String looks like:
RED;34;34,BLUE;44;44,GREEN;8;8,BLUE;53;53
I tried to solve it like this but i can't take a charAt(i) and replace it with an other char.
int counter =0;
for (int i=0;i<s.length();i++){
if(s.charAt(i) == ';'){
counter++;
}
if(counter ==3){
s.charAt(i)=',';
counter =0;
}
}
Normally some own effort is demanded from the question, but regex is hard.
s = s.replaceAll("([^;]*;[^;]*;[^;]*);", "$1,");
A sequence of 0 or more of not-semicolon followed by semicolon and such.
[^ ...characters... ] is some char not listed.
...* is zero or more of the immediately preceding match.
The match of the 1st group (...) is given in $1, so actually only the last semicolon is replaced by a comma.
You can use the modulo % operator to know the 3rd time something occurs. And a simple conversion between string and char array to do the rest:
class Main {
public static void main(String[] args) {
String s1 = "RED;34;34;BLUE;44;44;GREEN;8;8;BLUE;53;53";
char [] s = s1.toCharArray();
int j=0;
for(int i=0;i<s.length;i++){
if (s[i]==';') {
j++;
if(j % 3 == 0) {
s[i] = ',';
}
}
}
System.out.println(s);
}
}
There are many ways to do it, as I suggested in a comment. Here are implementations of the ones I suggested, but there are of course more ways than this.
The first is the simplest, from a code point of view, if you know regex. See answer by Joop Eggen for an explanation.
The second is likely the fastest, especially if you eliminate the % modulo operator by resetting j to 0 instead.
private static String usingRegex(String s) {
return s.replaceAll("([^;]*;[^;]*;[^;]*);", "$1,");
}
private static String usingCharArray(String s) {
char[] arr = s.toCharArray();
for (int i = 0, j = 0; i < arr.length; i++)
if (arr[i] == ';' && ++j % 3 == 0)
arr[i] = ',';
return new String(arr);
}
private static String usingStringBuilder(String s) {
StringBuilder sb = new StringBuilder(s);
for (int i = 0, j = 0; i < sb.length(); i++)
if (sb.charAt(i) == ';' && ++j % 3 == 0)
sb.setCharAt(i, ',');
return sb.toString();
}
private static String usingSubstring(String s) {
int i = -1, j = 0;
while ((i = s.indexOf(';', i + 1)) != -1)
if (++j % 3 == 0)
s = s.substring(0, i) + ',' + s.substring(i + 1);
return s;
}
Test
String s = "RED;34;34;BLUE;44;44;GREEN;8;8;BLUE;53;53";
System.out.println(usingRegex(s));
System.out.println(usingCharArray(s));
System.out.println(usingStringBuilder(s));
System.out.println(usingSubstring(s));
Output
RED;34;34,BLUE;44;44,GREEN;8;8,BLUE;53;53
RED;34;34,BLUE;44;44,GREEN;8;8,BLUE;53;53
RED;34;34,BLUE;44;44,GREEN;8;8,BLUE;53;53
RED;34;34,BLUE;44;44,GREEN;8;8,BLUE;53;53
Not that elegant like by #Joop, but probably simplier to understand:
String s = "RED;34;34;BLUE;44;44;GREEN;8;8;BLUE;53;53";
char[] chars = s.toCharArray();
int counter = 1;
for (int i = 0; i < chars.length; i++){
if (chars[i] == ';'){
if (counter == 3){
chars[i] = ','; // replace ';' with ','
counter = 1; // set counter to 1
}else {
counter++;
}
}
}
String output = String.valueOf(chars);
System.out.println(output); // RED;34;34,BLUE;44;44,GREEN;8;8,BLUE;53;53
Question is : The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)
P A H N
A P L S I I G
Y I R
And then read line by line: "PAHNAPLSIIGYIR"
I have written below code, appearantly it works fine, but I might miss some corner cases. Could you help me to find all the corner cases for this question on my answer?
public static String zigZagConversion(String s , int rowNum){
if (s == null){
throw new IllegalArgumentException();
}
if (rowNum == 1){
return s;
}
StringBuilder str = new StringBuilder();
int step = 2 * rowNum - 2 ;
for (int i = 0 ; i < rowNum ; i++){
if( i == 0 || i == rowNum -1){
for (int j = i ; j < s.length() ; j +=step){
str.append(s.charAt(j));
}
}
else{
int step2 = 2* (rowNum - i - 1);
int step3 = step - step2;
int k = i;
boolean flag = true;
while (k < s.length()){
str.append(s.charAt(k));
if(flag){
k += step2;
flag = false;
}
else{
k +=step3;
flag = false;
}
}
}
}
return str.toString();
}
It gives incorrect output for "PAYPALISHIRING", 4
P I N
A L S I G
Y A H R
P I
So the correct answer should be PINALSIGYAHRPI.
But your program gives PINALIGYAIHRNPI:
an "S" is missing, one extra "I" and one extra "N".
Your revised version is still incorrect, it gives PINALSIIGYAHNPI.
The problem is in the while loop in the middle.
You need to alternate the step counting,
setting the flag on and off.
Your mistake was to only set it off once, and never back on again.
str.append(s.charAt(k));
if (flag) {
k += step2;
flag = false;
} else {
k += step3;
flag = true;
}
With this correction, I believe your solution is correct. (I also added a minor improvement there, extracting the common str.append(s.charAt(k)); from the if-else branches.
My solution on leetcode forum:
https://leetcode.com/problems/zigzag-conversion/discuss/549451/Java-Solution-O(n)-with-algorithm
The mathematic algorithm for zigzag is:
originalDiff = numRows * 2 - 2;
If -> 'currRow' equals First or last lines
use the originalDiff (numRows * 2 - 2)
Else ->
For each new line:
upperDiff += 2,
lowerDiff -=2
Examples:
numRows =2 -> originalDiff = 2
PYAIHRN
APLSIIG
3 -> 4
P A H N
A P L S I I G
Y I R
numRows = 4 -> originalDiff = 6
P I N
A L S I G
Y A H R
P I
numRows = 5 -> originalDiff = 8
P H
A SI
Y I R
P L I G
A N
*/
My solution:
class Solution {
public String convert(String s, int numRows) {
if(numRows == 1) {
return s;
}
String newString = "";
int originalDiff = numRows * 2 - 2;
int diff = originalDiff;
int upperDiff = 0;
boolean isGoingDown = true;
int currIndex = 0;
int currRow = 0;
int startingIndex = 0;
for(int i = 0; i < s.length(); i++) {
System.out.println(currIndex);
newString += s.charAt(currIndex);
if(currRow == 0 || currRow == numRows - 1) {
currIndex += originalDiff;
} else {
if(isGoingDown) {
currIndex += diff;
isGoingDown = !isGoingDown;
} else {
currIndex += upperDiff;
isGoingDown = !isGoingDown;
}
}
if(currIndex >= s.length()) {
currRow++;
diff -= 2;
upperDiff += 2;
currIndex = currRow;
isGoingDown = true;
}
if(currRow == numRows) {
i = s.length();
}
}
return newString;
}
}
Zigzag conversion from leetcode in Javascript
Solution
const zigzag = (str, num) => {
if (num === 1) {
return str;
}
let check = true;
let result = [];
let i = 0;
while (i < str.length) {
result.push([]);
let j = 0;
while (j < num) {
if (check){
result[result.length-1].push(str[i]);
i++;
} else {
if (j == 0) {
result[result.length-1].push(null);
} else if (j === num-1) {
result[result.length-1].unshift(null);
} else {
result[result.length-1].unshift(str[i]);
i++;
}
}
j++;
}
check = !check;
}
let zigzag = [];
for (let k = 0; k < num; k++){
for(let l = 0; l < result.length; l++) {
zigzag.push(result[l][k]);
}
}
return zigzag.join("");
}
Example Input
zigzag("ABCD", 3)
Output
ABDC
Run
https://repl.it/#VinitKhandelwal/zigzag-conversion-javascript
Using HashMap
public String convert(String s, int numRows) {
if (numRows == 1){
return s;
}
StringBuilder result = new StringBuilder();
Map<Integer, StringBuilder> map = new HashMap<>();
for (int i = 0; i < numRows; i++) {
map.put(i,new StringBuilder());
}
int it = 0;
boolean flip = true;
for (int i = 0; i < s.length(); i++) {
if (flip) {
if(it<s.length()){
map.get(it).append(s.charAt(i));
it++;
}
} else {
map.get(it).append(s.charAt(i));
it--;
}
if (it + 1 == numRows || it == 0)
flip = !flip;
}
for (Map.Entry entry: map.entrySet()) {
result.append(entry.getValue());
}
return result.toString();
}
My Solution is traversing the string in the same way it is said in the problem, it is better to make string array of size numrows and the rest is storing the string character as it is in the logic,
you can keep the index and when that index is 0 i.e at the starting then we have to go till the end of the row and then except for first and last row, every array will have diagonal element.
So after traversing till the end then assign index = numrows - 2 and save in the respective array string and decrease and do the same till index >0 and then again traverse till the end row, do this and when we reach the end of the string then break from the loop.
and then concate all the string of string array in a new res string.
class Solution {
public String convert(String s, int n) {
if(n==1 || n>=s.length())
return s;
String[] a = new String[n]; //string array
int ind=0; // index for the string array
boolean flag=true;
int cnt=0; //to keep the counter till where we have traversed the string
while(true && flag)
{
if(ind==0)
{
for(int i=0;i<n;i++)
{
a[i] += s.charAt(cnt);
cnt++;
if(cnt==s.length())
{
flag=false;
break;
}
} // here it has reached the end so we assign here
ind = n-2;
}
else if(ind>0 && ind<n && flag)
{
a[ind] += s.charAt(cnt);
cnt++;
if(cnt==s.length())
{
flag=false;
break;
}
ind--; // to move diagonally up
}
}
String res = new String("");
for(int i=0;i<a.length;i++)
{
// System.out.println(a[i].substring(4));
res += a[i].substring(4);
}
return res;
}
}
Following is the simple solution.
class Solution:
def convert(self, s: str, numRows: int) -> str:
if numRows <= 1:
return s
res = ""
p = numRows * 2 - 2
temp = p
for i in range(0,numRows):
index = i
flag = 0
while index < len(s):
res = res + s[index]
if i == 0 or i == numRows-1:
index = index + p
else:
if flag == 0:
index = index + temp
flag = 1
else:
index = index + p-temp
flag = 0
temp = temp - 2
return res
zigzag-conversion Complete JavaScript-based solution
Created an Array of an array of row lengths. The main motive is to arrange characters in 2D array form and concat string row-wise.
var convert = function(s, numRows) {
let array =[],c=0,str='';
for(let row =0; row<numRows ; row++) {
array[row] = new Array();
}
while(c < s.length) {
for(let row =0; row<numRows ; row++) {
if((row+1)%numRows ==0) {
array[row].push(s[c]);
c++;
break;
} else {
array[row].push(s[c]);
c++;
}
}
for(let rr = numRows-2 ; rr>0;rr--) {
array[rr].push(s[c]);
c++;
}
}
for(let row =0; row<numRows ; row++) {
for(let i=0;i<array[row].length;i++){
if(array[row][i]){
str+=array[row][i]
}
}
}
return str
};
convert("PAYPALISHIRING",3)
This code supposed to output the longest run on which a character in a string has a consecutive runs of itself. Though the problem is that it outputs: 8 (which should be 5 instead). I just would like to ask what seems to be the problem regarding this code.
public class Sample {
public static void main(String[] args) {
String setofletters = "aaakkcccccczz"; /* 15 */
int output = runLongestIndex(setofletters);
System.out.println("Longest run that first appeared in index:" + output);
}
public static int runLongestIndex(String setofletters) {
int ctr = 0;
int ctrstor = 0;
int ii = 0;
int output = 0;
// loops until the last character in the string
for (int i = 0; i < setofletters.length() - 1; i++) {
// checks if the letter is the same to the next
if (setofletters.charAt(i) == setofletters.charAt(i++)) {
ctr++;
ii = i++;
// loops until the letter in the index is no longer equal
while (setofletters.charAt(i) == setofletters.charAt(ii)) {
ii++;
ctr++;
}
if (ctr > ctrstor) {
output = i;
}
// storing purposes
ctrstor = ctr;
}
// resets the counter
ctr = 0;
}
return output;
}
}
UPDATE Sorry, I misunderstood your question a bit, you need to make the following changes in your code to make it work.(lines with comments)
public static int runLongestIndex(String setofletters){
int ctr = 1; // every character is repeated at least once, so you should initialize it to 1, not 0
int ctrstor = 0;
int ii = 0;
int output = 0;
for (int i = 0; i < setofletters.length() - 1; i++) {
if (i < setofletters.length() - 1 && setofletters.charAt(i) == setofletters.charAt(i+1)) { // i++ is not same as i+1
ctr++;
ii = i+1; // i++ is not same as i+1
while (setofletters.charAt(i) == setofletters.charAt(ii)) {
ii++;
ctr++;
}
if (ctr > ctrstor) {
output = i;
}
ctrstor = ctr;
}
ctr = 1; // for the same reason I mentioned above
}
return output;
}
EDIT : the easiest way to write your code is :
public static int runLongestIndex(String setofletters){
int ctr = 1;
int output = 0;
int j=0;
for(int i=0; i<setofletters.length()-1;i++){
j=i;
while(i <setofletters.length()-1 && setofletters.charAt(i)==setofletters.charAt(i+1)){
i++;
ctr++;
}
if(ctr>output){
output=j;
}
ctr = 1;
}
return output;
}
Why are you assigning i to output? You should assign ctr to output.
change
if(ctr>ctrstor){
output=i;
}
to
if(ctr>ctrstor){
output=ctr;
}
and also I think you should change
if(setofletters.charAt(i)==setofletters.charAt(i++))
to
if(i<setofletters.length()-1 && setofletters.charAt(i)==setofletters.charAt(i+1)){
and you should intialize ctr to 1 but not 0 because every character is repeated at least once.
I'll give you a Scala implementation for that problem.
Here it is the automatic test (in BDD style with ScalaTest)
import org.scalatest._
class RichStringSpec extends FlatSpec with MustMatchers {
"A rich string" should "find the longest run of consecutive characters" in {
import Example._
"abceedd".longestRun mustBe Set("ee", "dd")
"aeebceeedd".longestRun mustBe Set("eee")
"aaaaaaa".longestRun mustBe Set("aaaaaaa")
"abcdefgh".longestRun mustBe empty
}
}
Following is the imperative style implementation, with nested loops and mutable variables as you would normally choose to do in Java or C++:
object Example {
implicit class RichString(string: String) {
def longestRun: Set[String] = {
val chunks = mutable.Set.empty[String]
val ilen = string.length
var gmax = 0
for ((ch, curr) <- string.zipWithIndex) {
val chunk = mutable.ListBuffer(ch)
var next = curr + 1
while (next < ilen && string(next) == ch) {
chunk += string(next)
next = next + 1
}
gmax = chunk.length max gmax
if (gmax > 1) chunks += chunk.mkString
}
chunks.toSet.filter( _.length == gmax )
}
}
}
Following is a functional-style implementation, hence no variables, no loops but tail recursion with result accumulators and pattern matching to compare each character with the next one (Crazy! Isn't it?):
object Example {
implicit class RichString(string: String) {
def longestRun: Set[String] = {
def recurse(chars: String, chunk: mutable.ListBuffer[Char], chunks: mutable.Set[String]): Set[String] = {
chars.toList match {
case List(x, y, _*) if (x == y) =>
recurse(
chars.tail,
if (chunk.isEmpty) chunk ++= List(x, y) else chunk += y,
chunks
)
case Nil =>
// terminate recursion
chunks.toSet
case _ => // x != y
recurse(
chars.tail,
chunk = mutable.ListBuffer(),
chunks += chunk.mkString
)
}
}
val chunks = recurse(string, mutable.ListBuffer(), mutable.Set.empty[String])
val max = chunks.map(_.length).max
if (max > 0) chunks.filter( _.length == max ) else Set()
}
}
}
For example, for the given "aeebceeedd" string, both implementations above will build the following set of chunks (repeating characters)
Set("ee", "eee", "dd")
and they will filter those chunks having the maximum length (resulting "eee").
This code should work for any length of string sequence.
public class LongestStringSequqnce {
static String myString = "aaaabbbbcccchhhhiiiiibbbbbbbbbccccccc";
static int largestSequence = 0;
static char longestChar = '\0';
public static void main(String args[]) {
int currentSequence = 1;
char current = '\0';
char next = '\0';
for (int i = 0; i < myString.length() - 1; i++) {
current = myString.charAt(i);
next = myString.charAt(i + 1);
// If character's are in sequence , increase the counter
if (current == next) {
currentSequence += 1;
} else {
if (currentSequence > largestSequence) { // When sequence is
// completed, check if
// it is longest
largestSequence = currentSequence;
longestChar = current;
}
currentSequence = 1; // re-initialize counter
}
}
if (currentSequence > largestSequence) { // Check if last string
// sequence is longest
largestSequence = currentSequence;
longestChar = current;
}
System.out.println("Longest character sequence is of character "
+ longestChar + " and is " + largestSequence + " long");
}
}
Source : http://www.5balloons.info/program-java-code-to-find-longest-character-sequence-in-a-random-string/
if(ctr>ctrstor){
output=i;
}
//storing purposes
ctrstor=ctr;
This looks like the problem. So if you find 8 consecutive characters, it will set output to 8, and proceed. The next time thru, it finds 3 consecutive characters, so doesn't set output, but sets ctrstor. Next time thru it finds 4 consecutive characters, and this will set output to 4
There are few traps in the code that your logic felt in:
Code incorrectly assumes that there is always next character to compare current one.
This fails for string like "a" or the last character in any string.
Code does not store the max count of characters but only the max index (i).
MaxCount is needed to compare the next chars sequence size.
Loop for and loop while repeat the same subset of characters.
Also variable name style makes it harder to understand the code.
After correcting above
public static int runLongestIndex(String setofletters) {
int maxCount = 0;
int maxIndex = 0;
// loops each character in the string
for (int i = 0; i < setofletters.length() - 1; ) {
// new char sequence starts here
char currChar = setofletters.charAt(i);
int count = 1;
int index = i;
while ( (index < setofletters.length() - 1) &&
(currChar == setofletters.charAt(++index)) ) {
count++;
}
if (count > maxCount) {
maxIndex = i;
maxCount = count;
}
i = index;
}
return maxIndex;
}
See Java DEMO
I think you don't need an internal loop:
public static int runLongestIndex(String setofletters) {
if (setofletters == null || setofletters.isEmpty()) {
return -1;
}
int cnt = 1;
char prevC = setofletters.charAt(0);
int maxCnt = 1;
//char maxC = prevC;
int maxRunIdx = 0;
int curRunIdx = 0;
for (int i = 1; i < setofletters.length(); i++){
final char c = setofletters.charAt(i);
if (prevC == c) {
cnt++;
} else {
if (cnt > maxCnt) {
maxCnt = cnt;
//maxC = prevC;
maxRunIdx = curRunIdx;
}
cnt = 1;
curRunIdx = i;
}
prevC = c;
}
if (setofletters.charAt(setofletters.length() - 1) == prevC) {
if (cnt > maxCnt) {
//maxC = prevC;
maxCnt = cnt;
maxRunIdx = curRunIdx;
}
}
return maxRunIdx;
}
and this code:
System.out.println(runLongestIndex("aaakkcccccczz"));
gives you
5
This is how a "colleague" of mine is understanding to write readable code in order to solve this problem, even if this is working :)
public static int count (String str) {
int i = 0;
while(i < str.length()-1 && str.charAt(i)==str.charAt(i+1))
i ++;
return ++i;
}
public static int getLongestIndex(String str){
int output = 0;
for(int i=0, cnt = 1, counter = 0 ; i<str.length() - 1;i += cnt, cnt = count(str.substring(i)), output = (counter = (cnt > counter ? cnt : counter)) == cnt ? i : output);
return output;
}
int indexOfLongestRun(String str) {
char[] ar = str.toCharArray();
int longestRun = 0;
int lastLongestRun = 0;
int index = 0;
for(int i = ar.length-1; i>0; i--){
if(ar[i] == ar[i-1]){
longestRun++;
}else{
if(longestRun > lastLongestRun){
lastLongestRun = longestRun;
longestRun = 0;
index = i;
}
}
}
return index;
Well, the solution a bit depends on the additional requirements. Here is the code which returns the FIRST longest sequence of a repeated character int the given string, meaning if you have a second sequence with the same length you never get it out :(. But still, this is a simple and clear solution here, so good news - it works! :)
string = 'abbbccddddddddeehhhfffzzzzzzzzdddvyy'
longest_sequence = ''
for i in range(len(string)):
is_sequence = True
ch_sequence = ''
while is_sequence:
ch_sequence += string[i]
if i+1 < len(string) and string[i]==string[i+1]:
i += 1
else:
is_sequence = False
if len(ch_sequence) > len(longest_sequence):
longest_sequence = ch_sequence
print (longest_sequence)
#Paolo Angioletti already provided an answer using Scala, but it's more complicated than it needs to be. The idea is not very different from Run-length encoding. Time complexity O(n).
def longestConsecutive(s: String): (Char, Int) = {
Iterator.iterate(('\u0000', 0, 0)) { case (ch, longestRun, i) =>
val run = (i until s.length)
.takeWhile(s(_) == s(i))
.size
if (run > longestRun) (s(i), run, i + run)
else (ch, longestRun, i + run)
}
.dropWhile(i => s.isDefinedAt(i._3))
.take(1)
.map(x => (x._1, x._2))
.next()
}
Tested with:
("s", "ch", "n")
----------------
("", '\u0000', 0),
("a", 'a', 1),
("aabcddbbbea", 'b', 3),
("abcddbbb", 'b', 3),
("cbccca", 'c', 3)
#include <iostream>
#include<algorithm>
using namespace std;
int main() {
string s="abbcccccbbffffffffff";
//cin>>s;
int count=1;
int maxcount=1;
int start=0;
int ps=0;
for (int i=0;i<s.size()-1;i++)
{
if(s.at(i)==s.at(i+1))
{
count +=1;
maxcount=max(maxcount,count);
}
else
{
ps=max(ps,start+count);
count =1;
start=i;
}
}
for(int i=1;i<=maxcount;i++)
{
cout<<s.at(i+ps);
}
// your code goes here
return 0;
}
This is the simplest I can think of and it will print the number of the longest sequenced identical characters in a one line string.
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String s = scanner.nextLine();
scanner.close();
int count = 0;
int curCount = 1;
for (int i = 0; i < s.length() -1; i++) {
if (s.charAt(i) == s.charAt(i + 1)) {
curCount++;
if (curCount > count) {
count = curCount;
}
}else {
if (curCount > count) {
count = curCount;
}
curCount = 1;
}
}
System.out.println(count);
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am trying to write a really simple poker game. I'm just using the non-face cards, 2-9, without suites or anything of the like. I'm trying to figure out how to write a method that determines if five cards are a full house, which is a pair and a 3 of a kind. I have the user input 5 integers that represent card values and store them in a single array. I tried writing something like this:
public static boolean containsFullHouse(int[] hand)
{
for (int i = 0; i < hand.length; i++){
int count = 0;
for (int j = 0; j < hand.length; j++){
if (hand[i] == hand[j]){
count++;}
if (count == 3){
return true;}
}
}
for(int i = 0; i < hand.length; i++){
for(int j = i + 1; j < hand.length; j++){
if(hand[i] == hand[j]){
return true;}
}
}
}
return false;
}
You need to count the occurrences of each number, and create what is called a cardinality map. Then the cardinalities must be (2,3) or (3,2). If not using guava or Apache Commons Collections (which contain convenience methods to do this), this can be done in the following way:
public static boolean isFullHouse(final int[] input) {
if (input.length != 5) { throw new IllegalArgumentException("need 5 integers"); }
Map<Integer, Integer> cardinalityMap = new HashMap<>();
for (int i : input) {
if (cardinalityMap.containsKey(i)) {
cardinalityMap.put(i, cardinalityMap.get(i) + 1);
}
else {
cardinalityMap.put(i, 1);
}
}
if (cardinalityMap.size() != 2) { return false; }
Collection<Integer> occurences = cardinalityMap.values();
int first = occurences.iterator().next();
return first == 2 || first == 3;
}
I would use CollectionUtils.getCardinalityMap from Apache Commons to do this
public static void main(String[] args) {
Integer[] fullHouse = new Integer[]{7, 7, 7, 4, 4};
Integer[] notFullHouse = new Integer[]{2, 2, 2, 2, 3};
Integer[] notFullHouse2 = new Integer[]{1, 4, 2, 2, 3};
System.out.println(isFullHouse(fullHouse));
System.out.println(isFullHouse(notFullHouse));
System.out.println(isFullHouse(notFullHouse2));
}
private static boolean isFullHouse(Integer[] cards){
Map<Integer,Integer> cardinalityMap = CollectionUtils.getCardinalityMap(Arrays.asList(cards));
if(cardinalityMap.size() == 2) {
if (cardinalityMap.values().containsAll(Arrays.asList(2, 3))) {
return true;
}
return false;
}
return false;
}
Problems:
You're checking index i twice, although correct (since you check for count == 3), it's unnecessary.
You're also returning before you check the other 2.
The second loop will return true since it will find the numbers from the previous loop.
If you sort them, you can simply check whether the two pairs of cards on both sides are the same and check whether the middle card is the same as either one. So something like this:
Arrays.sort(hand);
return (hand[0] == hand[1] && hand[3] == hand[4] &&
(hand[2] == hand[1] || hand[2] == hand[3]));
Alternatively, if you want to fix your function:
public static boolean containsFullHouse(int[] hand)
{
// a variable that keeps track of one of the 3-of-a-kind indices (used in 2-of-a-kind check)
int pos = -1;
for (int i = 0; i < hand.length && pos == -1; i++){
// start count at one instead
int count = 1;
// start j from next position rather than 0
for (int j = i+1; j < hand.length && pos == -1; j++){
if (hand[i] == hand[j]) {
count++;
}
if (count == 3) {
pos = i;
}
}
}
// if we didn't find 3-of-a-kind, return false
if (pos == -1)
return false;
// look for 2-of-a-kind
for(int i = 0; i < hand.length; i++){
// exclude elements that match one of the 3-of-a-kind
if (hand[i] != hand[pos]){
for(int j = i + 1; j < hand.length; j++){
if(hand[i] == hand[j]){
return true;
}
}
}
}
return false;
}
A full house consists of 2 different integers, so keep a counter for both. It also needs to keep track of the 2 different values. If you combine this you get something like this:
public static boolean containsFullHouse(int[] hand)
{
int value1 = -1, count1 = 0;
int value2 = -1, count2 = 0;
for (int i = 0; i < hand.length; i++) {
if(hand[i] == value1) {
// Found another value1 card
count1++;
} else if(hand[i] == value2) {
// Found another value2 card
count2++;
} else if(value1 == -1) {
// Found a new card, store as value1
value1 = hand[i];
count1++;
} else if(value2 == -1) {
// Found a new card, store as value2
value2 = hand[i];
count2++;
} else {
// Found a third card, so it cannot be a full house!
return false;
}
}
if(value2 == -1) {
// Found 'five of a kind'?!
return false;
}
// Check if it is a full house
return (count1 == 3 && count2 == 2) || (count1 == 2 && count2 == 3;)
}