java delete reverse string in list - java

I have struct Array or List String like:
{ "A.B", "B.A", "A.C", "C.A" }
and I want delete reverse string from list that end of only:
{ "A.B", "A.C" }
how type String use and how delete reverse String?

To reverse a string I recommend using a StringBuffer.
String sample = "ABC";
String reversed_sample = new StringBuffer(sample).reverse().toString();
To delete object form you ArrayList use the remove method.
String sample = "ABC";String to_remove = "ADS";
ArrayList<String> list = new ArrayList<Sample>();
list.add(to_remove);list.add(sample );
list.remove(to_remove);

You can get use of a HashMap to determine whether a string is a reversed version of the other strings in the list. And you will also need a utility function for reversing a given string. Take a look at this snippets:
String[] input = { "A.B", "B.A", "A.C", "C.A" };
HashMap<String, String> map = new HashMap<String, String>();
String[] output = new String[input.length];
int index = 0;
for (int i = 0; i < input.length; i++) {
if (!map.containsKey(input[i])) {
map.put(reverse(input[i]), "default");
output[index++] = input[i];
}
}
A sample String-reversing method could be like this:
public static String reverse(String str) {
String output = "";
int size = str.length();
for (int i = size - 1; i >= 0; i--)
output += str.charAt(i) + "";
return output;
}
Output:
The output array will contain these elements => [A.B, A.C, null, null]

A code is worth thousand words.....
public class Tes {
public static void main(String[] args) {
ArrayList<String> arr = new ArrayList<String>();
arr.add("A.B");
arr.add("B.A");
arr.add("A.C");
arr.add("C.A");
System.out.println(arr);
for (int i = 0; i < arr.size(); i++) {
StringBuilder str = new StringBuilder(arr.get(i));
String revStr = str.reverse().toString();
if (arr.contains(revStr)) {
arr.remove(i);
}
}
System.out.println(arr);
}
}

You can do this very simply in O(n^2) time. Psuedocode:
For every element1 in the list:
For every element2 in the list after element1:
if reverse(element2).equals(element1)
list.remove(element2)
In order to make your life easier and prevent ConcurrentModificationException use Iterator. I won't give you the code because it's a good example to learn how to properly use iterators in Java.
Reverse method:
public String reverse(String toReverse) {
return new StringBuilder(toReverse).reverse().toString();
}
Edit: another reverse method:
public String reverse(String toReverse) {
if (toReverse != null && !toReverse.isEmpty) {
String[] elems = toReverse.split(".");
}
StringBuilder reversedString = new StringBuilder("");
for (int i = elems.length - 1; i >= 0; i++) {
reversedString.append(elems[i]);
reversedString.append(".");
}
return reversedString.toString();
}

Check this
public static void main(String arg[]){
String str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
List<String> strList = new ArrayList<String>();
strList.add("A.B");
strList.add("B.A");
strList.add("A.C");
strList.add("C.A");
Iterator<String> itr = strList.iterator();
while(itr.hasNext()){
String [] split = itr.next().toUpperCase().split("\\.");
if(str.indexOf(split[0])>str.indexOf(split[1])){
itr.remove();
}
}
System.out.println(strList);
}
output is
[A.B, A.C]

You can iterate the list while maintaining a Set<String> of elements in it.
While you do it - create a new list (which will be the output) and:
if (!set.contains(current.reverse())) {
outputList.append(current)
set.add(current)
}
This solution is O(n*|S|) on average, where n is the number of elements and |S| is the average string length.
Java Code:
private static String reverse(String s) {
StringBuilder sb = new StringBuilder();
for (int i = s.length()-1 ; i >=0 ; i--) {
sb.append(s.charAt(i));
}
return sb.toString();
}
private static List<String> removeReverses(List<String> arr) {
Set<String> set = new HashSet<String>();
List<String> res = new ArrayList<String>();
for (String s : arr) {
if (!set.contains(reverse(s))) {
res.add(s);
set.add(s);
}
}
return res;
}
public static void main(String[]args){
String[] arr = { "a.c" , "b.c", "c.a", "c.b" };
System.out.println(removeReverses(arr));
}
will yield:
[a.c, b.c]

Related

Converting List<Object> to String returns empty results

