0% found this document useful (0 votes)
3 views

java 5-6

The document contains two Java programs: the first program finds the longest substring without repeating characters from a given string, while the second program extracts characters immediately before and after occurrences of a specified word in a string. Both programs utilize string manipulation techniques and data structures like LinkedHashMap for efficient processing. The code includes user input handling and demonstrates the functionality through examples.

Uploaded by

Shashank S
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

java 5-6

The document contains two Java programs: the first program finds the longest substring without repeating characters from a given string, while the second program extracts characters immediately before and after occurrences of a specified word in a string. Both programs utilize string manipulation techniques and data structures like LinkedHashMap for efficient processing. The code includes user input handling and demonstrates the functionality through examples.

Uploaded by

Shashank S
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

5.

Design and Develop a simple Java program to find the longest substring
without repeating characters in a given String. Accept the String through
Command Line argument.

package Pgm5;

import java.util.LinkedHashMap;

import java.util.Scanner;

public class Lab_5 {

static void longestSubstring(String inputString)

//Convert inputString to charArray

char[] charArray = inputString.toCharArray();

//Initialization

String longestSubstring = null;

int longestSubstringLength = 0;

//Creating LinkedHashMap with characters as keys and their position as values.

LinkedHashMap<Character, Integer> charPosMap = new LinkedHashMap<Character,


Integer>();

//Iterating through charArray

for (int i = 0; i < charArray.length; i++)

char ch = charArray[i];

//If ch is not present in charPosMap, adding ch into charPosMap along with its
position
if(!charPosMap.containsKey(ch))

charPosMap.put(ch, i);

//If ch is already present in charPosMap, reposioning the cursor i to the


position of ch and clearing the charPosMap

else

i = charPosMap.get(ch);

charPosMap.clear();

//Updating longestSubstring and longestSubstringLength

if(charPosMap.size() > longestSubstringLength)

longestSubstringLength = charPosMap.size();

longestSubstring = charPosMap.keySet().toString();

System.out.println("Input String : "+inputString);

System.out.println("The longest substring : "+longestSubstring);

System.out.println("The longest Substring Length : "+longestSubstringLength);

}
public static void main(String[] args)

Scanner sc= new Scanner(System.in);

System.out.println("Enter a string: ");

String str= sc.nextLine();

longestSubstring(str);

Program 6.
Given a string and a non-empty word string, return a string made of each char just before and
just after every appearance of the word in the string. Ignore cases where there is no char
before or after the word, and a char may be included twice if it is between two words.
If inputs are "abcXY123XYijk" and "XY", output should be "c13i".
If inputs are "XY123XY" and "XY", output should be "13". Create a Java program for the
same.

package javapgms;
public class pgm5 {
public static void main(String[] args) {
// TODO Auto-generated method stub
String Test="xvxXY123XYabcXY";
String word="XY";
String result= new String();
String pattern[]= Test.split(word); //1st string:xvx
// 2nd string:123
//3rd string:abc splitting the input string based on word pattern
for(int i=0;i<pattern.length;i++)
{
int length=pattern[i].length(); // number of strings returned after split operation

if(i==0) // first string


{
if (!(pattern[i]).equals("")) //if first string is empty, case when input string starts with word
string
{
String strend=pattern[i].substring(length-1,length); //if not, take the last character of the
string , eg abcXY123 , abc ->a xvx & XY take x
result=strend;
}
}
else if(i==pattern.length-1 && !(Test.endsWith(word))) // last string but not ending with
XY , eg abcXYhj, except cases like abcXY abcXY & XY take c
{ String strbegin= pattern[i].substring(0,1); // take the first character
result=result.concat(strbegin);
break; }
else
{
String strend=pattern[i].substring(length-1,length); //all other case take first and last
character of the string
String strbegin= pattern[i].substring(0,1);
result=result.concat(strbegin).concat(strend);
}
}
System.out.print("Final output "+ result);
}
}

You might also like