I want to take two strings and alternate the characters into a new string using a for method.
Example: "two" and "one"
Result: "townoe"
This is what I have so far, and I really don't know how to finish it.
public class Alternator {
String alternate(String a, String b) {
String s = "";
for (int i = 0; i < a.length(); i++) {
s += i;
System.out.println(s);
}
return null;
}
}
public class Alternator{
public static String alternate(String a, String b){
String s = "";
int i = 0;
while (i < a.length() && i < b.length()){
s += a.charAt(i) +""+ b.charAt(i);
i++;
}
while (i < a.length() ){
s += a.charAt(i);
i++;
}
while (i < b.length()){
s += b.charAt(i);
i++;
}
return s;
}
public static void main(String[] args){
String a = "two", b = "one";
String s = Alternator.alternate(a,b);
System.out.println(s);
}
}
To use for loop instead of while loop, simply remove all while lines with for lines like the following, then remove the i++ line from each while loop
for(; i < a.length() && i < b.length(); i++){
//the inside of the loop MINUS THE LINE i++
}
for(; i < a.length(); i++){
//the inside of the loop MINUS THE LINE i++
}
for(; i < b.length(); i++){
//the inside of the loop MINUS THE LINE i++
}
Here is some compact way of doing that:
String alternate(String a, String b) {
StringBuilder builder = new StringBuilder();
int smallerStringLength = Math.min(a.length(), b.length());
for (int i = 0; i < smallerStringLength; i++) {
builder.append(a.charAt(i));
builder.append(b.charAt(i));
}
return builder.toString();
}
Or even more optimized:
String alternate(String first, String second) {
char[] firstChars = first.toCharArray();
char[] secondChars = second.toCharArray();
int smallerCharsCount = Math.min(firstChars.length, secondChars.length);
StringBuilder builder = new StringBuilder(smallerCharsCount * 2);
for (int i = 0; i < smallerCharsCount; i++) {
builder.append(firstChars[i]);
builder.append(secondChars[i]);
}
return builder.toString();
}
This will work if string are of same length or of the different lengths.
static void mergeStrings(String a, String b) {
StringBuilder mergedBuilder = new StringBuilder();
char[] aCharArr = a.toCharArray();
char[] bCharArr = b.toCharArray();
int minLength = aCharArr.length >= bCharArr.length ? bCharArr.length : aCharArr.length;
for (int i=0; i<minLength; i++) {
mergedBuilder.append(aCharArr[i]).append(bCharArr[i]);
}
if(minLength < aCharArr.length) {
mergedBuilder.append(a.substring(minLength));
}
else{
mergedBuilder.append(b.substring(minLength));
}
Systemout.println(mergedBuilder.toString());
}
Assuming that the two strings are the exact same length, you can do the following. If they are different length, then currently your prompt doesn't say how you want the resultant string to be set up.
public class Alternator {
String alternate(String a, String b) {
String s = "";
for (int i = 0; i < 2*a.length(); i++) {
if (i%2==0) // modular arithmetic to alternate
s += a.charAt(i/2); // Note the integer division
else
s += b.charAt(i/2);
}
System.out.println(s);
return s;
}
}
Alternatively, even easier, but the index i doesn't mark the length of your string s:
public class Alternator {
String alternate(String a, String b) {
String s = "";
for(int i = 0; i < a.length(); i++){
s += a.charAt(i);
s += b.charAt(i);
}
return s;
}
}
Use this:
String alternate(String a, String b){
StringBuilder builder = new StringBuilder();
final int greaterLength = a.length() > b.length() ? a.length() : b.length();
for(int i = 0; i < greaterLength; i++){
if (i < a.length()) {
builder.append(a.charAt(i));
}
if (i < b.length()) {
builder.append(b.charAt(i));
}
}
return builder.toString();
}
It uses the String.charAt method to obtain letters, and a StringBuilder to create the string.
(When given two strings of non-equal length, this returns an alternation of the first two chars, and then does just the remaining string. EG: Hello and Hi --> HHeillo)
According to the comments I've read, you are having trouble understanding for loops, and how to use them with strings.
For loops are most often used to iterate over arrays, or to perform a task a given number of times.
for (int i = 0; i < 5; i++) {
System.out.println(i);
}
This would give the output
0
1
2
3
4
For loops start at the value of the initializer, the first thing you put in int i = 0;
They then check the expression, the second part of the for loop, and if it returns true, it executes all of the code inside the braces. i < 5;
Once it has done that, it runs the incrementor, the last part of the for loop. i++
After that, it checks the expression again. I guess you can see where this is going. Until the expression returns false, everything inside the curly braces of the for loop gets executed over and over again.
Strings can be iterated over with a for loop, but you can't reference it like an array using array[index]. You have to either convert it into an array, using .toCharArray() on your String, and return the result to an empty char array char[], or use the .charAt(index) method on your string.
This code will go over a string, and output each character, one by one:
for (int i = 0; i < myString.length(); i++) {
System.out.println(myString.charAt(i));
}
If the string had a value of "Hello", the output would be:
H
e
l
l
o
Using this, instead of outputting the characters using System.out.println();, we can put them into an empty string, using +=:
myOtherString += myString.charAt(i);
That means, if we want to go over two Strings at a time, and alternate them, like you do, we can iterate over two strings at the same time, and add them to a new string:
myAlternatedString += myString.charAt(i);
myAlternatedString += myOtherString.charAt(i);
if MyString was still "Hello" and myOtherString was "World", the new string would be:
Hweolrllod
following code reads 2 different inputs and merges into a single string.
public class PrintAlternnateCharacterString {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String a = in.next();
String b = in.next();
String mergedString = "";
int lenA = a.length();
int lenB = b.length();
if (lenA >= lenB) {
for (int i = 0; i < lenA; i++) {
if (i < lenB) {
mergedString += a.charAt(i) + "" + b.charAt(i);
} else {
mergedString += a.charAt(i);
}
}
}
if (lenB > lenA) {
for (int i = 0; i < lenB; i++) {
if (i < lenA) {
mergedString += a.charAt(i) + "" + b.charAt(i);
} else {
mergedString += b.charAt(i);
}
}
}
System.out.println("the merged string is-->" + mergedString);
}
}
public static String stringConcate(String str1,String str2){
String str3="";
if(str1!=null && str2!=null && !str1.isEmpty() && !str2.isEmpty()){
if(str1.length()==str2.length()){
for(int i=0;i<=str1.length()-1;i++){
str3+=str1.charAt(i);
str3+=str2.charAt(i);
}
}
if(str1.length()>str2.length()){
for(int i=0;i<=str1.length()-1;i++){
str3+=str1.charAt(i);
if(i<str2.length()){
str3+=str2.charAt(i);
}
}
}
if(str2.length()>str1.length()){
for(int i=0;i<=str2.length()-1;i++){
if(i<str1.length()){
str3+=str1.charAt(i);
}
str3+=str2.charAt(i);
}
}
}
return str3;
}
String str1 = "one"; String str2 = "two";
StringBuilder sb = new StringBuilder();
int i = 0;
for (; i < str1.length() && i < str2.length(); i++) {
sb.append(str1.charAt(i)).append(str2.charAt(i));
}
for(; i < str1.length(); i++) {
sb.append(str1.charAt(i));
}
for(; i < str2.length(); i++) {
sb.append(str2.charAt(i));
}
System.out.println("result = " + sb.toString());// otnweo
This will handle for different length too
This could be donw with very simple if...else.
public static void main(String... args) {
int[] one = { 1, 2, 3 };
int[] two = { 44, 55, 66, 77, 88 };
System.out.println(Arrays.toString(alternate(one, two)));
}
public static int[] alternate(int[] one, int[] two) {
int[] res = new int[one.length + two.length];
for (int i = 0, j = 0, k = 0; i < res.length; i++) {
if (i % 2 == 0)
res[i] = j < one.length ? one[j++] : two[k++];
else
res[i] = k < two.length ? two[k++] : one[j++];
}
return res;
}
Output:
[1, 44, 2, 55, 3, 66, 77, 88]
Related
I'm trying to get a function to take a String and change every 5th character to a 'z'. For whatever reason the first character changed is always the 6th character and I can't figure out why. There's also an error if the string is divisible by 5, which is either because it starts on the 6th character or because my loop iteration goes one too far.
I've messed with the loop iteration but just can't see why it's starting on the 6th character.
public static String change5thPosition(String s) {
int fives = (int) Math.floor((s.length() / 5));
System.out.println(fives);
char[] chars = s.toCharArray();
for (int i = 0; i <= fives; i++) {
if (i != 0)
chars[i * 5] = 'z';
}
String returnString = new String(chars);
return returnString;
}
Input example:
all mimsy were the baragroves
Expected output:
all zimsyzwerezthe zbragzoves
Actual output:
all mzmsy zere zhe bzragrzves
Edit: Thanks for the help. here is my updated code:
public static String change5thPosition(String s) {
char[] chars = s.toCharArray();
for (int i = 4; i < s.length(); i = i + 5) {
chars[i] = 'z';
}
String returnString = new String(chars);
return returnString;
}
Your code changes:
i=1 : chars[1 * 5 = 5]
i=2 : chars[2 * 5 = 10]
... and so on.
But since arrays are indexed starting with 0, you actually want to change the characters at 4,9,14, ....
So just subtract 1.
chars[i*5 - 1] = 'z';
These "off by one" errors are quite common in programming, and you'll learn to anticipate them.
On a side note, there's no need for:
for(int i=0; i<=fives; i++){
if (i!=0) {
...
}
}
... when you can just start i at 1:
for(int i=1; i<=fives; i++){
...
}
You could also avoid having to multiply, by incrementing in fives:
for(int i=4, i < chars.length; i += 5) {
char[i] = 'z';
}
Simply add a -1 to your index.
public static String change5thPosition(String s){
int fives = (int)Math.floor((s.length()/5));
System.out.println(fives);
char[] chars = s.toCharArray();
for(int i=0; i<=fives; i++){
if (i!=0)
chars[i*5 -1]='z';
}
String returnString = new String(chars);
return returnString;
}
A simple loop will do the Job. Take care of the index and avoid complex calculations.
String n = "all mimsy were the baragroves";
char[] s = n.toCharArray();
for(int i = 4;i<s.length;i+=5){
s[i]='z';
}
System.out.println(s);
You can try this code.
public class Simple {
public static void main(String args[]) {
System.out.println(change5thPosition("all mimsy were the baragroves"));
}
public static String change5thPosition(String s) {
int fives = (int) Math.floor((s.length() / 5));
System.out.println(fives);
char[] chars = s.toCharArray();
int index = 1;
StringBuilder myvar = new StringBuilder();
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (index == 5) {
myvar.append("z");
index = 1;
} else {
myvar.append(c);
index++;
}
}
return myvar.toString();
}
}
I am working on this magical sub-string problem.
Magical binary strings are non-empty binary strings if the following two conditions are true:
The number of 0's is equal to the number of 1's.
For every prefix of the binary string, the number of 1's should not be less than the number of 0's.
I got stuck on how to proceed further in my Java program.
Here is my program:
static String findLargest(String str) {
String[] splits = str.split("");
Set<String> set = new LinkedHashSet<String>();
for (int i = 0; i < splits.length; i++) {
if (splits[i].equals("0")) {
continue;
}
int zeros = 0;
int ones = 0;
StringBuilder sb = new StringBuilder("");
for (int j = i; j < splits.length; j++) {
if (splits[j].equals("0")) {
zeros++;
} else {
ones++;
}
sb.append(splits[j]);
if (zeros == ones && ones >= zeros) {
set.add(sb.toString());
}
}
}
set.remove(str);
List<String> list = new ArrayList<String>(set);
System.out.println(list);
return null;
}
Using this program I am able to get the magical sub-strings for the given input String 11011000 as [10, 101100, 1100] in my list variable.
Now from here I am struggling how to remove the invalid entry of 101100 from my list and then use the elements 10, 1100 to swap from my input 11011000 to get the final result as 11100100
Also please guide me if there is any other alternate approach.
If your question is about only eliminating the unwanted "101100" from the result, here is the answer
import java.util.ArrayList;
import java.util.HashMap;
import java.lang.*;
import java.util.Set;
import java.util.*;
public class HelloWorld{
public static void main(String []args){
findLargest("11011000");
}
public static String findLargest(String str) {
String[] splits = str.split("");
Set<String> set = new LinkedHashSet<String>();
for (int i = 0; i < splits.length; i++) {
if (splits[i].equals("0")) {
continue;
}
int zeros = 0;
int ones = 0;
StringBuilder sb = new StringBuilder("");
for (int j = i; j < splits.length; j++) {
if (splits[j].equals("0")) {
zeros++;
} else {
ones++;
}
sb.append(splits[j]);
if (zeros == ones && ones >= zeros) {
set.add(sb.toString());
j = i +1; // RESET THE INDEX ELEMENT TO SKIP THE SUBSTRING FROM CONSIDERATION
break; // BREAK FROM THE LOOP
}
}
}
set.remove(str);
List<String> list = new ArrayList<String>(set);
System.out.println(list);
return null;
}
}
I can provide some points.
First, get all the magical substrings and store them as a pair of start and end index(l, r) in a list;
Second, sort the list based on index l;
Those who can be potentially swapped substring can get from the same index l. look at the example given "11011000"
the list will have (0,7),(1,2),(1,6),(3,6),(4,5)
obviously only potential swap is among (1,2)(1,6)
deal these substrings have same index l will help find potential swapping substrings, sort them to find the maximum order.
class Pair{
int start;
int end;
}
public List<Pair> findmagicalPairs(String binString){
List<Pair> magicPairs = new ArrayList<Pair>();
for(int start=0;start<binString.length()-1;start++){
int ones=0;
int zeros=0;
for(int i=start; i<binString.length();i++){
if(binString.charAt(i) == '1'){
ones++;
} else if(binString.charAt(i)=='0'){
zeros++;
}
if(ones == zeros){ //check if magical
Pair temp=new Pair();
temp.start=start;
temp.end =i;
magicPairs.add(temp);
}
}
}
return magicPairs;
}
public String largestMagical(String binString) {
// Write your code here
List<Pair> allPairs = findmagicalPairs(binString);
String largest=binString;
//check by swapping each pairs
for(int i=0;i<allPairs.size()-1;i++){
for(int j=i+1;j<allPairs.size()-1;j++){
if(allPairs.get(i).end+1 == allPairs.get(j).start){
//consecutive Pair so swap and see largest
int index = allPairs.get(j).start;
String swapped = binString.substring(0,allPairs.get(i).start)+binString.substring(allPairs.get(j).start,allPairs.get(j).end+1)+binString.substring(allPairs.get(i).start,allPairs.get(i).end+1)+binString.substring(allPairs.get(j).end+1);
largest = LargestString(largest, swapped);
} else {
//else ignore
}
}
}
return largest;
}
public String LargestString(String first, String second){
if(first.compareTo(second)>0){
return first;
} else {
return second;
}
}
JavaScript Code!
const largestMagical = (binString) => {
//console.log({ binString });
const len = binString.length;
const height = Array(len + 1).fill(0),
num = { 1: 1, 0: -1 },
marked = Array(len + 1).fill(false),
sameHeights = {};
let i,
j,
result = "";
for (i = 1; i <= len; ++i) {
height[i] = height[i - 1] + num[binString[i - 1]];
}
//console.log({ height });
for (i = 0; i <= len; ++i) {
if (marked[i]) continue;
marked[i] = true;
sameHeights[i] = [i];
for (j = i + 1; j <= len; ++j) {
if (height[j] < height[i]) break;
if (height[j] === height[i]) {
sameHeights[i].push(j);
marked[j] = true;
}
}
}
//console.log({ sameHeights });
for (let k in sameHeights) {
const leng = sameHeights[k].length;
let startId, midId, endId;
for (startId = 0; startId < leng - 2; ++startId) {
for (midId = startId + 1; midId < leng - 1; ++midId) {
for (endId = midId + 1; endId < leng; ++endId) {
const start = sameHeights[k][startId],
mid = sameHeights[k][midId],
end = sameHeights[k][endId];
//console.log({start, mid, end});
const swapped =
binString.substring(0, start) +
binString.substring(mid, end) +
binString.substring(start, mid) +
binString.substring(end, len);
//console.log({swapped});
if (swapped > result) result = swapped;
}
}
}
}
return result;
};
console.log(largestMagical("1010111000"));
console.log(largestMagical("11011000"));
I am having a hard time trying to come up with a constructor for this example using an array list.
public class BigIntConstructorDemo {
public static void main(String[] args) {
BigInt1 b1 = new BigInt1("1000000");
System.out.println("b1 is " + b1);
}
}
This is a class with an array called digits that holds a max of 40 digits. The constructor loops through the array....
public class BigInt1 {
public static final int MAX_DIGITS = 40;
private int [ ] digits = new int[MAX_DIGITS];
public BigInt1(String s){
for (int i = 0; i < MAX_DIGITS; i++)
digits [i] = 0;
int i = 0;
for(int j = s.length()-1; j >= 0; j--){
digits[i] = s.charAt(j) - '0';
i++;
}
}
}
and then uses the toString method.
public String toString(){
String s = "";
for (i = 0; i < MAX_DIGITS;i++)
s = digits[i] + s;
if (s.equals(""))
s = "0";
return s;
I know I have to use a for loop, but i just can't seem to get the syntax right. Should I use the .add method that comes with the array list class? how could take a single character in a string and then put it into each element of an array list?
import java.util.ArrayList;
public class BigInt2{
private ArrayList <Integer> bigNum = new ArrayList<Integer>();
public BigInt() {
}
public BigInt(String s){
for(int index = 0;index ? bigNum.size();index++){
}
Yes, use add.
This is your old code with an array:
for(int j = s.length()-1; j >= 0; j--){
digits[i] = s.charAt(j) - '0';
i++;
}
with an arrayList foreach you get:
for (char c: s.toCharArray()) {
digits.add(c - '0');
}
note that the endianness (left to right or right to left) is switched in this case, if you want it the other way around simply loop as in your previous example over s.toCharArray() positions:
char[] chars = s.toCharArray();
for(int j = chars.length-1; j >= 0; j--){
digits.add(chars[j] - '0');
}
Below code is from topcoder website. I was trying to figure the time complexity for this code. There is 1 for loop and 1 while loop in the method isRandom and 1 for loop in the method diff. I guess the worst case scenario would be O(n^2). Is that correct?
public class CDPlayer {
private boolean[] used;
public boolean diff(String str, int from, int to) {
Arrays.fill(used, false);
to = Math.min(to, str.length());
for (int i = from; i < to; i++) {
if (used[str.charAt(i) - 'A']) {
return false;
}
used[str.charAt(i) - 'A'] = true;
}
return true;
}
public int isRandom(String[] songlist, int n){
String str = "";
for (int i = 0; i < songlist.length; i++) {
str += songlist[i];
}
used = new boolean[26];
for (int i = 0; i < n; i++) {
if (!diff(str, 0, i)) {
continue;
}
int j = i;
boolean bad = false;
while (j < str.length()) {
if (!diff(str, j, j + n)) {
bad = true;
break;
}
j += n;
}
if (bad) {
continue;
}
return i;
}
return -1;
}
}
I figured out something like this O(S) + O(n^2) + O(SS)*O(n^2), where
S = songlist.length, SS = sum of all song lengths. So your complexity depends on various inputs and it can't be represented by simple value.
P.S. Note that String is immutable object, so better use StringBuilder.
Before:
String str = "";
for (int i = 0; i < songlist.length; i++) {
str += songlist[i];
}
After:
StringBuilder builder = new StringBuilder();
for (int i = 0; i < songlist.length; i++) {
builder.append(songlist[i]);
}
In that case you won't create new String object on each iteration
As "n" is not the size of the input, it can not really be O(n) or O(n^2).
If m is the length of all strings in songlist, then you are jumping over that string in steps of the size n. So the compelxity is related to m not to n. I did not calculate in big O etc. since a few decades ... however I would assume the complexity is O(m).
My problem is how to count but not count the same character twice. Like comparing 'aba' to 'are' should give 1 as result since it has only one char in common.
This is where I got so far:
public int sameChars (Vector<String> otherStrs){
int result = 0;
String original = "aba";
for (int h= 0; h< otherStrs.size(); h++) {
String targetStr = otherStrs.get(h);
for (int i=0; i< original.length(); i++) {
char aux = original.charAt(i);
for (int j=0; j< Math.min(original.length(), targetStr.length()); j++) {
char targetAux = targetStr.charAt(j);
if (aux == targetAux) {
result++;
break;
}
}
}
}
return result;
}
Ideas are welcome, thanks.
You can create a hash of character count from the original string. Then for each target string, check if it has a char that has a non-zero value in your hash. This will prevent scanning your original string more than once.
Pseudocode:
For each char c in original string {
hash[c]++
}
For each target string str {
For each char c_ in str {
if hash[c_] > 0 {
result++;
}
}
}
This smells like homework, so here's the just the basic idea: You need to keep track of the distinct characters you've already counted as being in both places. A Set might be a good way to do this. Before incrementing your counter, check to see if the character you're looking at is already in that Set.
I am not sure to understand your requirement: do you want to count the number of times the distinct characters found in the reference string original, here "aba" thus 'a' and 'b', are found in a set of strings stored in the Vector otherStrs?
If that's the case, I would advise first to reduce the original string to distinct characters (looking for and removing duplicates, or using a Map). Then loop over the strings in the Vector and do the same for each string (removing duplicates or using a Map) before incrementing your counter each time a character is found in common.
Just out of curiosity, what is the end goal of this computation?
Here's my implementation:
public static int commonChars(String s1, String s2) {
if (s1 == null || s1.isEmpty())
throw new IllegalArgumentException("Empty s1");
if (s2 == null || s2.isEmpty())
throw new IllegalArgumentException("Empty s2");
char[] a1 = s1.toCharArray();
char[] a2 = s2.toCharArray();
Arrays.sort(a1);
a1 = removeDups(a1);
Arrays.sort(a2);
a2 = removeDups(a2);
int count = 0;
for (int i = 0, j = 0; i < a1.length && j < a2.length;) {
if (a1[i] == a2[j]) {
i++;
j++;
count++;
}
else if (a1[i] > a2[j])
j++;
else
i++;
}
return count;
}
public static char[] removeDups(char[] array) {
char[] aux = new char[array.length];
int c = 1;
aux[0] = array[0];
for (int i = 1 ; i < array.length; i++) {
if (array[i] != array[i-1])
aux[c++] = array[i];
}
return Arrays.copyOf(aux, c);
}