Disclaimer : I'm using this Post, as reference for List<Object> to List<String> and this Post for Java List<String> of strings to a JavaScript array.
I've List<Seat> and I want to get all values of it in a comma separated String, I tried in this way
import java.util.*;
import java.lang.*;
class Rextester
{
public Rextester(){
Seat seat1 = new Seat();
seat1.setSeatNumber(1);
Seat seat2 = new Seat();
seat2.setSeatNumber(2);
Seat seat3 = new Seat();
seat3.setSeatNumber(3);
List<Seat> seatList = new ArrayList<Seat>();
seatList.add(seat1);
seatList.add(seat2);
seatList.add(seat3);
Utility util = new Utility();
String stringSeats = util.toJavascriptArray(seatList);
System.out.println("JavaScriptArray is " + stringSeats);
}
public static void main(String args[])
{
new Rextester();
}
private class Seat {
private Integer seatNumber;
public Integer getSeatNumber() {
return this.seatNumber;
}
public void setSeatNumber(Integer seatNumber) {
this.seatNumber = seatNumber;
}
public String toString() {
return ""+ seatNumber;
}
}
private class Utility {
public String toJavascriptArray(List<Seat> listSeats){
List<String> strings = new ArrayList<String>();
for (Seat object : listSeats) {
strings.add(object != null ? object.toString() : null);
}
String[] arr = new String[strings.size()];
arr = strings.toArray(arr);
StringBuffer sb = new StringBuffer();
sb.append("[");
for(int i=0; i<arr.length; i++){
if(i+1 < arr.length){
sb.append(",");
}
}
sb.append("]");
return sb.toString();
}
}
}
but this gives me
JavaScriptArray is [,,]
on console, am I making some mistakes? an online working code is http://rextester.com/NDUGT61105
You didn't append iterated element, see below
for (int i = 0; i < arr.length; i++) {
sb.append(arr[i]); // add this
if (i + 1 < arr.length) {
sb.append(",");
}
}
have a look at the toString implementations of Arrays and List
Simply you can return
strings.toString()
or
Arrays.toString(arr)
To get the expected result
Another option, if you're using Java 8 is using StringJoiner:
https://docs.oracle.com/javase/8/docs/api/java/util/StringJoiner.html
You forgot to append the array elements to the string buffer!
Add
sb.append(arr[i]);
inside the loop where you prepare the string buffer and everything is ok

return multiple values java for removing last part of a word

