Ibibo Interview Question
Testing / Quality AssurancesCountry: United States
Interview Type: Phone Interview
sites.google.com/site/spaceofjameschen/home/regular-expression/find-strings-with-a-consecutive-repetition-of-a-specified-letter---ibibo
^^Thank you good sire!! But the solution you gave is in C++ I guess!! How do I match (.)\1{1} as regex in Java?
The same regex can be used in java.
public class Main {
Pattern p = Pattern.compile("(.)\\1{1}");
Pattern pattern = Pattern.compile("(\\w)\\1+");
public static void main(String[] args) {
Main appMain = new Main();
Set<String> stringSet = new HashSet<String>();
stringSet.add("Dauresselam");
stringSet.add("slab");
stringSet.add("fuss");
stringSet.add("boolean");
stringSet.add("clap");
stringSet.add("clap");
appMain.compare(stringSet);
}
private void compare(Set<String> stringSet) {
for (String arg : stringSet) {
Matcher match = p.matcher(arg);
if (match.find())
System.out.println( " Match found in word:"
+ arg);
}
}
}
You can use any of the 2 regex. they both should work fine
So you have to return a string array as well. I tried with the below solution i know it's not perfect or polished from a purist p.o.v kinda didn't help me get in LoL:
import java.util.Scanner;
public class doubleChars {
public static String[] getDoubles(String[]In)
{
int inLen=In.length;
String zoom[]=new String[inLen];
int count=0;
if(inLen==0)
{
return zoom;
}
for(int i=0;i<=inLen-1;i++)
{
String A=In[i];
//System.out.println(A);
int striLen=A.length();
for(int j=0;j<striLen-1;j++)
{
if(A.substring(j, j+1).equals(A.substring(j+1, j+2)))
{
zoom[count]=A;
count++;
break;
}
}
}
return zoom;
}
public static void main(String[] args)
{
char more='y';
int ab=0;
String[] res={};
String[] fillMe={"durres", "murres", "", "abcdeee", "boolean", "nger", "lagger"};
Scanner strobe=new Scanner(System.in);
System.out.println("Please enter the arraye of the string");
/*while(strobe.hasNext())
{
fillMe[ab]=strobe.next();
ab++;
}
*/
res=doubleChars.getDoubles(fillMe);
for(int k=0;k<res.length;k++)
{
if(res[k]==null)
{
break;
}
System.out.println(res[k]);
}
}
}
void findCons(String[] words){
if (words==null);
else{
for(String s : words){
for(int i=0; i<s.length()-1;i++){
if(s.charAt(i)==s.charAt(i+1)){
System.out.println(s);
break;
}
}
}
}
}
You should do only upto i<s.length -1
also it'll be better if you make it a function like Bool StringHasRepeatedChars(string input)
Java strings don't have subscript[] operator, so you need to use "s.charAt(i) == s.charAt(i+1)".
Using For each loop is a better option but oOZz is right...either use chartAt ot substring function to verify repetition.
public class SubString {
public static String[] s = { "Dauresselam","fuss", "boolean","clap" };
public static void main(String[] args) {
for (String ss : s) { if(!ss.equals(null))
for (int i = 0; i < ss.length()-1; i++) {
if(ss.charAt(i)==ss.charAt(i+1)){System.out.println(ss);}
} } } }
Ruby Code
a=Array.new
b=Array.new
a=["Dauresselam", "slab", "fuss", "boolean", "clap"]
p=0
l=a.length
puts l
for i in 0...l
flag=0
temp=Array.new
la=a[i].length
temp = a[i]
n=temp[0]
for j in 1...la
if n==temp[j]
flag=1
else
n=temp[j]
end
end
if flag == 1
b[p]=a[i]
p=p+1
end
end
puts "Array is: #{b}"
I think below function should work fine..and its run time complexity is O(n)
public static List<String> getConsecutiveRepeatedStrings(String[] input){
int inputLen = input.length;
List<String> li = new ArrayList<String>();
int strPtr = 0, i = 0 , j = 1;
while(strPtr<inputLen){
if(input[strPtr].charAt(i) == input[strPtr].charAt(j)){
li.add(input[strPtr]);
strPtr++;
i = 0 ;
j = 1;
}
else if(j < input[strPtr].length()-1){
i++;
j++;
}
else{
strPtr++;
i = 0 ;
j = 1;
}
}
return li;
}
public ArrayList<String> findRepeat(String[] input) {
int i = 0;
ArrayList<String> res = new ArrayList<String>();
res.clear();
for (String s : input) {
if (s.length() > 1) {
for (i = 1; i < s.length(); i++) {
if (s.charAt(i) == s.charAt(i - 1)) {
res.add(s);
break;
}
}
}
}
return res;
}
#include<stdio.h>
#include<string.h>
int main()
{
char a[5][15]={"kaushal","naggesh","indiaan","institute","myystring"};
char *str,*finalarr[5];
int i,len,j,count=0;
for(i=0;i<5;i++)
{
str=&a[i][0];
len=strlen(str);
for(j=1;j<len;j++)
{
if(str[j]==str[j-1])
{
finalarr[count]=str;
count++;
break;
}
}
}
for(i=0;i<count;i++)
printf(" %s ",finalarr[i]);
return 0;
}
#include<stdio.h>
struct list
{
char data[20];
struct list *next;
}*head=NULL;
struct list *insert(struct list *head,char n[20])
{
struct list *temp=head,*t;
if(!head)
{
head=(struct list *)malloc(sizeof(struct list));
strcpy(head->data,n);
head->next=NULL;
}
else
{
while(temp->next)temp=temp->next;
t=(struct list *)malloc(sizeof(struct list));
strcpy(t->data,n);
t->next=NULL;
temp->next=t;
}
return head;
}
int display(struct list *head)
{
struct list *temp=head;
while(temp)
{
printf("%s ",temp->data);
temp=temp->next;
}
return 0;
}
int search(struct list *head)
{
int i=1;
struct list *temp=head;
while(temp)
{
i=0;
while(temp->data[i++])if(temp->data[i]==temp->data[i-1]){printf("\n%s",temp->data);break;}
temp=temp->next;
}
return 0;
}
int del_list(struct list *head)
{
struct list *temp=head,*t;
while(temp)
{
t=temp->next;
free(temp);
temp=t;
}
return 0;
}
int main()
{
int n,i;
char m[20];
printf("enter the number of elements to be inserted into the list:\n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%s",&m);
head=insert(head,m);
}
printf("search results:\n");
search(head);
printf("\n\n");
display(head);
return 0;
}
//Using HashSet in C# this is really easy code.
public static HashSet<string> UniqueWords()
{
string[] myArray = new string[] { "dog", "cat", "lion", "cat", "tiger", "cat", "rat" };
HashSet<string> HS = new HashSet<string>();
int i = 0;
while (i < myArray.Length)
{
HS.Add(myArray[i]);
i++;
}
Console.ReadLine();
return HS;
}
import java.util.LinkedList;
public class Repeats {
public String[] findConsecutiveRepeats(String[] s) {
LinkedList<String> l = new LinkedList();
for(int i=0; i<s.length; i++) {
char temp = s[i].charAt(0);
for(int j=1; j<s[i].length(); j++) {
if (temp == s[i].charAt(j)) {
l.add(s[i]);
break;
}
else
temp = s[i].charAt(j);
}
}
return l.toArray(new String[0]);
}
public static void main(String[] args) {
Repeats r = new Repeats();
String s[] = {"aa", "ab", "ac"};
String s1[] = r.findConsecutiveRepeats(s);
for(int i=0; i<s1.length; i++)
System.out.println(s1[i]);
}
}
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) {
String[] words = {"Dauresselam", "slab", "fuss", "boolean", "clap", "ddan", "endd"};
List<String> results = new ArrayList<String>();
for (String word:words) {
if (word.length() <= 1) break;
int prev = -1;
for (char ch:word.toCharArray()) {
if (prev >= 0 && ch - word.charAt(prev) == 0) {
results.add(word);
}
prev++;
}
}
for (String word:results) System.out.println(word);
}
}
Way more readable and thus maintainable...
public class charDup {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String[] array = { "india", "fuss", "hello", "glee" };
String[] output = new String[10];
int count = 0;
for (int i = 0; i < array.length; i++)
for (int j = 0; j < array[i].length() - 1; j++)
if (array[i].charAt(j) == array[i].charAt(j + 1)) {
System.out.println(array[i]);
output[count] = array[i];
count++;
}
}
}
package QATest;
public class QATest{
public String[] arrayStrings;
public static void main(String[] args)
{
QATest test = new QATest();
test.arrayStrings = new String[] {"a","aa","hello","what","zoom", "11223", "abbba", "-+??-"};
splitComparePrint(test.arrayStrings);
}
public static void splitComparePrint(String[] Array){
boolean repeated = false;
for (int i=0; i<Array.length; i++)
{
repeated = findRepetition(Array[i]);
if (repeated){
System.out.print(Array[i] + '\n');
}
}
}
public static boolean findRepetition(String string){
boolean repeated = false;
for (int i=0;i<string.length()-1;i++)
{
if (string.charAt(i)==string.charAt(i+1))
{
repeated = true;
return repeated;
}
}
return repeated;
}
}
public static List<string> FindRep(string[] arr)
{
List<string> list = new List<string>();
bool found = false;
int i = 0;
int j = 0;
// int k = 0;
foreach (string str in arr)
{
found = false;
for (i = 0; i < str.Length - 1; i++)
{
if (str[i] == str[i+1])
{
found = true;
break;
}
}
if (found == true)
{
list.Add(arr[j]);
j++;
}
else
{
j++;
}
}
return list;
}
import java.util.ArrayList;
public class ArrayOfStrings {
public static void main(String[] args)
{
// TODO Auto-generated method stub
String arr[]={"Dauresselam", "slab", "fuss", "boolean", "clap"};
ArrayList newarr=new ArrayList();
for(int i=0;i<arr.length-1;i++)
{
for(int j=0;j<arr[i].length()-1;j++)
{
if(arr[i].charAt(j)==arr[i].charAt(j+1))
{
newarr.add(arr[i]);
}
}
}
System.out.println(newarr);
}
}
public static void consecutiveOccurence(){
String[] arr={"Dauresselam", "slab", "fuss", "boolean", "clap"};
int size=arr.length;
boolean consecutive=false;
for(int i=0;i<size;i++){
String now=arr[i];
consecutive=false;
for (int j=1;j<now.length();j++){
if (now.charAt(j)==now.charAt(j-1)){
consecutive=true;
break;
}
}
if (consecutive){
System.out.println(now);
}
}
}
if regex is an option:
this can be used.
- Prithvi May 28, 2013