java 5-6
java 5-6
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;
//Initialization
int longestSubstringLength = 0;
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);
else
i = charPosMap.get(ch);
charPosMap.clear();
longestSubstringLength = charPosMap.size();
longestSubstring = charPosMap.keySet().toString();
}
public static void main(String[] args)
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