In my code, fromright method checks the length of last[] and returns only one string. I want to return all matched values. What's the solution?
public static String last[]={"es","e","s"};
public static void main(String[] args) {
text tx=new text();
String checkString = "lives";
String fin=tx.fromright(checkString);
System.out.println("remaining: "+fin);
}
public String fromright(String wrd) {
String tmp="";
for (int i = 0; i < last.length; i++) {
tmp=wrd.substring(0, wrd.length()-last.length);
}
return tmp;
}
You are overriding your tmp variable in your for loop every time. So you can only get one result.
Use this instead or smth. similiar which can hold multiple values.
public List<String> fromright(String wrd) {
List<String> tmp= new ArrayList<String>();
for (int i = 0; i < last.length; i++) {
tmp.add(wrd.substring(0, wrd.length()-last.length));
}
return tmp;
EDIT:
This does not work anymore.
String fin=tx.fromright(checkString);
^
Replace it with
List<String> fin= new ArrayList<String>(tx.fromright(checkString));
And print out all values with this
for(String s : fin) System.out.println(s);
public List<String> fromright(String wrd) {
List<String> result = new ArrayList<>();
for (int i = 0; i < last.length; i++) {
if(wrd.endsWith(last[i]))
result.add(last[i]);
}
return result;

Java Substring by Keyword

I have to make separate Strings from one single String.
For example given the String:
.*C.{0}A.{2}T.{0}T.{0}T.{2}T.{0}G.{8}T.{7}A.{7}T.{2}T.{12}A.{5}T.{4}T.{45}A.{1}A.{10}G.{19}A.{25}T.{3}A.{1}A.{4}G.{1}A.{2}A.{29}A.{0}C.{15}A.{1}C.{1}A.{6}T.{3}G.{5}T.{0}T.{0}C.{3}G.{2}C.{1}G.{4}G.{1}G.*
I have to create a HashSet with the following content:
.*C.{0}A.{2}T.{0}T.*
.*A.{2}T.{0}T.{0}T.*
.*T.{0}T.{0}T.{2}T.*
.*T.{0}T.{2}T.{0}G.*
...
The elements are formed by taking 4 of the entries from the original string and creating a smaller string from them. Then you move one entry along in the original string and repeat.
How can I do this?
Thanks!
You want to take a string, representing a list of elements, and turn it into a set of overlapping shorter lists of elements. You can do this by having a method which returns the elements from the list and then a sliding window which selects sets of elements to display:
private static final Pattern pattern = Pattern.compile("[ACGT]\\.\\{\\d+\\}");
public static List<String> extract(String input) {
Matcher matcher = pattern.matcher(input);
List<String> result = new ArrayList<String>();
while (matcher.find()) {
result.add(matcher.group(0));
}
return result;
}
public static Set<String> compose(List<String> elements, int window) {
Set<String> result = new HashSet<String>();
for (int i = 0; i <= elements.size() - window; i++) {
StringBuilder builder = new StringBuilder(".*");
for (int j = i; j < i + window; j++) {
builder.append(elements.get(j));
}
// This strips the final quantifier turning:
// .*C.{0}A.{2}T.{0}T.{0}
// into
// .*C.{0}A.{2}T.{0}T
builder.delete(builder.lastIndexOf("."), builder.length());
builder.append(".*");
result.add(builder.toString());
}
return result;
}
You can check this with the following method:
public static void main(String[] args) {
String input = ".*C.{0}A.{2}T.{0}T.{0}T.{2}T.{0}G.{8}T.{7}A.{7}";
Set<String> result = compose(extract(input), 4);
// The result will contain
// ".*C.{0}A.{2}T.{0}T.*"
// etc
}
Here is a possible solution:
public class Main {
public static void main(String[] args) {
String s = ".*C.{0}A.{2}T.{0}T.{0}T.{2}T.{0}G.{8}T.{7}A.{7}T.{2}T.{12}A.{5}T.{4}T.{45}A.{1}A.{10}G.{19}A.{25}T.{3}A.{1}A.{4}G.{1}A.{2}A.{29}A.{0}C.{15}A.{1}C.{1}A.{6}T.{3}G.{5}T.{0}T.{0}C.{3}G.{2}C.{1}G.{4}G.{1}G.*";
String[] array = s.split("}");
Set<String> result = new HashSet<String>();
for ( int i = 0 ; i < array.length-3 ; i++) {
String firstElement = array[i].startsWith(".*") ? array[i].substring(2) : array[i];
String lastElement = array[i+2]+"}"+array[i+3].substring(0,1)+".*" ;
String element = ".*"+firstElement+"}"+array[i+1]+"}"+lastElement;
result.add(element);
System.out.println(element);
}
//Your result are in the Set result
}
}

Java split string with combinations

My input string is
element1-element2-element3-element4a|element4b-element5-
Expected output is
element1-element2-element3-element4a-element5-
element1-element2-element3-element4b-element5-
So the dash (-) is the delimiter and the pipe (|) indicates two alternative elements for a position.
I am able to generate combinations for input containing a single pipe:
ArrayList<String> finalInput = new ArrayList<String>();
String Input = getInputPath();
StringBuilder TempInput = new StringBuilder();
if(Input.contains("|")) {
String[] splits = Input.split("\\|", 2);
TempInput.append(splits[0]+"-"+splits[1].split("-", 2)[1]);
finalInput.add(TempInput.toString());
TempInput = new StringBuilder();
String[] splits1 = new StringBuilder(Input).reverse().toString().split("\\|", 2);
finalInput.add(TempInput.append(splits1[0]+"-"+splits1[1].split("-", 2)[1]).reverse().toString());
}
But this logic fails if there are multiple pipe symbols.
How to split a String on the last occurrance only?
Is there any efficient way to use split String with combinations?
Input:
element1-element2-element3-element4a|element4b-element5-element6a|element6b
Output:
element1-element2-element3-element4a-element5-element6a
element1-element2-element3-element4b-element5-element6a
element1-element2-element3-element4a-element5-element6b
element1-element2-element3-element4b-element5-element6b
Recursion helps.
public static void main(String[] args) {
produce("element1-element2-element3-element4a|element4b"
+ "-element5-element6a|element6b");
}
private static void produce(String input) {
String[] sequence = input.split("-");
String[][] elements = new String[sequence.length][];
for (int i = 0; i < sequence.length; ++i) {
elements[i] = sequence[i].split("\\|");
}
List<String> results = new ArrayList<>();
walk(results, elements, 0, new StringBuilder());
}
private static void walk(List<String> results, String[][] elements,
int todoIndex, StringBuilder done) {
if (todoIndex >= elements.length) {
results.add(done.toString());
System.out.println(done);
return;
}
int doneLength = done.length();
for (String alternative : elements[todoIndex]) {
if (done.length() != 0) {
done.append('-');
}
done.append(alternative);
walk(results, elements, todoIndex + 1, done);
done.setLength(doneLength); // Undo
}
}
The String.split method is used twice to get a navigatable String[][]. And to build a final String a StringBuilder is used.
You can use StringTokenizer in Java. Basically it makes tokens of the string.
public StringTokenizer(String str, String delim)
Here's an example:
String msg = "http://100.15.111.60:80/";
char tokenSeparator= ':';
StringTokenizer st = new StringTokenizer(msg, tokenSeparator + "");
while(st.hasMoreTokens()) {
System.out.println(st.nextToken());
}
I have write a demo for you as what I comment after your post, the code may be ugly, but it works
public class TestSplit {
//define a stringList hold our result.
private static List<String> stringList = new ArrayList<String>();
//this method fork the list array when we meet a "|"
public static void forkStringList(){
List<String> tmpList = new ArrayList<String>();
for(String s: stringList){
tmpList.add(s);
}
stringList.addAll(tmpList);
}
//when we meet "|" split two elems, should add it to
//the string list half-half
public static void addTowElems(String s1, String s2){
for(int i=0;i<stringList.size()/2;i++){
stringList.set(i,stringList.get(i)+s1);
}
for(int i = stringList.size()/2;i<stringList.size();i++){
stringList.set(i,stringList.get(i)+s2);
}
}
// if not meet with a "|" just add elem to everyone of the stringlist
public static void addOneElem(String s){
for(int i=0;i<stringList.size();i++){
stringList.set(i,stringList.get(i)+s);
}
}
public static void main(String[] argvs){
//to make *fork* run, we must make sure there is a "init" string
//which is a empty string.
stringList.add("");
// this is your origin string.
String input = "a-b-c-d|e-f";
for (String s: input.split("\\-")){
if(s.contains("|")){
//when meet with "|", first fork the stringlist
forkStringList();
// then add them separately
addTowElems(s.split("\\|")[0],s.split("\\|")[1]);
}else {
// else just happily add the elem to every one
// of the stringlist
addOneElem(s);
}
}
//checkout the result, should be expected.
System.out.println(stringList);
}
}
Here's my iterative solution:
import java.util.*;
public class PathParser {
private static final String DELIMINATOR_CONCAT = "-";
private static final String DELIMINATOR_OPTION = "|";
private List<String> paths;
private List<String> stack;
private List<String> parse(final String pathSpec) {
stack = new ArrayList<String>();
paths = new ArrayList<String>();
paths.add("");
final StringTokenizer tok = createStringTokenizer(pathSpec);
while (tok.hasMoreTokens()) {
final String token = tok.nextToken();
parseToken(token);
}
if (!stack.isEmpty()) {
updatePaths();
}
return paths;
}
private void parseToken(final String token) {
if (DELIMINATOR_CONCAT.equals(token)) {
updatePaths();
} else if (DELIMINATOR_OPTION.equals(token)) {
// nothing to do
} else {
stack.add(token);
}
}
private void updatePaths() {
final List<String> originalPaths = new ArrayList<String>(paths);
paths.clear();
while (stack.size() > 0) {
paths.addAll(createNewPaths(originalPaths));
}
}
private List<String> createNewPaths(final List<String> originalPaths) {
final List<String> newPaths = new ArrayList<String>(originalPaths);
addPart(newPaths, stack.remove(0));
addPart(newPaths, DELIMINATOR_CONCAT);
return newPaths;
}
private void addPart(final List<String> paths, final String part) {
for (int i = 0; i < paths.size(); i++) {
paths.set(i, paths.get(i) + part);
}
}
private StringTokenizer createStringTokenizer(final String pathSpec) {
final boolean returnDelimiters = true;
final String delimiters = DELIMINATOR_CONCAT + DELIMINATOR_OPTION;
return new StringTokenizer(pathSpec, delimiters, returnDelimiters);
}
public static void main(final String[] args) {
final PathParser pathParser = new PathParser();
final String input = "element1-element2-element3-element4a|element4b|element4c-element5-element6a|element6b|element6c";
System.out.println("Input");
System.out.println(input);
System.out.println();
final List<String> paths = pathParser.parse(input);
System.out.println("Output");
for (final String path : paths) {
System.out.println(path);
}
}
}
Output:
Input
element1-element2-element3-element4a|element4b-element5-element6a|element6b
Output
element1-element2-element3-element4a-element5-element6a-
element1-element2-element3-element4b-element5-element6a-
element1-element2-element3-element4a-element5-element6b-
element1-element2-element3-element4b-element5-element6b-
This helps to acheive the same..
public class MultiStringSplitter {
public static void main(String arg[]) {
String input = "a-b|c-d|e-f|g-h";
String[] primeTokens = input.split("-");
String[] level2Tokens = null;
String element = "";
String level2element = "";
ArrayList stringList = new ArrayList();
ArrayList level1List = new ArrayList();
ArrayList level2List = new ArrayList();
for (int i = 0; i < primeTokens.length; i++) {
// System.out.print(primeTokens[i]);
if (primeTokens[i].contains("|")) {
level2Tokens = primeTokens[i].split("\\|");
for (int j = 0; j < level2Tokens.length; j++) {
for (int k = 0; k < stringList.size(); k++) {
element = (String) stringList.get(k);
level2element = element + level2Tokens[j];
level2List.add(level2element);
}
}
stringList = new ArrayList();
for (int w = 0; w < level2List.size(); w++) {
stringList.add(level2List.get(w));
}
level2List = new ArrayList();
}
else {
if (stringList.size() > 0) {
for (int z = 0; z < stringList.size(); z++) {
element = (String) stringList.get(z);
element = element + primeTokens[i];
level1List.add(element);
}
stringList = new ArrayList();
for (int w = 0; w < level1List.size(); w++) {
stringList.add(level1List.get(w));
}
level1List = new ArrayList();
}
else {
element = element + primeTokens[i];
if (stringList.size() == 0) {
stringList.add(element);
}
}
}
}
for (int q = 0; q < stringList.size(); q++) {
System.out.println(stringList.get(q));
}
}
}
Input : a-b|c-d|e-f|g-h
Output:
abdfh
acdfh
abefh
acefh
abdgh
acdgh
abegh
acegh

Count the number of occurrences of keywords in an Array list

public static void main(String[] args) {
ArrayList<String> list = new ArrayList<String>();
String s = "nope";
String v = "nopenopes";
list.add(s);
list.add(v);
keywordCount(list, "nope");
}
public static int keywordCount(ArrayList<String> str , String keyword){
int count = 0;
for (int i = 0;i<str.size();i++){
while ((i= str.indexOf(keyword,i)) != -1) { // here is where I found the error
count++;
i += keyword.length();
}}
System.out.println(count);
return count;
}
hello, I'm writing a piece of code that counts the occurrences of a specific keyword inside an array list. It worked for a line of string but did not when trying to do the same thing for an arraylist. Can someone please point out what I did incorrectly and help me fix it. Thanks in advance.
You can sue Collections.frequncy()
List<String> list=new ArrayList<>();
list.add("a");
list.add("b");
list.add("a");
System.out.println(Collections.frequency(list,"a")); // out put is 2
Let's say you have a List<String> list then you want to find number of occurrence keyword
Then
int numOfOccurrences= Collections.frequency(list,keyword)
Easily you can use contains() method;
public static int keywordCount(ArrayList<String> str , String keyword){
int count = 0;
for (int i = 0; i<str.size();i++){
if(str.get(i).contains(keyword))
count++;
}
}
You could use Arrays.asList() to construct your List.
public static void main(String[] args) {
System.out.println(keywordCount(Arrays.asList("nope", "nopenopes"), "nope"));
}
public static int keywordCount(List<String> al, String keyword) {
int count = 0;
for (String str : al) {
int i = 0;
while ((i = str.indexOf(keyword, i)) != -1) {
count++;
i += keyword.length();
}
}
return count;
}
When I run the above I get the output
3
One issue in your code was
i = str.indexOf(keyword,i)
In your post str is not a String, it is an ArrayList.
use code below.
public static void main(String[] args) {
List<String> list = new ArrayList<String>();
list.add("a");
list.add("b");
list.add("a");
// get all Unique keywords
Set<String> set = new HashSet<String>(list);
for(String keyword: set){
System.out.println(keyword + ": " + Collections.frequency(list, keyword));
}
}
Output:
b: 1
a: 2

Categories