Click here to Skip to main content
15,905,414 members

Welcome to the Lounge

   

For discussing anything related to a software developer's life but is not for programming questions. Got a programming question?

The Lounge is rated Safe For Work. If you're about to post something inappropriate for a shared office environment, then don't post it. No ads, no abuse, and no programming questions. Trolling, (political, climate, religious or whatever) will result in your account being removed.

 
GeneralRe: Friday Programming Challenge Pin
PIEBALDconsult12-Sep-14 12:19
mvePIEBALDconsult12-Sep-14 12:19 
GeneralRe: Friday Programming Challenge Pin
DaveAuld12-Sep-14 12:27
professionalDaveAuld12-Sep-14 12:27 
GeneralRe: Friday Programming Challenge Pin
DaveAuld12-Sep-14 12:29
professionalDaveAuld12-Sep-14 12:29 
GeneralRe: Friday Programming Challenge Pin
PIEBALDconsult12-Sep-14 13:14
mvePIEBALDconsult12-Sep-14 13:14 
GeneralRe: Friday Programming Challenge Pin
Jörgen Andersson12-Sep-14 21:57
professionalJörgen Andersson12-Sep-14 21:57 
GeneralRe: Friday Programming Challenge Pin
PIEBALDconsult12-Sep-14 22:06
mvePIEBALDconsult12-Sep-14 22:06 
GeneralRe: Friday Programming Challenge Pin
Jörgen Andersson12-Sep-14 22:42
professionalJörgen Andersson12-Sep-14 22:42 
GeneralRe: Friday Programming Challenge Pin
PIEBALDconsult15-Sep-14 5:36
mvePIEBALDconsult15-Sep-14 5:36 
Here's what I devised.

Obviously, I didn't add any handling of special cases -- it assumes a collection of at least two non-NULL strings.

My first implementation builds up the result in a StringBuilder and spits it out once a difference is found or after all characters have been found to be equal.

C#
public static string
GetCommonPrefixA
(
  params string[] List
)
{
  System.Text.StringBuilder result = new System.Text.StringBuilder ( List [ 0 ].Length ) ;

  for ( int i = 0 ; i < List [ 0 ].Length ; i++ )
  {
    for ( int j = 1 ; j < List.Length ; j++ )
      if ( ( i >= List [ j ].Length ) || ( List [ 0 ] [ i ] != List [ j ] [ i ] ) )
        return ( result.ToString() ) ;

    result.Append ( List [ 0 ] [ i ] ) ;
  }

  return ( result.ToString() ) ;
}


This implementation doesn't use a StringBuilder; it simply uses the value from the outer for loop to perform a substring -- this might be better for languages that don't have a StringBuilder.

C#
public static string
GetCommonPrefixB
(
  params string[] List
)
{
  int i = 0 ;

  for ( ; i < List [ 0 ].Length ; i++ )
    for ( int j = 1 ; j < List.Length ; j++ )
      if ( ( i >= List [ j ].Length ) || ( List [ 0 ] [ i ] != List [ j ] [ i ] ) )
        return ( List [ 0 ].Substring ( 0 , i ) ) ;

  return ( List [ 0 ].Substring ( 0 , i ) ) ;
}


Because I'm not a fan of having multiple returns, I also devised this third implementation that uses a boolean to stop the loops.
Something I don't like about this is that the outer loop variable (i) is incrememted an extra time. Dead | X|

C#
public static string
GetCommonPrefixC
(
  params string[] List
)
{
  bool same = true ;
  int i = 0 ;

  for ( ; same && i < List [ 0 ].Length ; i++ )
    for ( int j = List.Length - 1 ; same && j > 0 ; j-- )
      same = ( ( i < List [ j ].Length ) && ( List [ 0 ] [ i ] == List [ j ] [ i ] ) ) ;

  return ( List [ 0 ].Substring ( 0 , i - 1 ) ) ;
}


Of course a goto would also work and not perform the extra increment.

C#
    public static string
    GetCommonPrefixD
    (
      params string[] List
    )
    {
      int i = 0 ;

      for ( ; i < List [ 0 ].Length ; i++ )
        for ( int j = List.Length - 1 ; j > 0 ; j-- )
          if ( ( i >= List [ j ].Length ) || ( List [ 0 ] [ i ] != List [ j ] [ i ] ) )
            goto done ;

done: return ( List [ 0 ].Substring ( 0 , i ) ) ;
    }


You may have noticed that these last two also run the second for loop (j) as a decrement; the purpose of this is to detect a difference as soon as possible when the collection has been sorted. It also reduces calls to List [ 0 ].Length. These "optimisations" probably won't yield any big improvements, but they're cheap.

modified 15-Sep-14 16:29pm.

GeneralAmazing... Pin
R. Giskard Reventlov12-Sep-14 9:51
R. Giskard Reventlov12-Sep-14 9:51 
GeneralAPOD PinPopular
R. Giskard Reventlov12-Sep-14 4:46
R. Giskard Reventlov12-Sep-14 4:46 
GeneralRe: APOD Pin
glennPattonWork312-Sep-14 6:00
professionalglennPattonWork312-Sep-14 6:00 
GeneralOT: Did you see... Pin
OriginalGriff12-Sep-14 6:06
mveOriginalGriff12-Sep-14 6:06 
GeneralRe: OT: Did you see... Pin
glennPattonWork312-Sep-14 6:40
professionalglennPattonWork312-Sep-14 6:40 
GeneralRe: APOD Pin
TheGreatAndPowerfulOz12-Sep-14 8:38
TheGreatAndPowerfulOz12-Sep-14 8:38 
GeneralOracle Sucks Pin
MehGerbil12-Sep-14 4:10
MehGerbil12-Sep-14 4:10 
GeneralRe: Oracle Sucks PinPopular
Corinna John12-Sep-14 4:42
Corinna John12-Sep-14 4:42 
GeneralRe: Oracle Sucks Pin
Rage12-Sep-14 4:48
professionalRage12-Sep-14 4:48 
GeneralRe: Oracle Sucks Pin
MehGerbil12-Sep-14 5:07
MehGerbil12-Sep-14 5:07 
GeneralRe: Oracle Sucks Pin
Wendelius12-Sep-14 6:32
mentorWendelius12-Sep-14 6:32 
GeneralRe: Oracle Sucks Pin
Roger Wright12-Sep-14 11:15
professionalRoger Wright12-Sep-14 11:15 
GeneralRe: Oracle Sucks Pin
DaveX8612-Sep-14 15:11
DaveX8612-Sep-14 15:11 
GeneralRe: Oracle Sucks Pin
Rage12-Sep-14 4:43
professionalRage12-Sep-14 4:43 
GeneralRe: Oracle Sucks Pin
MehGerbil12-Sep-14 5:05
MehGerbil12-Sep-14 5:05 
GeneralRe: Oracle Sucks Pin
Rage12-Sep-14 5:08
professionalRage12-Sep-14 5:08 
GeneralRe: Oracle Sucks Pin
MehGerbil12-Sep-14 5:10
MehGerbil12-Sep-14 5:10 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.