Repeat String Recursively [closed] - java

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
This is homework. I get the logic but i got stuck on the code. I've done it with normally way and it takes 1 week to get the code. I need to get repeat string with recursive way in Java.
This is my code :
static String repeatString (final int n, final String[] syllables, final String currentWord) {
if (n == 0) {
System.out.println(currentWord);
} else {
for (int i = 0; i < syllables.length; i++) {
repeatString(n - 1, syllables, currentWord + syllables[i]);
}
}
return "";
}
if i call in main method like
String[] str = {"a", "b"};
repeatString(1, str, " ");
then i get output (a,b) if i change to
repeatString(2,str," ");
then i get output ( aa,ab,ba,bb) if i change to
repeatString(3,str," ");
then i get output (aaa,aab,aba,abb,baa,bab,bba,bbb) and so on.
So basically it is like 2 to the power to n. If n=1, i got 2, if n=3, i got 8, and so on.
I would be grateful if someone can help me to get this code in recursive way.
Any help is much appreciated.

The method you have there is recursive already. Being recursive does NOT mean it should have no for loops. A recursive method in cheap words means the method calls itself, which yours does.

Related

Using ASCII to find valid Anagram [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 10 months ago.
Improve this question
Why doesn't this solution work for finding valid anagram?
26/36 test cases get passed in LeetCode.
class Solution {
public boolean isAnagram(String s, String t) {
int sASCII = 0, tASCII = 0;
if(s.length() != t.length()) {return false;}
else{
for(int i = 0 ; i < s.length(); i++){
sASCII += (int)s.charAt(i);
tASCII += (int)t.charAt(i);
}
}
if(sASCII == tASCII){
return true;
}
return false;
}
}
The sums tASCII and sASCII can be equal even if the numbers are not anagrams. Let's say that you can get the number 100 by adding 60+40, but you can also get it by adding 70+30, so i recommend to use a HashMap to note every occurence of every letter or to sort the strings as arrays of chars and then compare them.

How can I store characters of string variable in an integer array in Java [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
For example if:
String str = "100101010";
Then following should happen:
a[0]=1
a[1]=0
a[2]=0
...
It should hold for large strings also (up to 64 characters).
One hint: find a chart for ASCII. http://www.asciitable.com/index/asciifull.gif The code for "0" is 48 and the code for "1" is 49. You can convert characters to numbers just by subtracting 48 from the ASCII value.
int value = str.charAt(0) - 48;
It's important to realize that a Java char is just an integer type just like int and byte. You can do math with them. With that idea in mind, you should be able figure out the rest yourself.
(This is technically a duplicate since I answered a similar question long ago, but I can't find it, so you get a freebie.)
public static int[] getAsArray(String value){
// not null ; not empty ; contains only 0 and 1
if(value == null || value.trim().length()<1 || !value.matches("[0-1]+")){
return new int[0];
}
//if it necessary !value.matches("[0-1]+" regex to validate
value = value.trim();
value = value.length()>64 ? value.substring(0,63) : value;// up to 64 characters
int[] valueAsInt = new int[value.trim().length()];
for (int i = 0 ;i<value.length();i++){
valueAsInt[i]=(int)value.toCharArray()[i]-48;
}
return valueAsInt;
}
also if it's possible use shor or byte type as it's enoght to store 0,1 and you consume less memory as with int
I don't know why you want to do this, but it is a simple problem.
public static void main (String[] args) {
String s = "111232";
String element[] = s.split("");
System.out.println(element[0]);
System.out.println(element[1]);
System.out.println(element[2]);
System.out.println(element[3]);
System.out.println(element[4]);
System.out.println(element[5]);
}

Is it possible to print Fibonacci series in java without using recursion, loop and custom function [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I have been trying various methods. However I cannot find out whether it is possible to print Fibonacci series in java without using recursion, loop and custom functions. If yes then how?
You can use the scheduled exector pool. It's not technically a loop, though it is using the repeating thread to behave like a loop.
private static volatile int currentNum = 1;
private static volatile int previousNum = 0;
public static void main(String[] args) {
ScheduledThreadPoolExecutor timer = new ScheduledThreadPoolExecutor(1);
timer.scheduleWithFixedDelay(() -> {
System.out.println(currentNum);
int temp = currentNum;
currentNum += previousNum;
if (currentNum < 0) {
// overflow
timer.shutdown();
}
previousNum = temp;
}, 0, 1, TimeUnit.MILLISECONDS);
}

Remove value arraylist java from input [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I have an array like this:
1101 "TV"
5531 "Baju Baru"
1425 "Mesin Cuci"
Then i want to remove "TV" from my Arraylist. So i must type "1101" then the value is remove. But if i'm wrong it show "code is invalid".
Here is my code:
for (int i = 0; i < listBarang.size(); i++) {
System.out.println(listBarang.get(i));
}
System.out.println("Your code stuff: ");
int code = Integer.parseInt(input.next());
listBarang.remove(i);
Any answer?
Consider using Map to store your arrays and you can remove the TV element from the it. try this.
Map<Integer,String> map = new HashMap<>();
map.put(1101,"TV");
map.put(5531 ,"Baju Baru");
map.put(1425 ,"Mesin Cuci");
for (Map.Entry<Integer,String> hh : map.entrySet()) {
if (hh.getKey() == 1101){
map.remove(hh.getKey());
}
}
System.out.println(map);
output without "TV"
{1425=Mesin Cuci, 5531=Baju Baru}
remove(i) makes no sense outside of your loop. Move it inside and if the code equals the input remove(i).

Find all possible permutations or combinations with the same order [closed]

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'm trying to write a program to print all possible permutations with 8 variables where each variable has multiple choices.
For eg. i have
A = {"A1"} //has only one choice
B = {"B1", "B2", "B3", "B4"} // has 4 choices
C = {"C1", "C2"} //has 2 choices
:
:
I = {"I1", "I2", "I3", "I4"} //has 4 choices
My output should be of the form: A-B-C-D-E-F-G-H-I with all possible choices of each variable and the order should be the same.
A1-B1-C1-D1-E1-F1-G1-H1-I1
A1-B2-C1-D1-E1-F1-G1-H1-I1
A1-B3-C1-D1-E1-F1-G1-H1-I1
A1-B4-C1-D1-E1-F1-G1-H1-I1
A1-B1-C2-D1-E1-F1-G1-H1-I1
etc.
I looked at all the other questions here, but i'm not able to figure out if i can use the inbuilt java permutations class for this. I tried writing a recursive program but am stuck when trying to explode each choice into all possible outputs. Appreciate any tips on how to accomplish this in either java/c++/vba since the language is not a consideration. Thanks!
You can write a series of 9 nested loops that does the job in a straightforward way. This would be a mess, though, and it can be done more simply with recursion on the nesting level. (Don't try recursing on the data at each level; that's pointless.) Here's a Java solution in pseudocode:
String[][] DATA = {A, B, ..., I};
void printAll(int recursionLevel, String prefix) {
String[] level = DATA[recursionLevel];
if (recursionLevel == DATA.length - 1) {
// last level -- actually do the output
for (String val : level) {
System.out.println(prefix + "-" + val);
}
} else {
// recurse
if (prefix.length() > 0) {
prefix += "-";
}
for (String val : level) {
printAll(recursionLevel + 1, prefix + val);
}
}
}
You would generate the output by calling:
printAll(0, "");

Categories

Resources