lparka123
BAN USERpublic class StringRotate {
public static void main(String[] args) {
String str="General Electric";
StringBuffer sb = new StringBuffer();
for(String s: str.split(" ")){
sb.append(shift(s,3));
sb.append(" ");
}
System.out.println(sb.toString());
}
public static String shift(final String input, final int shift){
if(input==null || input.length()==0){
return "";
}
int length = input.length();
int offSet = shift % length;
return input.substring(offSet,length)+input.substring(0,offSet);
}
}
import java.util.Scanner;
public class ArrayFrequency{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int temp=0;
int n=0;
int[] array;
while (sc.hasNextInt()) {
n = sc.nextInt();
if(n<0 && n>10 ){
System.out.println("please enter a valid numer.");
break;
}
temp=temp+n;
array = new int[n];
for(int i=n-1;i>=0;i--){
array[i]=n;
}
for(int x: array){
System.out.println(x);
}
}
if(sc.hasNextInt()==false){
sc.close();
System.out.println("plese enter a valid numer from 0-9.Exit...");
Runtime.getRuntime().exit(0);
}
}
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import org.apache.commons.lang.StringUtils;
public class UniqueCharSet {
public static void main(String[] args) {
// input string
String str = "May student students dog studentssess god Cat act tab bat flow wolf lambs Amy Yam balms looped poodle john alice";
// input string converted to lower case
str = str.toLowerCase();
// split the input string into string array
String[] s1 = StringUtils.split(str);
// sort the array of string words
String[] strArray = new String[s1.length];
for (int i = 0; i < s1.length; i++) {
strArray[i] = sort(s1[i]);
}
// store sorted array of strings with it;s word count
Map<String, Integer> hashMap = new HashMap<String, Integer>();
int tmp = 0;
String subStr = null;
boolean flag = false;
for (int x = 0; x < strArray.length; x++) {
if (subStr != null) {
if (strArray[x].contains(subStr)) {
flag = true;
}
}
if (!hashMap.containsKey(strArray[x]) || flag) {
subStr = strArray[x];
if (flag) {
hashMap.put(strArray[x], ++tmp);
flag = false;
} else {
tmp = 0;
hashMap.put(strArray[x], ++tmp);
}
} else {
hashMap.put(strArray[x], ++tmp);
}
}
// iterate through hash map and check is any word is having count greater than 1 print it;s unique charcter set from input
Iterator<Entry<String, Integer>> iter = hashMap.entrySet().iterator();
while (iter.hasNext()) {
Map.Entry<String, Integer> pair = iter.next();
String temp = pair.getKey();
int val = pair.getValue();
if (val > 1) {
for (int x = 0; x < s1.length; x++) {
if (sort(s1[x]).equals(temp)) {
System.out.print(s1[x]);
System.out.print("\t");
}
}
System.out.println();
}
}
}
/**
* sort the input string alphabetically
* @param input input string
* @return sorting string
*/
public static String sort(String input) {
String original = input;
int j = 0;
char temp = 0;
char[] chars = original.toCharArray();
for (int i = 0; i < chars.length; i++) {
for (j = 0; j < chars.length; j++) {
if (chars[j] > chars[i]) {
temp = chars[i];
chars[i] = chars[j];
chars[j] = temp;
}
}
}
return String.valueOf(chars);
}
}
String str="I found an apple in a tree";
String[] articles={"a","an","the"};
String[] inputString=str.split(" ");
char inputChar='a';
int wordCnt=0;
for(String s: inputString){
if(!s.equalsIgnoreCase(articles[0])&&
!s.equalsIgnoreCase(articles[1])&&
!s.equalsIgnoreCase(articles[2])){
if(s.length()>1){
for(int i=0;i<s.length();i++){
if(inputChar==s.charAt(i)){
wordCnt+=1;
break;
}
}
}
}
}
System.out.println("word count:"+wordCnt);
Integer[] sa ={1, 3, 4, 5, 8, 9, 11, 13, 14, 15, 16, 20, 23, 30,31,32};
LinkedList aa = new LinkedList();
aa.addFirst(sa[0]);
for(int i=1;i<sa.length-1;i++){
if(sa[i-1]+1==sa[i]){
aa.removeLast();
String temp="";
for(int j=i-1;j<sa.length-1;j++){
int a=sa[j];
int b=sa[j+1];
if(a+1==b){
temp=temp+Integer.toString(sa[j])+":";
if(j==sa.length-2){
temp=temp+Integer.toString(sa[j+1]);
aa.addLast(temp);
i=j;
break;
}
}else{
temp=temp+Integer.toString(sa[j]);
aa.addLast(temp);
i=j;
break;
}
}
}else{
aa.addLast(sa[i]);
}
}
Iterator iterator = aa.iterator();
while (iterator.hasNext()) {
// Print element to console
System.out.print(iterator.next()+" ");
}
It works.
Hi
If you observe the input there are some numbers followed by consecutive numbers. For eg:
1 does not have consecutive number 2. So, add 1 in Linked list node.
Add next element in input, i.e 3 to another node of Linked List, then check in the input array for 3 consecutive, i.e 4, it is available. So, add 4 to the same node of Linked list. Like that 5 also to the same node of Linked List.
Now, the linked list contains the [1]->[3,4,5]
Next element in 8 not consecutive to 5. So, add 8 to another node of Linked list.
Like that will have to create Linked List.
Hope you understand the sequence.
Final Sequence is : [1 ]-> [3,4,5]-> [8,9] -> [11] -> [13,14,15,16] -> [20] -> [23] -> [30,31,32].
String[] sa = new String[] { "A", "B", "C" };
LinkedList ll = new LinkedList(Arrays.asList(sa));
// iterate over each element in LinkedList
Iterator iterator = ll.iterator();
while (iterator.hasNext()) {
// Print element to console
System.out.println((String) iterator.next());
}
Hoping that it will help you.
public class LetterCount {
- lparka123 July 31, 2017public static void main(String[] args) {
String str="aaabbba";
compress(str);
}
public static void compress(String str){
int size = countCompression(str);
if(size >str.length()){
return;
}
StringBuffer myStr = new StringBuffer();
char last = str.charAt(0);
int count=1;
for(int i=1;i<str.length();i++){
if(str.charAt(i)==last){
count++;
}else{
myStr.append(last);
myStr.append(count);
last = str.charAt(i);
count=1;
}
}
myStr.append(last);
myStr.append(count);
System.out.println(myStr.toString());
}
public static int countCompression(String str){
char last = str.charAt(0);
int size=0,count=1;
for(int i=1;i<str.length();i++){
if(str.charAt(i)==last){
count++;
}else{
last = str.charAt(i);
size+=1+String.valueOf(count).length();
count=1;
}
}
size+=1+String.valueOf(count).length();
return size;
}
}