Write a program which has a function and that function
should take 2 or 3 or any number of strings and it should
return the largest common prefix of all those strings. If
there is no common prefix it should return an empty string.
for eg:- INPUT OUTPUT
glo {glory,glorious,glod}
gl {glad,glow}
{calendar,phone}
empty string
Answers were Sorted based on User's Feedback
Answer / jayakrishnan
strings[0].Substring(0,GetIndex(strings));
public static int GetIndex(string[] s)
{
int index=0;
for (int i = 0; i <= s.Length; i++)
{
if (i == s[0].Length) return i;
char comp = s[0][i];
for (int j = 1; j <= s[i].Length; j++)
{
if (j == s.Length)
{
index = j;
break;
}
if (i >= s[j].Length)
{
index = s[j].Length;
return index;
}
if (comp != s[j][i])
{
index = i;
return index;
}
}
}
return index;
}
Is This Answer Correct ? | 2 Yes | 0 No |
Answer / naresh
public class commonprefix
{
private static String CommonPrefix(String[] ss)
{
if (ss.length == 0)
{
return "";
}
if (ss.length == 1)
{
return ss[0];
}
int prefixLength = 0;
for(char c:ss[0].toCharArray())
{
for(String s:ss)
{
if (s.length()<= prefixLength ||
s.charAt(prefixLength)!= c)
{
return ss[0].substring(0, prefixLength);
}
}
prefixLength++;
}
return ss[0]; // all strings identical
}
public static void main(String args[])
{
System.out.println(""+CommonPrefix(args));
}
}
Is This Answer Correct ? | 0 Yes | 1 No |
Code for Working with Files under a Directory?
program to reverse the order of digits in a given number of any length.
Create a class called Accounts which has data members like ACCOUNT no, Customer name, Account type, Transaction type (d/w), amount, balance D->Deposit W->Withdrawal If transaction type is deposit call the credit(int amount) and update balance in this method. If transaction type is withdraw call debit(int amt) and update balance. Pass the other information like Account no,name,Account Type through constructor. Call the show data method to display the values.
How to add a value from textBox over an existing certain column in SQL Server
Write a program to input an integer and - display the reverse - display the sum of each digit - should include logic that considers the input number as any number of digits long
Write a program. there are 1..n numbers placed in an array in random fashion with one integer missing. find the missing number.
Write a function which accepts list of nouns as input parameter and return the same list in the plural form. Conditions: i) if last letter is r then append s ii) if word ends with y then replace it by ies iii) call this function in main() and produce the required output. for eg:- if chair is input it should give chairs as output.
Write a function which accepts a sentence as input parameter.Each word in that sentence is to be reversed. Space should be there between each words.Return the sentence with reversed words to main function and produce the required output. for eg:- i/p: jack jill jung kill o/p: kcaj llij gnuj llik
Hello I am hosting remoting within IIS. Everything works just fine as long as I allow anonymous access at the IIS level. When I allow only Windows Authenticated sessions, something very strange happens: If the client activates the remote object with IP address - works fine If the client activates it using a machine name - get http 401 exception is thrown. The exception is thrown when the client calls the remote method. I set the channel's useDefaultCredentials property to true. Any idea how to allow Windows Authentication? Here is the call stack: System.Net.WebException: The remote server returned an error: (401) Unauthorized .. ---> System.ComponentModel.Win32Exception: The target principal name is incorrect at System.Net.NTAuthentication.GetOutgoingBlob(Byte[] incomingBlob, Boolean handshakeComplete) at System.Net.NTAuthentication.GetOutgoingBlob(String incomingBlob, Boolean handshakeComplete) at System.Net.NegotiateClient.DoAuthenticate(String challenge, WebRequest webRequest, ICredentials credentials, Boolean preAuthenticate) at System.Net.NegotiateClient.DoAuthenticate(String challenge, WebRequest webRequest, ICredentials credentials, Boolean preAuthenticate) at System.Net.NegotiateClient.Authenticate(String challenge, WebRequest webRequest, ICredentials credentials) at System.Net.AuthenticationManager.Authenticate(String challenge, WebRequest request, ICredentials credentials) at System.Net.AuthenticationState.AttemptAuthenticate (HttpWebRequest httpWebRequest, ICredentials authInfo) --- End of inner exception stack trace ---
Code for IP Address Comparison and Conversion in C#?
I am developing a web application using google map api.I want to update the map inside the div control within update panel. I got other controls updated but map doesn't get updated.I do not not want to update map whenever unnecessary controls are fired at server side.How could it be achieved?
Give the code for Handling Mouse Events?