//
// main.m
// wordslgest
//
// Created by tql on 2021/3/14.
//
import <Foundation/Foundation.h>
@interface Solution : NSObject
-(NSString *)longestWord:(NSArray *)aray;
@end
@implementation Solution
-(NSString *)longestWord2:(NSArray *)aray{
NSMutableArray *LongesWords = [NSMutableArray array];
NSInteger maxlength = 0;
for (NSString *word in aray) {
if (word.length>maxlength) {
maxlength = word.length;
}
}
if (maxlength==0) {
return @"";
}
for (NSString *word in aray) {
if (word.length== maxlength) {
[LongesWords addObject:word];
}
}
[LongesWords sortUsingComparator:^NSComparisonResult(NSString * obj1, NSString * obj2) {
return [obj1 compare:obj2] ;
}];
return LongesWords.firstObject;
}
- (void) GetSubStr:(NSString)arayobj c:(NSString)str d:(NSString)str1 s:(NSString)str2{
NSArray *sepreateArray = [ str componentsSeparatedByString:arayobj];
NSLog(@"now subcount count =%hhd",sepreateArray.count);
if (sepreateArray.count==0) {
*str1 = @"";
*str2 = @"";
}else if (sepreateArray.count==1){
if ([str hasPrefix:arayobj]) {
*str1 = @"";
*str2 = [str stringByReplacingOccurrencesOfString:@"" withString:arayobj];
}else {
*str1 = [str stringByReplacingOccurrencesOfString:@"" withString:arayobj];
*str2 = @"";
}
}else if(sepreateArray.count==2){
*str1 = sepreateArray[0];
*str2 = sepreateArray[1];
}else {
}
}
-(BOOL) Ifcontain:(NSArray)aray s:(NSString)str f:(BOOL)skip {
for (int i=0; i<aray.count; i++) {
if ( [aray[i] isEqualToString:str])
{
NSLog(@"now the same str = %@, skip=%hhd",str,skip);
if (skip==YES)
break;
else
{
return YES;
}
}
}
for (int i=0; i<aray.count; i++) {
if ( [aray[i] isEqualToString:str])
{
NSLog(@"now the same str = %@, skip=%hhd",str,skip);
if (skip==YES)
continue;
}
if ([str containsString:aray[i]])
{
//NSArray *sepreateArray = [aray[i] componentsSeparatedByString:str];
NSString* str1, *str2;
str1=@"";
str2=@"";
// NSString* str1Save, *str2Save;
NSLog(@"find contain = %@ ",aray[i]);
[self GetSubStr:aray[i] c:str d:&str1 s:&str2];
NSLog(@"str1 = %@ strmid=%@ str2 = %@",str1,aray[i], str2);
BOOL skp = NO;
// if ( [str1 isEqual:str] || [str2 isEqual:str])
// skp = YES;
if (![str1 isEqual:@""] && ![self Ifcontain: aray s:str1 f:skp]){
NSLog(@"Now NO str1 = %@ ",str1);
return NO;
}
else if ([str2 isEqual:@""] || [self Ifcontain: aray s:str2 f:skp])
{
NSLog(@"Now find it ! str1 = %@ strmid = %@ str2 = %@",str1,aray[i], str2);
return YES;
}
}
}
return NO;
}
-(NSString *)longestWord:(NSArray *)aray{
if (aray.count==0) {//数据空
return @"";
}
NSMutableArray *sortArray = [aray sortedArrayUsingComparator:^NSComparisonResult(NSString * obj1, NSString * obj2) {
if (obj1.length==obj2.length) {
return [obj1 compare:obj2];
}
return obj1.length < obj2.length ? NSOrderedAscending : NSOrderedDescending;
}];
NSLog(@"%@",sortArray);
NSInteger firslenght = [sortArray[0] length];
NSInteger lastlenght = [[sortArray lastObject] length];
if (firslenght==lastlenght) {//全部一样长
return sortArray.firstObject;
}
BOOL find = NO;
/* */
NSString *result = @"";
for (NSInteger i = sortArray.count - 1; i > 0; i--) {
NSString *currentString = sortArray[i];
NSLog(@"最外层 currentString =%@",currentString);
NSInteger currentIndx = 0;
/*
for (NSInteger j = i- 1; j>=0; j--) {
currentIndx = j;
NSString *compareString = sortArray[j];
NSLog(@"内层:%@",compareString);
while ([currentString containsString:compareString]) {
currentString = [currentString stringByReplacingOccurrencesOfString:compareString withString:@""];
NSLog(@"余下的:::%@",currentString);
j = i - 1;
if ([currentString isEqualToString:@""] ) {
find = YES;
break;
}else if (currentString.length <= firslenght){
find = NO;
j = currentIndx -1;
currentString = sortArray[i];
break;
}else if(j<0){
j = currentIndx -1;
currentString = sortArray[i];
break;
}
}
if (find) {
break;
}
}
*/
find = [self Ifcontain:sortArray s:(sortArray[i]) f:YES];
if (find) {
result = sortArray[i];;
break;
}
}
return result;
return @"";
}
@end
int main(int argc, const char * argv[]) {
@autoreleasepool {
// insert code here...
NSLog(@"Hello, World!");
Solution *so = [Solution new];
//NSArray *words = @[@"apple",@"iOS",@"dog",@"nana",@"nzn",@"goodma",@"nznd",@"main",@"good",@"goodman",@"gooeman",@"goocman",@"goodmanznd"];
NSArray *words = @[@"apple",@"iOS",@"dog",@"nana",@"nzn",@"goodma",@"nznd",@"main",@"good",@"goodman",@"gooeman",@"goocman",@"nzngoodmadog"];
//NSArray *words = @[@"apple",@"iOS",@"dog",@"nana",@"nzn",@"goodma",@"nznd",@"main",@"good",@"goodman",@"gooeman",@"goocman",@"goodmanzndm",@"ain",@"goodmanzndmain"];
NSString *result = [so longestWord:words];
NSLog(@"result== %@",result);
}
return 0;
}