Intel Interview Question
Software Engineer / DevelopersCountry: United States
Interview Type: In-Person
import java.util.*;
import java.lang.*;
public class Firstocc {
/**
* @param args
*/
public static void main(String[] args)throws Exception {
String s="";
String ms=" ";
String sub="";
boolean b=false;
Scanner sc=new Scanner(System.in);
System.out.print("enter the actual string");
s=sc.next();
System.out.print("enter the searching string");
sub=sc.next();
int subl=sub.length();
System.out.print(subl);
int n=s.length()-subl;
for(int i=0;i<n;i++)
{
ms=s.substring(i,subl);
//System.out.println("matching string is"+ms);
if(sub.equalsIgnoreCase(ms))
{
b=true;
System.out.println("matching string first occurance position id:"+i);
exit(0);
}
subl++;
}
if(!b)
{
System.out.println("string doesn't match");
}
}
private static void exit(int i) {
System.exit(0);
}
}
sampe o/p:enter the actual stringhellokittuhwrukittu
enter the searching stringkittu
5matching string first occurance position id:5
Easy Fast Solution we know that we can index an array by a char A['d'], So we can use the same approach by
Initialize an BOOLEAN array of 128 (ascii chars) to false;
i.e. Boolean [] asciiArray = new Boolean[128]
for(int i < occurence.length(); i++) {
asciiArray[occurence[i]]=true;
}
After iitialization, we can map this array to the string we want to find the occurence, resetting if the no match.
class MyString {
public static void main(String []args) {
String str = "ababcdef";
String subStr= "abc";
boolean flag = false;
if (str.length() < subStr.length())
System.out.println(subStr + " is not a sub string of " + str);
int i = 0
for ( ; i < str.length() -subStr.length() + 1; i++) {
int index = 0;
flag = true;
for ( int j = 0 ; j < subStr.length() ; j++) {
if (str.charAt(i+index) != subStr.charAt(j)) {
flag = false;
break;
}
index++;
}
if (flag) break;
}
if (flag) {
System.out.println(subStr + " is a sub string of " + str);
} else {
System.out.println(subStr + " is not a sub string of " + str + " @ index of " + i);
}
}
}
Here we have to find the first occurance of one string in to another,
str1="abc"
str2="ahmedabad"
For this case, how many time 'a' occurs in str2 that we have to find.
Correct me if I am wrongly interpreting.
code:
char ch=str1.charAt(0);
int count=0;
for(i=0;i<str2.length();i++)
{
if(ch==str2.charAt(i))
count++;
else
continue;
}
Sytem.out.println("Occurance is : " + count + "times");
Does it not this much simple?
bool sub_string(char* m_str, char* sub_str)
{
if (m_str == NULL || sub_str == NULL)
return false;
char* m_str_tmp = m_str;
char* sub_str_tmp = sub_str;
int index;
bool flag = true;
while (*m_str_tmp)
{
if (*m_str_tmp == *sub_str_tmp)
sub_str_tmp++;
if (*m_str_tmp == NULL && *sub_str_tmp != NULL)
sub_str_tmp = sub_str;
if(*m_str_tmp != NULL && *sub_str_tmp == NULL)
return true;
if (*m_str_tmp == NULL && *sub_str_tmp != NULL)
return false;
m_str_tmp++;
}
return false;
}
bool sub_string(char* m_str, char* sub_str)
{
if (m_str == NULL || sub_str == NULL)
return false;
char* m_str_tmp = m_str;
char* sub_str_tmp = sub_str;
int index;
while (*m_str_tmp)
{
if (*m_str_tmp == *sub_str_tmp)
sub_str_tmp++;
if (*m_str_tmp == NULL && *sub_str_tmp != NULL)
sub_str_tmp = sub_str;
if(*m_str_tmp != NULL && *sub_str_tmp == NULL)
return true;
if (*m_str_tmp == NULL && *sub_str_tmp != NULL)
return false;
m_str_tmp++;
}
return false;
}
public static void SearchOne(String searchString, String OriginalString){
String temp="";
for(int i=0;i<OriginalString.length()-1;i++){
char c=OriginalString.charAt(i);
temp=""+c;
for(int j=i+1;j<OriginalString.length();j++){
temp=temp+OriginalString.charAt(j);
System.out.println("Temp is "+temp);
if(temp.equalsIgnoreCase(searchString)){
System.out.println("Found at "+i+"th and "+j +"th iteration");
return;
}
}
}
}
public static void SearchOne(String searchString, String OriginalString){
String temp="";
for(int i=0;i<OriginalString.length()-1;i++){
char c=OriginalString.charAt(i);
temp=""+c;
for(int j=i+1;j<OriginalString.length();j++){
temp=temp+OriginalString.charAt(j);
System.out.println("Temp is "+temp);
if(temp.equalsIgnoreCase(searchString)){
System.out.println("Found at "+i+"th and "+j +"th iteration");
return;
}
}
}
}
C- Solution
// Stringoccurence.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <conio.h>
#include <iostream>
#include <Windows.h>
using namespace std;
bool occurence(char* str1, char* str2)
{
int sizeStr2 = 0;
char* sample = str2;
bool bFound = false;
int count = 0;
//Compare str1and str2, increment str2 only if a match is found
while (*sample != '\0')
{
++sizeStr2;
++sample;
}
sample = str2;
while (*str1 != 0)
{
if (*str1 == *str2)
{
++str1;
++str2;
++count;
if (count == sizeStr2)
{
bFound = true;
return bFound;
}
}
else
{
++str1;
str2 = sample;
}
}
return bFound;
}
int _tmain(int argc, _TCHAR* argv[])
{
char* str1 = "Sampletesttest";
char *str2 = "test";
bool bFound = occurence(str1, str2);
cout << bFound;
_getch();
return 0;
}
#include<iostream>
using namespace std;
int strFind(string str,string str2)
{
//assume str.length()> str2.length()
for(int i=0;i<str.length();i++)
{
for(int j=0;true;j++)
{
if(str[i]==str2[j]){
i++;
}else{break;}
if(str2[j+1]=='\0')
{
return i-j-1;
}
}
}
return -1;
}
int main()
{
string src="why should i love programming? i love programming because it lets me not feel boring";
string sub=" love p";
cout<<"find str("<<sub<<") from str ("<<src<<") at "<< strFind(src, sub)<<endl;
}
In case of c or c++, we can use built in function strstr which will return the first occureence of substr in srcstr................
#include<iostream.h>
#include<string.h>
void main()
{
char src[]="why should i love programming? i love programming because it lets me not feel boring";
char sub[]=" love p";
char *p=strstr(src,sub);
if(p)
cout<<p;
else
cout<<"The string is not present";
}
Here is my code:
- chenlc626 March 06, 2013